在mousemove随机化阵列图像

时间:2016-12-21 17:27:39

标签: jquery css

我有这个代码在悬停时更改图像,有没有办法在div内部的mousemove上更改它 谢谢。

  var images = ["images/warlo1.svg",
              "images/warlo2.svg",
              "images/warlo3.svg",
              "images/warlo4.svg",
              "images/warlo5.svg",
              "images/warlo6.svg",
              "images/warlo7.svg",
              "images/warlo8.svg",],

i = 0,//counter
$swap = $(".swap"),//only get the object once
swapper;//setup a var for setInterval

function swapImg(){
    i = Math.floor(Math.random() * images.length);//keep i under the      array length
    $swap.attr("src",images[i]);      
    i++;
}

$swap.hover(function(){//mouseover
    swapper = setInterval(swapImg,300);//call function every 400
},function(){//mouseout
    clearInterval(swapper);
});

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

https://api.jquery.com/mousemove/

$swap.mousemove(function( event ) {
swapImg()
});

答案 1 :(得分:0)

function swapImg(){
    if (toggle) {
       // swap image here

        toggle = false;
        setTimeout(function() {
          toggle = true;
        }, 5000);
    }
}