html - Using setInterval in jquery to make gears scroll in a loop
習慣沉默
習慣沉默 2017-05-16 13:25:04
0
3
702
  1. There is a gear. The animation we are doing now is that the scroll event will be triggered when the mouse is hovering.

  2. The animation I want to make is that after the page is loaded, the gear will roll out and back again after a period of time.
    Both right scrolling and left scrolling can be achieved, but I don’t know how to write "after a period of time + roll out and roll back" in jquery

html:

<p id="wheel1">
    <p>Running right</p>
</p>
<p id = "wheel2">
    <p>Running left</p>
</p>

css:

<style type="text/css">
    #wheel1{
        width: 150px;
        height: 150px;
        background-color: pink;
        border:5px dotted purple;
        border-radius: 80px;
        float: right;
    }
    #wheel2{
        width: 150px;
        height: 150px;
        background-color: pink;
        border:5px dotted purple;
        border-radius: 80px;
        
    }
    #wheel2:hover{
        transform: translate(1000px) rotate(720deg);
        transition: transform 8s ease;
    }
     #wheel1:hover{
        transform: translate(-500px) rotate(-720deg);
        transition: transform 8s ease;
    }
    p{
        font-size: 25px;
        color: white;
        margin: 30px;
    }
習慣沉默
習慣沉默

reply all(3)
小葫芦

方法一:纯CSS 实现
给两个齿轮添加向左滚 和 向右滚的样式
html

<p id="wheel1" class="roll-left">
  <p>Running right</p>
</p>
<p id = "wheel2" class="roll-right">
  <p>Running left</p>
</p>

在样式里添加了无限循环滚动的动画。
如果需要滚出去隔一会再回来,可以把translate(-1000px)的值增大,比如 2000px,根据需求自己控制。
translate 的值增大后,需要响应的增大 rotate 的值,也是根据需求自己调节就行了。
css

#wheel1, #wheel2{
  width: 150px;
  height: 150px;
  background-color: pink;
  border:5px dotted purple;
  border-radius: 80px;
  position: absolute;
}
#wheel1{
  right: 0;
}
p{
  font-size: 25px;
  color: white;
  margin: 30px;
}
.roll-left{
  animation: roll-left 6s linear infinite; // 给动画添加 infinite 值,让动画无限循环
  -webkit-animation-direction:alternate; // 反向执行动画
  animation-direction:alternate;
}
.roll-right{
  animation: roll-right 6s linear infinite;
  -webkit-animation-direction:alternate;
  animation-direction:alternate;
}
@keyframes roll-left{
  from{}
  to {transform: translate(-1000px) rotate(-720deg)}
}

@keyframes roll-right{
  from{}
  to{transform: translate(1000px) rotate(720deg)}
}

方法二:使用jquery 控制
如果想用 jquery 控制的话,css 需要修改一下

.roll-left{
  animation: roll-left 8s linear;
}
.roll-right{
  animation: roll-right 8s linear;
}
@keyframes roll-left{
  0% {}
  50% {transform: translate(-1000px) rotate(-720deg)}
  100% {}
}
@keyframes roll-right{
  0% {}
  50% {transform: translate(1000px) rotate(720deg)}
  100% {}
}

js

setInterval(function(){
  $('#wheel1').addClass('roll-left').one('animationend', function() { // 每次动画完成后移除样式
    $('#wheel1').removeClass('roll-left');
  });
}, 2000); // 通过修改这个数值去控制每隔多久执行一次
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!