2017-01-03 77 views
-1

当鼠标悬停在元素中时,我需要动画,但是当用户使鼠标悬停时,动画停止在本地当前。当鼠标悬停在元素JS或Jquery中时循环

 countRotate = 0; 
 
     adiciona = true; 
 
     $('.circulo-left').mouseenter(function(){ 
 
      $('#circuloprincipalcomabaazulclaro').css('transform', 'rotate('+countRotate+'deg)'); 
 
      if(adiciona == true){ 
 
       countRotate++; 
 
       if(countRotate == 360){ 
 
        adiciona = false; 
 
       } 
 
      }else{ 
 
       countRotate = countRotate - 1; 
 
       if(countRotate == 0){ 
 
        adiciona = true; 
 
       } 
 
      } 
 
     }); 
 
     // I thought of something like that.
.circle{ 
 
     width:200px; 
 
     height:200px; 
 
     border-radius:50%; 
 
     border 
 
    } 
 
    .circle:hover{ 
 
     #circuloprincipalcomabaazulclaro{ 
 
      @include animation(rotate 3s infinite alternate); 
 
     } 
 
    } 
 
    @keyframes rotate { 
 
     from{ 
 
      transform: rotate(0deg); 
 
     } 
 
     to{ 
 
      transform: rotate(360deg); 
 
     } 
 
    } 
 
    /* But when I remove the mouse it returns to the starting point. */
<div class="circle"></div>

+1

我们都很好,谢谢关心。请分享您尝试过的内容并阅读[MCVE](http://stackoverflow.com/help/mcve) – philantrovert

+0

我们需要[mcve]比任何事情都重要。 –

+1

代码段不起作用。你需要包含jquery。 – G0dsquad

回答

3

更新:增加了一些JS来获得所需的输出。

你不需要jquery或js来获得这种效果。这是你需要做的。

编辑你的代码了一下。

const circle = document.querySelector('.circle'); 
 

 
circle.onmouseover = function(){ 
 
    this.style.animation = 'rotate 1.5s infinite linear'; 
 
    this.style.webkitAnimationPlayState="running"; 
 
} 
 

 
circle.onmouseout = function(){ 
 
    this.style.webkitAnimationPlayState="paused"; 
 
}
.circle { 
 
    width: 200px; 
 
    height: 200px; 
 
    border-radius: 50%; 
 
    background: red; 
 
    position: relative; 
 
    cursor: pointer; 
 
} 
 

 
span { 
 
    width: 5px; 
 
    height: 30px; 
 
    background: #fff; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 50%; 
 
    margin-left: -5px; 
 
} 
 

 
@keyframes rotate { 
 
    from { 
 
    transform: rotate(0deg); 
 
    } 
 
    to { 
 
    transform: rotate(360deg); 
 
    } 
 
}
<div class="circle"><span></span></div>

+0

使其成为方形,因为无法看到整个圆的旋转。 – Aslam

+0

@JohnWeisz,哈哈谢谢:) – Aslam

+0

这就是我的代码。 但这样,当用户移除鼠标时,它将返回到开始位置。 – Wellingtonfjr

1

我已经建立在@hunzaboy的回答中,OP表示,他希望动画,以保持它的状态,所以我做了一些调整,也取得了旋线性的。

自旋会留是你离开它999999999秒(约有277小时)

.circle{ 
 
     width:200px; 
 
     height:200px; 
 
     border-radius:50%; 
 
     background: red; 
 
     position: relative; 
 
     transition-property: transform; 
 
     transition-duration: 2s; 
 
     transition-delay: 999999999s; 
 
     transition-timing-function: linear; 
 
    } 
 
span{ 
 
    width: 10px; 
 
    height: 40px; 
 
    background: #fff; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 50%; 
 
    } 
 
    .circle:hover{ 
 
     transform: rotate(360deg); 
 
     transition-delay: 0s; 
 
    } 
 
    /* But when I remove the mouse it returns to the starting point. */
<div class="circle"><span></span></div>

+0

好的解决方案,谢谢。 – Wellingtonfjr

+0

我的荣幸 - 随时接受;-) –

+0

@Jamesonthedog,它只运行一次。检查我更新的答案:) – Aslam

相关问题