Ali*_*ici 6 html javascript css firefox google-chrome
我想制作一个3D矩形(平行六面体),用户可以用箭头移动.它在Chrome中运行良好,但在Firefox中,一些转换(实际上很多)与Chrome不同.看看这个小提琴(这是我的整个代码),并在两个浏览器中进行比较,以便更好地理解.
因为第一个小提琴包含很多代码,我会简化它并选择一个随机奇怪的过渡.看看这个小提琴,然后按"向左"按钮或向左箭头一次.它工作正常,但当你再次按下它时,矩形旋转3次而不是1次.
这是Firefox的错误还是我做错了什么?
下面的代码就是你在简化的小提琴中找到的代码.
var position = 'show-front';
$('#left').bind('click', function() {
if (position == 'show-front') {
$('#box').removeClass().addClass('show-right');
position = 'show-right';
} else if (position == 'show-right') {
$('#box').removeClass().addClass('show-back-3');
position = 'show-back-3';
} else if (position == 'show-back-3') {
$('#box').removeClass().addClass('show-left');
position = 'show-left';
} else if (position == 'show-left') {
$('#box').removeClass().addClass('show-front');
position = 'show-front';
}
});
$(window).bind('keyup', function(event) {
switch (event.keyCode) {
case 37: // left
$('#left').click();
break;
}
});Run Code Online (Sandbox Code Playgroud)
.container {
width: 150px;
height: 100px;
position: relative;
margin: 25px auto 25px auto;
perspective: 600px;
}
#box {
width: 100%;
height: 100%;
position: absolute;
transform-style: preserve-3d;
transition: transform 1s;
}
#box figure {
display: block;
position: absolute;
border: 1px solid black;
line-height: 98px;
font-size: 45px;
text-align: center;
font-weight: bold;
color: white;
}
figure {
margin: 0;
}
#box .front,
#box .back {
width: 148px;
height: 98px;
}
#box .right,
#box .left {
width: 48px;
height: 98px;
left: 50px;
}
#box .top,
#box .bottom {
width: 148px;
height: 48px;
top: 25px;
line-height: 48px;
}
#box .front {
background: hsla(000, 100%, 50%, 0.7);
}
#box .back {
background: hsla(160, 100%, 50%, 0.7);
}
#box .right {
background: hsla(120, 100%, 50%, 0.7);
}
#box .left {
background: hsla(180, 100%, 50%, 0.7);
}
#box .top {
background: hsla(240, 100%, 50%, 0.7);
}
#box .bottom {
background: hsla(300, 100%, 50%, 0.7);
}
#box .front {
transform: translateZ(25px);
}
#box .back {
transform: rotateX(180deg) translateZ(25px);
}
#box .right {
transform: rotateY(90deg) translateZ(75px);
}
#box .left {
transform: rotateY(-90deg) translateZ(75px);
}
#box .top {
transform: rotateX(90deg) translateZ(50px);
}
#box .bottom {
transform: rotateX(-90deg) translateZ(50px);
}
#box.show-front {
transform: translateZ(-50px);
}
#box.show-right {
transform: translateZ(-150px) rotateY(-90deg);
}
#box.show-back-3 {
transform: translateZ(-50px) rotateX(180deg) rotateZ(-180deg);
}
#box.show-left {
transform: translateZ(-150px) rotateY(90deg);
}Run Code Online (Sandbox Code Playgroud)
<section class="container">
<div id="box" class="show-front">
<figure class="front">1</figure>
<figure class="back">2</figure>
<figure class="right">3</figure>
<figure class="left">4</figure>
<figure class="top">5</figure>
<figure class="bottom">6</figure>
</div>
</section>Run Code Online (Sandbox Code Playgroud)
基于 Firefox 在这方面只是有 bug 的假设(参见下面的分析),这里有一个适用于 Firefox 的解决方法。它将#box元素包装在 another 中div,并且仅转换包装器。而且包装器一次只能从起点向一个方向旋转 90 度,因此 Firefox 不会把它搞砸。
过渡完成后,旋转将重置回起始位置,同时内框旋转到新位置,两者都没有过渡,因此更改不可见。
第二个重要的变化是使用当前计算的变换#box并将旋转添加到其中,这样我们就不必在进行时跟踪旋转。
请注意,旋转的顺序很重要。为了实现您想要做的事情(在“世界空间”而不是“对象空间”中旋转),您需要以相反的顺序应用旋转。例如,要“向右”旋转,请使用.css("transform", "rotateY(90deg) " + currentComputedTransform)。这将解决您在评论中提到的问题,该问题似乎围绕错误的轴旋转。请参阅下面的详细信息。
另请注意,如果已经进行轮换,我不允许开始轮换,因为这是行不通的。如果您希望能够这样做,您可以将击键排队到数组中,但您可能还希望在这种情况下减少与队列长度成比例的转换持续时间,这样就不会永远花费时间。
更新的小提琴:https://jsfiddle.net/955k5fhh/7/
相关JavaScript:
$("#box").wrap("<div id='outer'></div>");
var pending=null;
function rotate(axis,angle,dir) {
if (pending) return;
$("#outer").removeClass().addClass(dir);
var current=$("#box").css("transform");
if (current=='none') current='';
pending="rotate"+axis+"("+angle+"deg) "
+ current;
}
$("#outer").bind('transitionend', function() {
$(this).removeClass();
$("#box").css('transform',pending);
pending=null;
});
$('#up').bind('click', function() {
rotate('X',90,"up");
});
$('#down').bind('click', function() {
rotate('X',-90,"down");
});
$('#right').bind('click', function() {
rotate('Y',90,"right");
});
$('#left').bind('click', function() {
rotate('Y',-90,"left");
});
Run Code Online (Sandbox Code Playgroud)
我一直在玩基于 JS 的解决方案,我遇到了这篇有用的帖子https://gamedev.stackexchange.com/a/67317 - 它指出要在“世界空间”而不是“对象空间”中旋转对象,您只需颠倒旋转顺序即可。
基于此,我将您的小提琴简化为以下内容:
var rot = "";
var tr = "translateZ(-50px)";
$('#up').bind('click', function() {
rot=" rotateX(90deg)"+rot;
$("#box").css("transform",tr+rot);
});
$('#down').bind('click', function() {
rot=" rotateX(-90deg)"+rot;
$("#box").css("transform",tr+rot);
});
$('#right').bind('click', function() {
rot=" rotateY(90deg)"+rot;
$("#box").css("transform",tr+rot);
});
$('#left').bind('click', function() {
rot=" rotateY(-90deg)"+rot;
$("#box").css("transform",tr+rot);
});
Run Code Online (Sandbox Code Playgroud)
https://jsfiddle.net/955k5fhh/(请注意,这不是一个完整的解决方案,因为最终字符串rot会变得太长)
在 Chrome 上,其行为符合预期。即使您只是链接例如一系列rotateX(90deg)转换,Firefox 也会再次出错。
所以我更进一步,在同一轴上卷起相邻的旋转......
var rots = [];
var tr = "translateZ(-50px)";
function transform() {
var tf = "translateZ(-50px)";
rots.forEach(function(rot) {
tf += " rotate" + rot[0] + "(" + rot[1] + "deg)";
});
console.log(tf);
$("#box").css("transform", tf);
}
function addRot(axis,angle) {
if (rots.length==0 || rots[0][0]!=axis) {
rots.unshift([axis,angle]);
} else {
rots[0][1]+=angle;
}
transform();
}
$('#up').bind('click', function() {
addRot('X',90);
});
$('#down').bind('click', function() {
addRot('X',-90);
});
$('#right').bind('click', function() {
addRot('Y',90);
});
$('#left').bind('click', function() {
addRot('Y',-90);
});
Run Code Online (Sandbox Code Playgroud)
https://jsfiddle.net/955k5fhh/2/
同样,它在 Chrome 中运行良好,在 Firefox 中运行得更好一些,但一旦你切换轴,你可能会以错误的方式旋转。同样,如果您在转换完成之前单击按钮,它可能会以错误的方式旋转。
所以我的结论是,不幸的是,Firefox 在这方面确实存在缺陷,但至少有解决方法。
| 归档时间: |
|
| 查看次数: |
1157 次 |
| 最近记录: |