【问题标题】:Multiple jQuery rollover/out button timeout issue?多个 jQuery 翻转/退出按钮超时问题?
【发布时间】:2013-06-23 20:44:06
【问题描述】:

我目前正在设置一系列按钮,每个按钮都有一个标题,可以显示更多关于翻转的信息。页面布局的设计使介绍部分位于中间,周围有按钮。滚动任何按钮会触发一个隐藏介绍的功能,然后显示有关正在与哪个按钮交互的更多信息。在推出时,我希望再次显示介绍,但在超时之后。

我遇到了一些超时不工作的问题 - introshow 函数似乎没有触发。

我试图让示例保持简单 - 实际上 introshow 函数将包含一些动画,但这与我遇到问题的超时函数无关。说到 jQuery,我还是个新手!

>> I've created a fiddle for this also

JS:

    $(document).ready(function() {

      $('div.pcontents > div').addClass('hidden');

      $('.person').each(function() {
        var theClass = $(this).attr('id');
        var $person = $(this)

        $person.hover(

        function() { // RollOver
           var index = 0;
          console.log('roll over')
          introhide();
          $('.pcontents .' + theClass).removeClass('hidden');
          $person.addClass('active');

        }, function() { // RollOut
          console.log('roll out')
          $('.pcontents .' + theClass).addClass('hidden');
          $person.removeClass('active');
          $(function() {

            setTimeout(function() {
              introshow();
            }, 2000 + index++);

          });
        });

      });

      var introhide = function() {
        $('#intro').addClass('hidden');
      }
      var introshow = function() {
        $('#intro').removeClass('hidden');
      }

    });

这里是 HTML

<div id="wrap">

<div id="persons">

    <dl class="person" id="pone">
        <dd>The first person</dd>
    </dl>   

    <dl class="person" id="ptwo">
        <dd>The second person</dd>
    </dl>   

    <dl class="person" id="pthree">
        <dd>The third person</dd>
    </dl>   

    <dl class="person" id="pfour">
        <dd>The forth person</dd>
    </dl>
</div>

<div class="pcontents">
    <div class="pone">  
        <h3>Person One</h3>
        <p>Description one</p>
    </div>
    <div class="ptwo">  
        <h3>Person two</h3>
        <p>Description two</p>
    </div>
    <div class="pthree">  
        <h3>Person three</h3>
        <p>Description three</p>
    </div>
    <div class="pfour">  
        <h3>Person four</h3>
        <p>Description four</p>
    </div>

</div>

<div id="intro">
    <p>This is the intro</p>
</div>

如果有人可以帮助我,那就太好了。

丰富

【问题讨论】:

    标签: jquery callback timeout rollover


    【解决方案1】:

    ReferenceError: index is not defined

    index 变量的作用域使其他函数无法访问它,该函数无法“看到它”。您需要将其移到函数定义之外,以便所有函数都可以访问:

    $(document).ready(function(){
        var index = 0,
        timeout = 0,
        introhide = function() {
            $('#intro').addClass('hidden');
        },
        introshow = function() {
            $('#intro').removeClass('hidden');
        };
    
        $('div.pcontents > div').addClass('hidden');
        $('.person').each(function(){
            var $person = $(this),
            theClass = $this.attr('class');
    
            $person.hover(function(){ // RollOver
                if (timeout) {
                    clearTimeout(timeout);
                    timeout = 0;
                }
    
                introhide();
                $('.pcontents .' + theClass).removeClass('hidden');
                $person.addClass('active');
            },function(){ // RollOut
                if (timeout) {
                    clearTimeout(timeout);
                    timeout = 0;
                }
    
                $('.pcontents .' + theClass).addClass('hidden');
                $person.removeClass('active');
    
                timeout = setTimeout(function() {
                    introshow();
                }, 2000 * index++); // Delay will grow after each hover event
            });
        });
    });
    

    【讨论】:

    • 非常感谢 - 现在说得通了。我一直在兜圈子:]
    • 请注意,您的代码存在一些缺陷,超时不会按预期工作,连续单击两个按钮会导致 hidden 类在第二个延迟指定之前被删除。
    • 嗨 - 感谢您的信息。我必须注意到,滚动按钮相当快地显示介绍 div,同时仍将鼠标悬停在按钮上。您还有其他方法可以建议吗?感谢您的帮助:]
    • 更新了我的答案,使用超时 id 将允许您在事件发生之前取消事件,并设置一个新事件。
    • 再次感谢:] 我用这个链接调整了小提琴以显示它工作:jsfiddle.net/richimgd/EQBCm/8
    猜你喜欢
    • 2014-10-07
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多