鼠标悬停和mouseout无法在Firefox上运行?

时间:2015-02-05 06:55:53

标签: javascript jquery slideshow mouseover mouseout

我想在鼠标悬停时尝试图像幻灯片放映并停止鼠标移出 以下是我的代码:但鼠标移出鼠标也是调用..在chrome上工作正常......

default_image = '';
timer = 0;

jQuery('.sales-product-images').on('mouseover',function(){
    var counter = 0;
    var selector = jQuery(this);
    var pd_id = jQuery(this).attr('id');
    var imageArray = jQuery.parseJSON(images);
    var product_images= imageArray[pd_id];
    default_image = jQuery(this).attr('data-image');
    console.log('default-image= ' + default_image);
    timer = setInterval(function(){selector.fadeOut("fast", function () {
        console.log(counter);
        if (counter === product_images.length) {
            console.log('==');
            counter = 0;
        }
        console.log('localhost/product/' + product_images[counter]);
        selector.attr('src', 'localhost/product/' + product_images[counter]);
        selector.fadeIn(2500);
        counter = counter+ 1;
    });
}, 2000)});


jQuery('.sales-product-images').on('mouseleave', function() {
    console.log('now end');
    // var counter = 0;
    clearInterval(timer);
)}; 

问题是:“现在结束”也是在firefox上打印鼠标悬挂。哪个不应该。

2 个答案:

答案 0 :(得分:0)

试试这个:

jQuery('.sales-product-images').on('mouseout', function() {
   console.log('now end');
   // var counter = 0;
  clearInterval(timer);
)}; 

答案 1 :(得分:0)

问题可能是由于mouseovermouseleave一起使用,当它的配对事件应为mouseout时。配对可以在下面看到。

mouseover / mouseout

$( ".sales-product-images" )
   .mouseover(function() {
      console.log("mouse over");
   })
   .mouseout(function() {
      console.log("mouse out");
});

mouseenter / mouseleave

$( ".sales-product-images" )
   .mouseenter(function() {
      console.log("mouse enter");
   })
   .mouseleave(function() {
      console.log("mouse leave");
});

以上方法是.on("", function(){})方法的快捷方式。

您可以按如下方式重写您的JavaScript:

default_image = '';
timer = 0;

jQuery('.sales-product-images').mouseover(function(){
    var counter = 0;
    var selector = jQuery(this);
    var pd_id = jQuery(this).attr('id');
    var imageArray = jQuery.parseJSON(images);
    var product_images= imageArray[pd_id];
    default_image = jQuery(this).attr('data-image');
    console.log('default-image= ' + default_image);
    timer = setInterval(function(){
        selector.fadeOut("fast", function () {
            console.log(counter);
            if (counter === product_images.length) {
                console.log('==');
                counter = 0;
            }
            console.log('localhost/product/' + product_images[counter]);
            selector.attr('src', 'localhost/product/' + product_images[counter]);
            selector.fadeIn(2500);
            counter = counter+ 1;
        });
    }, 2000);
}).mouseout(function() {
    console.log('now end');
    // var counter = 0;
    clearInterval(timer);
});