鼠标悬停并悬停意图

发布于 2024-12-06 08:00:51 字数 1785 浏览 0 评论 0原文

我使用鼠标悬停来更改 jQuery Supersized 插件上的幻灯片编号,具体取决于您滑过哪个按钮。

但是,如果在滚动到另一个按钮之前尚未完成下一张幻灯片的淡入淡出动画,则不会更改幻灯片。

有没有办法每隔几毫秒重新检查一次鼠标是否仍在按钮上并加载幻灯片(如果尚未加载)?

另外,如果鼠标在一定时间内没有悬停在任何按钮上,我想加载不同的幻灯片。如何堆叠事件,以便鼠标移出引用所有按钮并添加时间事件?

到目前为止我的代码(鼠标悬停当前仅适用于最后一个按钮):

$(".mybutton1").mouseover(function() {
api.goTo(2);
});

$(".mybutton2").mouseover(function() {
api.goTo(3);
});

$(".mybutton3").mouseover(function() {
api.goTo(4);

}).mouseout(function(){
api.goTo(1);
});

提前致谢!

====================更新======================

非常感谢您的最新更新。不幸的是我无法让你的代码工作。但是,我想我可能已经找到了一个更简单的解决方案,修改您的原始代码并使用 jQuery hideIntent...

我发现我可以禁用 Supersized 来阻止动画期间幻灯片的更改,并且我可以使用 hooverIntent 来确保它在更改之前等待足够的时间滑动以使动画不会堆叠起来。

以下代码可以完美地更改鼠标悬停时的幻灯片。

但我无法让 mouseout 工作,因为它为每个按钮创建一个新实例,并且当鼠标从一个按钮滚到下一个按钮时,它会堆叠动画。此外,似乎只有鼠标移出的延迟计时器,而不是像鼠标悬停那样的间隔选项。

所以我只需要稍微修改一下,这样:

如果鼠标没有在任何按钮上停留 1000 毫秒,则 api.goTo(1);

我能想到的唯一方法是创建一个浏览器窗口整个大小的不可见链接,并运行第二个hoverIntent函数以在滚动到此时更改幻灯片,但我认为这不是最好的方法!

谢谢

var buttonNumber = <?php echo $project_count; ?>; //Change this number to the number of buttons.
var prefix = "#project-link";
var prefix2 = "#project-bullet";

for(var i=0; i<buttonNumber; i++){

   (function(i){ //Anonymous function wrapper is needed to scope variable `i`
      var id = prefix+i;

      $(id).hoverIntent({
         interval: 350,
         over: mouseyover, 
         timeout: 1000,
          out: mouseyout
      });

      function mouseyover(){
         api.goTo(i+2);
         $(".project-bullet").fadeOut(1);
         $(prefix2+i).fadeIn(1000);
      }

      function mouseyout(){
         //api.goTo(1);
      }
   })(i);
}

I'm using mouseover to change the slide number on the jQuery Supersized plugin depending on which button you rollover.

However, if the fade animation to the next slide hasn't completed before rolling over another button, it doesn't change the slide.

Is there a way to re-check every few milliseconds whether the mouse is still over the button and load the slide if it hasn't been loaded already?

Also I'd like to load a different slide if the mouse hasn't been over any of buttons for a certain amount of time. How can I stack up the events so the mouseout refers to all of the buttons and also add a time event?

My code so far (mouseout currently only applies to the last button):

$(".mybutton1").mouseover(function() {
api.goTo(2);
});

$(".mybutton2").mouseover(function() {
api.goTo(3);
});

$(".mybutton3").mouseover(function() {
api.goTo(4);

}).mouseout(function(){
api.goTo(1);
});

Thanks in advance!

.

=====================UPDATE=====================

Thanks very much for your latest update. Unfortunately I couldn't get your code to work. However, I think I may have found an simpler solution modifying your original code and using jQuery hoverIntent…

I found out I could disable Supersized from stopping the slides changing during the animation and I can use hoverIntent to ensure that it waits enough time before changing the slide so the animations don't stack up.

The following code works perfectly changing slides on mouseover.

But I can't get mouseout to work because it's creating a new instance of it for each button, and it stacks up the animations when the mouse rolls out of one button and onto the next. Also there only seems to be a delayed timer for the mouseout and not an interval option like the mouseover.

So I just need to modify this slightly so that:

If the mouse is not over ANY of the buttons for 1000 ms, then api.goTo(1);

The only way I can think of would be to create an invisible link the entire size of the browser window and run a second hoverIntent function to change the slide when it rolls over this, but I don't think that would be the best way!

Thanks

var buttonNumber = <?php echo $project_count; ?>; //Change this number to the number of buttons.
var prefix = "#project-link";
var prefix2 = "#project-bullet";

