javascript - 当鼠标悬停在元素 JS 或 Jquery 中时循环

标签 javascript jquery html css

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

        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>

最佳答案

更新:添加了一些 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>

关于javascript - 当鼠标悬停在元素 JS 或 Jquery 中时循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41442176/

相关文章:

Javascript 将代码包装在匿名函数中

javascript - 如何从输入onkeyup文本框jquery获取原始插入文本

html - 如何使用 3 种不同的设置更改平板电脑、移动设备和更广泛设备的引导轮播?

javascript - 使用 Chart.js 显示/隐藏条形图中的数据

javascript - 如何使用 jquery 或 bootstrap 禁用表中的链接

javascript - 使用 Underscore.js 检查 undefined variable

javascript - 使用 Kendo Mobile ListView ,如何在 HeaderTemplate 上设置多个值?

javascript - 没有 Bootstrap.css 的 jQuery 菜单目标

javascript - 在哪里将 html 添加到 MaterializeCSS 工具提示?

html - CSS 未命中行为