JQuery的.hover()
可以使用的mouseover
和mouseout
狀態的功能,所以這是容易做到像
HTML:
<img id="swap" src="http://lorempixum.com/200/200/sports" alt=""/>
的jQuery:
var images = ["http://lorempixum.com/200/200/sports",
"http://lorempixum.com/200/200/food",
"http://lorempixum.com/200/200/abstract",
"http://lorempixum.com/200/200/people",
"http://lorempixum.com/200/200/technics",
"http://lorempixum.com/200/200/city"
],//store a bunch of images in an array
i = 0,//counter
$swap = $("#swap"),//only get the object once
swapper;//setup a var for setInterval
function swapImg(){
i = i % images.length;//keep i under the array length
$swap.attr("src",images[i]);// change image src
i++;
}
$swap.hover(function(){//mouseover
swapper = setInterval(swapImg,10);//call function every 10ms
},function(){//mouseout
clearInterval(swapper);//stop calling the image swapper
});
here's a demo
我爲虛擬圖像使用了http://lorempixel.com/,但每次請求時都會返回一張隨機圖像,因此顯示的圖像比數組中顯示的圖像更多,這會使其有時會加載時髦。
您可能需要JavaScript。 – j08691 2012-03-21 19:06:24
我該怎麼用Javascript做到這一點?我願意深入研究一個例子 – patricko 2012-03-21 19:15:06
或者,想一想,您可以嘗試在懸停時顯示快速循環的動畫gif。這可行嗎? – 2012-03-21 19:44:01