for(var i=0; i<buttonNumber; i++){

   (function(i){ //Anonymous function wrapper is needed to scope variable `i`
      var id = prefix+i;

      $(id).hoverIntent({
         interval: 350,
         over: mouseyover, 
         timeout: 1000,
          out: mouseyout
      });

      function mouseyover(){
         api.goTo(i+2);
         $(".project-bullet").fadeOut(1);
         $(prefix2+i).fadeIn(1000);
      }

      function mouseyout(){
         //api.goTo(1);
      }
   })(i);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回旧景 2024-12-13 08:00:51

更新答案(2011 年 10 月 9 日):
修订:

  1. 第 4 行:var goToApiOne = false; //新增:Api 1的“内存”
  2. Func mouseyover,第一行:goToApiOne = false; //取消对 Api 1 的任何移动
  3. Func mouseyout:见下文,整个函数已被替换

此代码背后的概念:

  1. 鼠标悬停在任何内容上(goToApiOne = false)
  2. 鼠标移出任何内容 goToApiOne = true + setTimeout 并延迟 1000 毫秒
  3. 如果在 1000 毫秒过去之前将鼠标悬停在另一张幻灯片上,则转到步骤1
  4. 通过 setTimeout 调用函数:If goToApiOne == true THEN gotoApi(1); and
    goToApiOne = false代码>(重置)。

var buttonNumber = <?php echo $project_count; ?>; //Change this number to the number of buttons.
var prefix = "#project-link";
var prefix2 = "#project-bullet";
var goToApiOne = false; //New: the "memory" of Api 1

for(var i=0; i<buttonNumber; i++){

   (function(i){ //Anonymous function wrapper is needed to scope variable `i`
      var id = prefix+i;

      $(id).hoverIntent({
         interval: 350,
         over: mouseyover, 
         timeout: 1000,
          out: mouseyout
      });

      function mouseyover(){
         goToApiOne = false; //Cancel any move to Api 1
         api.goTo(i+2);
         $(".project-bullet").fadeOut(1);
         $(prefix2+i).fadeIn(1000);
      }

      function mouseyout(){
         goToApiOne = true; //Activate delay
         setTimeout(function(){
             if(goToApiOne){
                 goToApiOne = false; //Disable GoTo Api 1
                 api.goTo(1);
             }
         }, 1000);//Timeout of 1000ms
      }
   })(i);
}



Old answer (28 sept 2011):

我已经更改了您的代码,以便您可以轻松地将效果应用于多个按钮,而无需复制粘贴函数的内容。

阅读下面代码中的注释,并根据您的意愿调整代码。在淡入淡出函数和按钮事件处理程序的范围内包含 fadeFinished 变量非常重要。

var buttonNumber = 3; //Change this number to the number of buttons.

var poller = {interval:0, delay:0}; //timeout reference
var prefix = "#projectlink";
function pollerFunc(i, delay){
    //i = buttonNumber.
    //delay = number of intervals before activating a requested slide
    if(delay !== true){
        //Clean-up
        poller.clearInterval(poller.interval);
        poller.delay = delay || 0;

        //Set new interval. 50 milliseconds between each call
        poller.setInterval(function(){pollerFunc(i, true)}, 50);
    }
    else if(!vars.in_animation){
        //Check whether a delay has been requested. If yes, decrease the delay
        //  counter. If the counter is still higher than zero, return.
        if(poller.delay > 0 && --poller.delay > 0) return;

        window.clearInterval(poller.interval);
        var gotoNum = (i+2) % buttonNumber;
        // % = Modulo = Go back to x when the number = buttonNumber + x
        api.goTo(gotoNum);
    }
}
for(var i=0; i<buttonNumber; i++){
    (function(i){ //Anonymous function wrapper is needed to scope variable `i`
        var id = prefix+i;
        $(id).mouseover(function(){
            pollerFunc(i, 0); //No delay, if possible.
        })
        .mouseout(function(){
            pollerFunc(i, 10); //Delay 10 intervals (50x10 = 500 milliseconds
                               // before fading back to slide 1
        })
    })(i);
}

另请注意:我建议将 .projectlink 更改为 #projectLink,因为该元素应该只出现一次。

预期行为

  1. 鼠标悬停在#projectlink2上:转到幻灯片 4(第一次,无延迟)
  2. < em>mouseout:设置延迟 500 毫秒的幻灯片 1 请求
  3. 将鼠标悬停在 #projectlink1 上(500 毫秒内) 中止“幻灯片 1 请求”。启动幻灯片 3
  4. 鼠标悬停(在动画完成之前)设置延迟 500 毫秒的幻灯片 1 请求
  5. 鼠标悬停# projectlink3:前面的(参见3)动画尚未完成。立即请求“幻灯片 5”
  6. 幻灯片动画 (3) 已完成 立即开始幻灯片 5
  7. mouseout 设置延迟 500 毫秒的幻灯片 1 请求
  8. 已过去 500 毫秒:启动幻灯片 1
  9. 等等。

新功能一次只会运行一张幻灯片。如果您想查看多个(排队的)幻灯片的代码,请参阅修订历史记录。

Updated answer (9 oct 2011):
Revisions:

  1. Line 4: var goToApiOne = false; //New: the "memory" of Api 1
  2. Func mouseyover, first line: goToApiOne = false; //Cancel any move to Api 1
  3. Func mouseyout: See below, the whole function has been replaced

The concept behind this code:

  1. Mouseover anything (goToApiOne = false)
  2. Mouseout anything goToApiOne = true + setTimeout with a delay of 1000ms
  3. IF hovered over another slide before 1000ms have been passed THEN go to step 1
  4. Function call by setTimeout: If goToApiOne == true THEN gotoApi(1); and
    goToApiOne = false (reset).

var buttonNumber = <?php echo $project_count; ?>; //Change this number to the number of buttons.
var prefix = "#project-link";
var prefix2 = "#project-bullet";
var goToApiOne = false; //New: the "memory" of Api 1

for(var i=0; i<buttonNumber; i++){

   (function(i){ //Anonymous function wrapper is needed to scope variable `i`
      var id = prefix+i;

      $(id).hoverIntent({
         interval: 350,
         over: mouseyover, 
         timeout: 1000,
          out: mouseyout
      });

      function mouseyover(){
         goToApiOne = false; //Cancel any move to Api 1
         api.goTo(i+2);
         $(".project-bullet").fadeOut(1);
         $(prefix2+i).fadeIn(1000);
      }

      function mouseyout(){
         goToApiOne = true; //Activate delay
         setTimeout(function(){
             if(goToApiOne){
                 goToApiOne = false; //Disable GoTo Api 1
                 api.goTo(1);
             }
         }, 1000);//Timeout of 1000ms
      }
   })(i);
}



Old answer (28 sept 2011):

I have changed your code, so that you can easily apply effects to multiple buttons without having to copy-paste the contents of the function.

Read the comments in the code below, and adjust the code to your wishes. It's important to have the fadeFinished variable in the scope of your fade function, and the button event handler.

var buttonNumber = 3; //Change this number to the number of buttons.

var poller = {interval:0, delay:0}; //timeout reference
var prefix = "#projectlink";
function pollerFunc(i, delay){
    //i = buttonNumber.
    //delay = number of intervals before activating a requested slide
    if(delay !== true){
        //Clean-up
        poller.clearInterval(poller.interval);
        poller.delay = delay || 0;

        //Set new interval. 50 milliseconds between each call
        poller.setInterval(function(){pollerFunc(i, true)}, 50);
    }
    else if(!vars.in_animation){
        //Check whether a delay has been requested. If yes, decrease the delay
        //  counter. If the counter is still higher than zero, return.
        if(poller.delay > 0 && --poller.delay > 0) return;

        window.clearInterval(poller.interval);
        var gotoNum = (i+2) % buttonNumber;
        // % = Modulo = Go back to x when the number = buttonNumber + x
        api.goTo(gotoNum);
    }
}
for(var i=0; i<buttonNumber; i++){
    (function(i){ //Anonymous function wrapper is needed to scope variable `i`
        var id = prefix+i;
        $(id).mouseover(function(){
            pollerFunc(i, 0); //No delay, if possible.
        })
        .mouseout(function(){
            pollerFunc(i, 10); //Delay 10 intervals (50x10 = 500 milliseconds
                               // before fading back to slide 1
        })
    })(i);
}

Another note: I recommend changing .projectlink to #projectLink, because the element should only occur once.

Expected behaviour:

  1. mouseover #projectlink2: Go to slide 4 (first time, no delay)
  2. mouseout: Set a slide 1 request with a delay of 500 ms.
  3. mouseover #projectlink1 (within 500ms) Aborts "slide 1 request". Initiates slide 3
  4. mouseout (before the animation has finished) Set a slide 1 request with a delay of 500ms
  5. mouseover #projectlink3: The previous (see 3) animation has not finished yet. "Slide 5" is requested without a delay
  6. The slide animation (3) has finished Immediately starts slide 5.
  7. mouseout Set a slide 1 request with a delay of 500ms
  8. 500ms has passed: Initiates slide 1
  9. Et cetera.

The new function will only run one slide a time. If you want to see the code for multiple (queued) slides, see the revision history.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文