2011-12-03 29 views
0

我有這個ID在我的html代碼:onmouseover和超時JavaScript的

<div id="me"> 
<img src="Me.JPG" alt="Me" width="450" height="450" alt="picture" align="right"/> 
</div> 

我如何可以改變圖片每3秒一次的鼠標移動到圖片,

,並返回到原始圖片一旦鼠標熄滅?

+0

更簡單的方法來做到這一點會被使用jQuery,想重新標記的問題。還請說明其他圖片來自哪裏? – alonisser

+0

@alonisser - 儘管它確實可能使它更容易,但我們不應該強迫OP將手放在他/她想要使用的東西上。似乎沒有必要包含30K的庫代碼,用於可以用約10行普通的JavaScript編寫的東西。 –

+3

@alonisser - 你不需要jQuery,沒有它,它真的不那麼困難,在純JavaScript中。 –

回答

4

您可以創建一個功能,每3秒更換一次圖像。然後,將鼠標懸停在圖像上時,調用該函數並啓動一個計時器。當鼠標離開圖像時,清除計時器。

var img = document.getElementById("me").getElementsByTagName("img")[0]; 
var images = ["Me.JPG", "new image path", "..."]; 
var i = 1; 
var timer; 

function change() { 
    img.src = images[i]; 
    if (++i >= images.length) { 
     i = 0; 
    } 
    timer = setTimeout(change, 3000); 
} 

img.onmouseover = function() { 
    timer = setTimeout(change, 3000); 
}; 

img.onmouseout = function() { 
    img.src = images[0]; 
    clearTimeout(timer); 
}; 
+0

演示:http://jsfiddle.net/QW3Ue/ –

0

喜歡的東西,只是給imgmyImgid(或任何你想要的):

var img_arr = ["img1.png", "img2.png", "img3.png"], 
    img_index = 0, 
    interval_id = 0, 
    div = document.getElementById("me"), 
    img = document.getElementById("myImg"); 

div.onmouseover = function() { 
    interval_id = window.setInterval(function() { 
     if (img_index === (img_arr.length - 1)) { 
      img_index = 0; 
     } 
     img.src = img_arr[img_index++]; 
    }, 3000); 
}; 

div.onmouseout = function() { 
    window.clearInterval(interval_id); 
}; 
3

這裏有一個快速的解決方案中的鏈接的jsfiddle:http://jsfiddle.net/jJBEu/2/

var img = document.getElementById('me').getElementsByTagName('img')[0], 
    index = 0, 
    sources = [ 
     'http://icons.iconarchive.com/icons/iconka/social-treat/128/yumtube-icon.png', 
     'http://icons.iconarchive.com/icons/iconka/social-treat/128/sweeter-icon.png', 
     'http://icons.iconarchive.com/icons/iconka/social-treat/128/facebow-icon.png' 
    ], 
    timer; 

img.addEventListener('mouseover', swapImages, false); 
img.addEventListener('mouseout', function(){ 
    clearTimeout(timer); 
    img.src = sources[0]; 
}, false); 

function swapImages(event, setindex){ 
    index = index == (sources.length - 1) ? 0 : index + 1; 
    timer = setTimeout(function(){ 
     img.src = sources[index]; 
     swapImages(); 
    }, 3000); 
} 

編輯修復一個愚蠢的錯誤,我在檢查數組索引長度不減1。

+0

它不會切換回原來的'onmouseout'。 –

+0

謝謝Jared。一個真正的業餘錯誤。 –

+0

它發生了,不要冒汗。如果你修復它,我會upvote這個答案。 ':)' –

1

HTML:

<img src="Me.JPG" alt="Me" width="450" height="450" alt="picture" align="right" 
onmouseover="animate(this)" onmouseout="stopanimation()" /> 

的javascript:

/* globals */ 
var animTimer = null; 
var animImg = null; 
var images = [new Image(), new Image(), new Image(), new Image(), new Image(),new Image()]; 
var imgIndex = 0;  

/* pre-load images in browser cash */ 
images[0].src = "Me.JPG"; 
images[1].src = "anim1.gif"; 
images[2].src = "anim2.gif"; 
images[3].src = "anim3.gif"; 
images[4].src = "anim4.gif"; 
images[5].src = "anim5.gif"; 

/* animation */ 
function animate(img){ 
    if (typeof img != 'undefined'){animImg = img;} 

    imgIndex += 1; 
    if (imgIndex>images.length-1){imgIndex=1;} 

    animImg.src=images[imgIndex].src; 
    animTimer = setTimeout(animate, 3000); 
} 

function stopanimation(){ 
    clearInterval(animTimer); 
    animImg.src = images[0].src; 
}