javascript - jQuery mousemove 自行触发

标签 javascript jquery css

我正在尝试将视频播放器全屏显示,并且希望控件显示在 mousemove 上。由于没有 mousestop 我只是启动了一个计时器,如果它完成,它将隐藏工具。

问题 – 由于我怀疑回调的性质,效果会在显示和隐藏之间来回切换。 mouseMoving 类在 css 中用于添加 display: block !important,当它被删除时,theControls 返回到它的原始 css,即 显示:无!重要

 $('#theDiv').mousemove(

        function()
        {
          //When the mouse is moving, add the class which adds display: block !important
          $('#theControls').addClass('mouseMoving');

          // Clear the timer each time the mouse moves, so that if the mouse doesnt move for a full 2 seconds,
          // hide the controls by removing the afforementioned class.

          clearTimeout(timer);

          var timer = setTimeout(

            function() 
            {
              $('#theControls').removeClass('mouseMoving');
            },

          2000);
        }
      );

事实上,如果你进入全屏并移动鼠标,控件将出现,然后隐藏,然后开发工具将显示类 mouseMoving 不断被追加和删除,< em>即使我不再移动鼠标。这将无限期地继续下去,或者直到鼠标再次移动,然后循环重复。

最佳答案

看看我的 fiddle 。 https://jsfiddle.net/Lfzt5u9j/37/

基本上,取!important您的属性(property) display:none !important; css 部分并将其放在 display:block 上在.mouseMoving 。您必须这样做的原因是因为 css 的 ID 部分中的任何内容都会覆盖 css 的 CLASS 部分。如果你不明白,请摆弄我的 fiddle 或提出问题:D

然后,让你的 Js 看起来像我的 fiddle 。

var timer;
$('#theDiv').mousemove(function() {
    //When the mouse is moving, add the class which adds display: block !important
    //console.log($('.mouseMoving'));
    $('#theControls').addClass('mouseMoving');

    // Clear the timer each time the mouse moves, so that if the mouse doesnt move for a full 2 seconds,
    // hide the controls by removing the afforementioned class.
    clearTimeout(timer);
    timer = window.setTimeout(function() {
        $('#theControls').removeClass('mouseMoving');
    }, 2000);
});

实际上,为了覆盖!important在您的情况下,您所要做的就是使类选择器更加具体。

就像你的类(class) mouseMoving

.mouseMoving {
    display:block !important;
}

将其更改为:

div#theControls.mouseMoving {
    display:block !important;
}

关于javascript - jQuery mousemove 自行触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40814159/

相关文章:

javascript - 我想使用 javaScript (无 jquery)在 html 表列中的 100 个字符后添加 "..."或 ">>"图标

javascript - For 循环代码将每个循环的 x 轴间距加倍

jQuery:计算元素数量并将元素编号用作 CSS 类

javascript - 在内容交换 js 中触发同位素重新布局

html - 在保持斑马条纹的表格中隐藏/显示 TR

javascript - 如何仅针对导航器屏幕之一禁用抽屉导航滑动?

javascript - 为什么拒绝不冒泡?

jquery - 如何更改 jquery html 内容?

javascript - CSS类不适用于特定场景

jquery - 使用动画 vb.net 滚动到页面底部