2017-01-03 73 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

相關問題