简体   繁体   English

当鼠标停止时,jQuery开始悬停动画

[英]jQuery start hover animation when mouse stops

So I have a sliding door style animation that as of now fires off when the mouse hovers over the box (my example is provided below). 所以我有一个滑门式动画,当鼠标悬停在盒子上时,它现在开始发射(我的例子在下面提供)。 Rather than it firing off immediately when the mouse hovers over the div I actually want the hover animation to only happen when the mouse stops on the div, much like it does in the chrome web store when you stop the mouse on one of the app containers (https://chrome.google.com/webstore/category/home) 当鼠标悬停在div上时,我实际上希望悬停动画只在鼠标停在div上时发生,而不是在鼠标停留在div上时发生,就像当你在其中一个app容器上停止鼠标时在chrome web商店中那样(https://chrome.google.com/webstore/category/home)

My Current Example http://jsfiddle.net/fvXgK/ 我的当前示例http://jsfiddle.net/fvXgK/

EDIT: Just to clarify further , there would be multiple cells appearing on this page. 编辑:为了进一步澄清,此页面上会出现多个单元格。

Thanks for any help you can provide!! 感谢您的任何帮助,您可以提供!!

My advice is to change your hover event. 我的建议是改变你的悬停事件。 On hover get the mouse coordinates then start a timeout with maybe 50-100ms. 在悬停时获取鼠标坐标然后开始超时可能50-100ms。 Use the timeout to compare the current mouse coordinates with the original set of coordinates. 使用超时将当前鼠标坐标与原始坐标集进行比较。 If they are the same, and your animation has not been executed, then do the animation. 如果它们相同,并且您的动画尚未执行,则执行动画。 This is just a rough idea of what you could try. 这只是你可以尝试的一个粗略的想法。

Edit: Also remember to make an on mouse out event that stops the timeout. 编辑:还记得制作一个停止超时的鼠标输出事件。

You can try the hoverintent plugin of jquery. 你可以尝试jquery的hoverintent插件。

http://cherne.net/brian/resources/jquery.hoverIntent.html http://cherne.net/brian/resources/jquery.hoverIntent.html

See this fiddle, easy to use: 看到这个小提琴,易于使用:

http://jsfiddle.net/pbaXa/ http://jsfiddle.net/pbaXa/

this is how I would do it... it's just easier: plain and simple: 这就是我要做的...它更简单:简单明了:

$('.smallCell.slidedown').hover(function() {
    $(".overlay", this).delay(1000).queue(function(){
    $(this).animate({
        top: '-260px'
    }, {
        queue: false,
        duration: 300
    });


    });
}, function() {
    $(".overlay", this).animate({
        top: '0px'
    }, {
        queue: false,
        duration: 300
    });
});

And this is also self explanatory: it will delay 1 second and queue your function. 这也是自我解释:它将延迟1秒并排队你的功能。

You can use a setTimeout for this and comparing to a stored "hover state" of the element. 您可以使用setTimeout进行此操作,并与元素的存储“悬停状态”进行比较。 My example might not be the best way to store the hover state if you are using multiple cells, but the concept still stands. 如果您使用多个单元格,我的示例可能不是存储悬停状态的最佳方式,但概念仍然存在。

var TIME_UNTIL_SLIDE = 1000;

var isHovering = false;
var hovertimer;

$('.smallCell.slidedown').hover(function() {
    isHovering = true;
    var $this = this;
    hovertimer = setTimeout(function(){
        if(isHovering) {
             $(".overlay", $this).stop().animate({
                top: '-260px'
            }, {
                queue: false,
                duration: 300
            });
        }
    }, TIME_UNTIL_SLIDE);

}, function() {
    isHovering = false;
    $(".overlay", this).stop().animate({
        top: '0px'
    }, {
        queue: false,
        duration: 300
    });
});

Shown at: http://jsfiddle.net/fvXgK/16/ 显示在: http//jsfiddle.net/fvXgK/16/

Maybe you should try this plugin: 也许你应该尝试这个插件:

http://www.richardscarrott.co.uk/posts/view/jquery-mousestop-event http://www.richardscarrott.co.uk/posts/view/jquery-mousestop-event

Easy to use and the example is just right there! 易于使用,这个例子恰到好处!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM