簡體   English   中英

jQuery mouseenter和mouseleave事件失控

[英]jQuery mouseenter and mouseleave events fire out of control

我正在使用這段代碼用懸浮元素的內容填充div。

$('.gallery .thumbs a').hover(
  function(){
    var target = $(this);
    $('.hover-box').html(target.clone());
    var top = target.offset().top;
    var left = target.offset().left;
    $('.hover-box').css({'display':'block', 'top':top, 'left':left}); 
  },
  function(){
    $('.hover-box').hide();
  }
);

問題是-許多人似乎已經擁有了-在添加了'mouseleave'處理程序之后,這兩個事件開始無法控制地觸發。

我知道與mouseover / out相關的冒泡問題,但這似乎是相同的。

有人知道為什么會這樣嗎?

編輯:

這是擺弄的事情。 不是最漂亮的景象,但功能與我的問題相同。 小提琴

這是因為您的函數會在每次hover以及在每次hover結束時觸發並重新觸發,因此每當您移動鼠標時,它都會觸發兩次。 你想要做的,而不是什么是火它mouseenter.thumbs amouseleave.hover-box這樣

jQuery(function () {
    jQuery('.thumbs a').hover(

    function () {
        var target = $(this);
        jQuery('.hover-box').html(target.clone());
        var top = target.offset().top;
        var left = target.offset().left;
        jQuery('.hover-box').css({
            'display': 'block',
            'top': top,
            'left': left
        });
    });
    $('.hover-box').mouseleave(function() {
        $('.hover-box').hide();
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM