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