如何具体转变观点来源?

时间:2017-10-20 05:33:10

标签: javascript css css3

如果all属性不使用transition,我似乎无法为perspective-origin设置动画。

此代码适用于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

1 个答案:

答案 0 :(得分:1)

这可能是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;

&#13;
&#13;
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";
}
&#13;
/*
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);
}
&#13;
<div class="scene">
    <div class="object"></div>
</div>

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