【问题标题】:Autohide a div if any buttons are pressed如果按下任何按钮,则自动隐藏 div
【发布时间】:2016-10-21 09:03:59
【问题描述】:

我正在制作自己的视频播放器,顶部有一个信息 div,底部有一个播放栏 div。

如果按下任何按钮,这些条应该会在 5 秒内消失,如果您按下按钮,这些条应该会出现(如果它们被隐藏)或再停留 5 秒。

一开始我是这样想的

  document.onkeyup = function(event) {
    showBar('playbarDiv');
    showBar('infoDiv');

    setTimeout(function() {
     hideBar(); 
    }, 5000);

    if (hideCounter !== 1){
     focusOn('playButton');
     hideCounter = 1;
    }

   };

当然,每次按下按钮时,您都会在队列中添加一个 setTimeOut 函数,因此 5 秒后条形开始隐藏和显示。

当我按下按钮而不是“加载”新的时,我需要通过“重新启动”SetTimeOut 之类的方法来避免这种情况。

这是一个简单的例子:Demo

这可能吗?还是我需要使用与 setTimeOut 不同的东西?

提前致谢。

【问题讨论】:

    标签: javascript jquery html css timeout


    【解决方案1】:

    是的,有可能,您可以使用window.clearTimeout(yourTimeOutVariable) 清除之前的超时。检查your demo, updated

    您可以在此处阅读更多信息http://www.w3schools.com/jsref/met_win_cleartimeout.asp

    【讨论】:

      【解决方案2】:

      试试这个:

          jQuery(document).ready(function(){
        document.onkeyup = function(event) {
      
          if($("#content").not(":visible")){
              $("#content").fadeIn(500);
                      setTimeout(function() { $("#content").fadeOut(500); }, 5000);
      
           };
      
        };
      });
      

      【讨论】:

        猜你喜欢
        • 2021-03-21
        • 1970-01-01
        • 2017-09-19
        • 2021-02-26
        • 2020-11-06
        • 1970-01-01
        • 2021-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多