简体   繁体   English

JavaScript导致CSS动画无法正常工作

[英]JavaScript induced CSS animation not working

Following advice from this CSS Tricks article , I tried to write some code to induce a CSS transition using JavaScript. 根据CSS Tricks这篇文章的建议,我尝试编写一些代码来使用JavaScript引发CSS转换。

(Here's my jsFiddle .) (这是我的jsFiddle 。)

HTML: HTML:

<button id="button" onclick="doMove2()" value="play">Play</button>
<div class="foo"></div>

CSS: CSS:

#button {
    position: absolute;
    width: 200px;
    height: 50px;
    top: 185px;
    left: 0px;
}

.foo {
    position: absolute;
    width: 50px;
    height: 50px;
    border: 1px dashed black;
    left: 0px;
    top: 120px;
}

.foo.horizTranslate {
    -webkit-transition: 3s;
    -moz-transition: 3s;
    -ms-transition: 3s;
    -o-transition: 3s;
     transition: 3s;
     margin-left: 50% !important;
 }

JavaScript: JavaScript的:

var foo = document.getElementsByClassName('foo')[0];

function doMove2(){
    document.getElementById("button").onclick = function(){

        if(this.innerHTML === 'Play'){
            this.innerHTML = 'Pause';
            foo.classList.add('horizTranslate');

        } else {
            this.innerHTML = 'Play';
            var computedStyle = window.getComputedStyle(foo2),
            marginLeft = computedStyle.getPropertyValue('margin-left');
            foo.style.marginLeft = marginLeft;
            foo.classList.remove('horizTranslate');    
        }
    }
}
  1. Why doesn't this work? 为什么不起作用?

  2. Could someone explain the difference between a transition and an animation ? 有人可以解释一下过渡动画之间的区别吗?

Firstly, I don't recommend using jsfiddle, sometimes its glitchy and unreliable. 首先,我不建议使用jsfiddle,有时它会出现故障且不可靠。 I use jsbin, check my demo: http://jsbin.com/guceneku/1/ 我使用jsbin,请检查我的演示: http ://jsbin.com/guceneku/1/

I removed the onclick in your HTML tag, since you already had a trigger in js, so it becomes: 我删除了HTML标记中的onclick,因为您已经在js中创建了一个触发器,因此它变为:

<button id="button" value="play">Play</button>
<div class="foo"></div> 

Next your js becomes: 接下来,您的js变为:

var foo = document.getElementsByClassName('foo')[0];

document.getElementById("button").onclick = function(){
        if(this.innerHTML === 'Play'){
            this.innerHTML = 'Pause';
            foo.classList.add('horizTranslate');
        } else{
            this.innerHTML = 'Play';
            var computedStyle = window.getComputedStyle(foo2),
            marginLeft = computedStyle.getPropertyValue('margin-left');
            foo.style.marginLeft = marginLeft;
            foo.classList.remove('horizTranslate');    
        }
    }

You have no initial value defined. 您没有定义初始值。 It's defaulting to a margin of auto , and auto is not transitionable. 默认情况下为auto的余量,并且auto不可转换。

Also, you should define the transitions on the "base" state, otherwise it won't transition when removing the class. 另外,您应该在“基本”状态下定义过渡,否则在删除类时不会过渡。

So: 所以:

.foo {
    position:absolute;
    width:50px;
    height:50px;
    border:1px dashed black;
    left:0px;
    top:120px;
    margin-left: 0; /* ADD THIS */
    transition: 3s; /* all browsers support unprefixed now */
}

.foo.horizTranslate {
    margin-left: 50%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM