search

Home  >  Q&A  >  body text

Invalid workaround for setTimeout combined with Mouseenter

$(".item-holder").each(function(){
    $(this).bind("mouseenter", function(e) {
        e.preventDefault();
        var t = setTimeout(function(){
            $(this).find(".mindex-blog-meta").animate({
                left:"0"
            },500);
            $(this).find(".ret").animate({
                top:"60%"
            },600);
        });
    },300)
});

$(".item-holder").each(function(){
    $(this).bind("mouseleave", function(e) {
        e.preventDefault();
        clearTimeout(t);
        $(this).find(".mindex-blog-meta").animate({
            left:"-60%"
        },500);

        $(this).parent().find(".ret").animate({
            top:"100%"
        },600);
    });
});
習慣沉默習慣沉默2744 days ago850

reply all(2)I'll reply

  • 狂飙的蜗牛

    狂飙的蜗牛2018-12-09 21:07:35

    Here is an article that explains it in great detail, as well as an online example:

    http://www.aijquery.cn/Html/jqueryjiqiao/130.html

    reply
    0
  • ringa_lee

    ringa_lee2017-05-19 10:47:28

    1. If you encounter multiple elementsand need to bind the same event processing, use an event proxy instead, that is, bind the event to the common parent element of these elements, and then use the selector to hit the specified element. See the documentation for specific methods;

    2. If you wantclearTimeout(), the variable must be found. The variable t you declare in the anonymous function is trapped in the scope and cannot be come out~

    3. No needsetTimeout, don’t jQ’s animations have delays?

    4. Use stop to achieve the effect of
    5. clearTimeout().

    reply
    0
  • Cancelreply