首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在第一个动画css的位置开始第二个动画css

在第一个动画css的位置开始第二个动画css
EN

Stack Overflow用户
提问于 2018-06-18 00:35:28
回答 1查看 223关注 0票数 1

我需要链接两个动画在我的界面HTML/CSS的用户事件(这里只是一个点击文档)。第一个动画启动正确,但是当我想重新启动第二个动画时什么都不动?

我知道如果删除.rotaiotn类,并使用超时为元素放入其他动画类,则第二个动画将从元素的第一个位置开始。

我想知道是否存在从第一个动画之后的蓝球位置开始第二个动画的解决方案?

代码语言:javascript
复制
document.addEventListener('click', startAnimation, false);
var isFisrtAnim = false;

function startAnimation(evt) {
   var elt = document.querySelector('#blue_ball');
   if (!isFisrtAnim) {
      elt.setAttribute('class', 'rotation');
   } else {
      elt.setAttribute('class', 'rotation2');
   }
   elt.addEventListener("animationend", animationAtEnd, false);
}

function animationAtEnd(evt) {
   evt.preventDefault();
   isFisrtAnim = !isFisrtAnim;
   var elt = evt.target;
   // todo here get new position of elt to start another animation
   // from the new position after first animation
   var new_margin_top = window.getComputedStyle(elt).getPropertyValue('margin-top');
   var new_margin_left = window.getComputedStyle(elt).getPropertyValue('margin-left');
   console.log('At end new margin-top : ' + new_margin_top + ' - new margin-left : ' + new_margin_left);
   // positions are the same of start element ? they are not modify ?
}
代码语言:javascript
复制
#circleNav {
   background: rgba(215, 229, 231, 0.4) !important;
   margin-top: 100px;
   margin-left: 120px;
   border-radius: 50%;
   width: 335px;
   height: 335px;
   border: 2px solid #0e6694;
}

img {
   max-width: 100%;
}

#blue_ball {
   position: absolute;
   margin-top: -350px;
   margin-left: 165px;
   width: 70px;
   height: 70px;
   border: none;
   z-index: 5;
   transform-origin: 120px 180px;
}

.rotation {
   -webkit-animation: rotation 3s linear;
   animation-fill-mode: forwards;
   -webkit-animation-fill-mode: forwards !important;
}

@-webkit-keyframes rotation {
   from {
      -webkit-transform: rotate(0deg);
   }
   to {
      -webkit-transform: rotate(240deg);
   }
}

.rotation2 {
   -webkit-animation: rotation 3s linear;
   animation-fill-mode: forwards;
   -webkit-animation-fill-mode: forwards !important;
}

@-webkit-keyframes rotation2 {
   from {
      -webkit-transform: rotate(240deg);
   }
   to {
      -webkit-transform: rotate(360deg);
   }
}    
代码语言:javascript
复制
<h2>
   CLICK ON THE BODY TO START ANIMATION
</h2>
<h4>
   When the Blue ball stop click an other time to start second animation, but don't work ?

</h4>
<div id="circleNav"></div>
<div id="blue_ball">
   <a href="#">
            <img id="btn_menu" src="http://mascaron.net/img/mini_rond_logo.png">
        </a>
</div>

jsfiddle上的smaple代码

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-18 01:51:23

只有一个问题,用css表示:

代码语言:javascript
复制
.rotation2 {
   -webkit-animation: rotation 3s linear;
   animation-fill-mode: forwards;
   -webkit-animation-fill-mode: forwards !important;
}

不应该是:

代码语言:javascript
复制
.rotation2 {
   -webkit-animation: rotation2 3s linear;   /* <----- here, rotation2
   animation-fill-mode: forwards;
   -webkit-animation-fill-mode: forwards !important;
}

在js部分,为什么不使用elem.classList https://developer.mozilla.org/fr/docs/Web/API/Element/classList来操作css类属性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50898535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档