我相信你的願望比其他答案張貼滿足要複雜一點 - 你希望圖像能夠順利淡化。此代碼在div中創建一個div,然後在mouseover上顯示內部(因此爲最高)div,將較低(最外層)div的背景更改爲循環中下一個div的背景,然後淡出較高圖像。它也有一個選擇性的鎖定機制,所以如果用戶用鼠標瘋狂,它不會以醜陋的方式使圖像閃爍。我希望這會有所幫助,並且如果您想要更深入地瞭解代碼的功能(大多數情況下您應該能夠閱讀它),請在評論中發佈。
此代碼有你的部分。
的Javascript:
var pos = 0, lockOut = 0;
var fade = 600; // change this if you like (in MS)
// fix these div names
var top = '#my-transitional-div', bottom = '#my-permanent-div';
var images = [ // fix these paths
'http://example.com/1.png',
'http://example.com/2.png',
'http://example.com/3.png',
'http://example.com/4.png',
'http://example.com/5.png'
];
$(document).ready(function() {
$(top).hide();
$(bottom).css("background-image",'url(' + images[pos] + ')');
$(bottom).live("mouseover",changeOut);
pos = images.length;
changeOut();
});
function changeOut() {
if (++lockOut > 1) {
lockOut--;
return;
}
$(top).css("background-image",'url('+images[pos-1]+')').show().fadeOut(fade);
if (pos >= images.length) pos = 0;
$(bottom).css("background-image",'url('+images[pos++]+')');
setTimeout(function(){lockOut--;},fade-10);
}
CSS:
/* fix these div names (also height, width) */
#my-transitional-div {
width: 500px;
height: 250px;
margin: 0;
padding: 0;
position: absolute;
left: 0;
top: 0;
}
#my-permanent-div {
position: relative;
width: 500px;
height: 250px;
margin: 0;
padding: 0;
}
HTML:
<div id="my-permanent-div">
<!-- notice that the "transitional" div (var=top) is INSIDE the other -->
<div id="my-transitional-div"></div>
</div>
如何添加其他4張圖片?我假設「-logo.jpeg」是主圖片,但是我在哪裏添加其他圖片?我可以只更改「藍色」,「綠色」 n',到我的圖像源像'images/blue_logo.png','images/green_logo.png'? – Bruno 2009-11-14 02:13:36
該腳本將根據「顏色」數組自動使用正確的文件名替換src值。因此,如果您的文件的名稱是:blue-logo.jpg,green-logo.jpg,red-logo.jpg,orange-logo.jpg,yellow-logo.jpg語句'colours [colIndex]'將取值(顏色名稱)並將其前綴爲'-logo.jpg',然後將其作爲src屬性值編寫。 – SimonDever 2009-11-14 04:06:07