如果没有为all属性使用transition,我似乎无法制作透视图动画。

此代码在Safari中有效:

var sceneDiv = document.getElementsByClassName("scene")[0];
sceneDiv.style.perspectiveOrigin = "100% 0%";
sceneDiv.style.transition = "all 1s";


此代码不:

var sceneDiv = document.getElementsByClassName("scene")[0];
sceneDiv.style.perspectiveOrigin = "100% 0%";
sceneDiv.style.transition = "perspective-origin 1s";




function reset()
{
    var sceneDiv = document.getElementsByClassName("scene")[0];
    sceneDiv.style.perspectiveOrigin = "50% 50%";
    sceneDiv.style.transition = "none";
}

function transitionAll()
{
    var sceneDiv = document.getElementsByClassName("scene")[0];
    sceneDiv.style.perspectiveOrigin = "10% 50%";

    /* this works ok, but using "all" affects performance */
    sceneDiv.style.transition = "all 1s";
}

function transitionPO()
{
    var sceneDiv = document.getElementsByClassName("scene")[0];
    sceneDiv.style.perspectiveOrigin = "10% 50%";

    /* this doesn't animate the object as expected */
    sceneDiv.style.transition = "perspective-origin 1s";
}

.scene
{
    width: 400px;
    height: 300px;
    background: #404040;

    -webkit-perspective: 600px;
    -moz-perspective: 600px;
    -ms-perspective: 600px;
    -o-perspective: 600px;
    perspective: 600px;
}

.object
{
    position: absolute;
    margin-left: -200px;
    margin-top: -200px;
    left: 50%;
    top: 50%;
    width: 400px;
    height: 400px;
    background: #ff8000;

    -webkit-transform: translateZ(-1200px) rotateX(60deg);
    -moz-transform: translateZ(-1200px) rotateX(60deg);
    -ms-transform: translateZ(-1200px) rotateX(60deg);
    -o-transform: translateZ(-1200px) rotateX(60deg);
    transform: translateZ(-1200px) rotateX(60deg);
}

<div class="scene">
    <div class="object"></div>
</div>

<button onclick="reset()">reset</button>
<button onclick="transitionAll()">all 1s</button>
<button onclick="transitionPO()">perspective-origin 1s</button>





JSFiddle

我想避免使用all来提高性能。我在第二组说明中做错了什么?

编辑

我尝试在JavaScript中使用WebKit扩展。结果相同。

JSFiddle w/ WebKit

最佳答案

这可能是Safari中的错误,因为他们没有将transition: perspective-origin 1s;速记映射到transition: perspective-origin-x 1s, perspective-origin-y 1s;

清楚地设置这两个属性在Safari中也适用。
但是Firefox和Chrome不喜欢仅设置这两个属性,因此您实际上将需要其中三个:

transition: perspective 1s, perspective-origin-x 1s, perspective-origin-y 1s;




function reset()
{
    var sceneDiv = document.getElementsByClassName("scene")[0];
    sceneDiv.style.perspectiveOrigin = "50% 50%";
    sceneDiv.style.transition = "none";
}

function transitionPO()
{
    var sceneDiv = document.getElementsByClassName("scene")[0];
    sceneDiv.style.perspectiveOrigin = "10% 50%";

    /* this does animates the object as expected */
    sceneDiv.style.transition = "perspective-origin 1s, perspective-origin-x 1s, perspective-origin-y 1s";
}

/*
removed all the vendor specifics rules
since most modern browsers don't need it anymore
*/
.scene
{
    width: 400px;
    height: 300px;
    background: #404040;
    perspective: 600px;
}

.object
{
    position: absolute;
    margin-left: -200px;
    margin-top: -200px;
    left: 50%;
    top: 50%;
    width: 400px;
    height: 400px;
    background: #ff8000;
    transform: translateZ(-1200px) rotateX(60deg);
}

<div class="scene">
    <div class="object"></div>
</div>

<button onclick="reset()">reset</button>
<button onclick="transitionPO()">perspective-origin 1s</button>

关于javascript - 具体如何过渡透视图源?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46842895/

10-12 15:23