javascript 上的鼠标悬停和超时

发布于 2024-12-19 04:07:15 字数 225 浏览 1 评论 0原文

我的html代码中有这个id:

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

当鼠标悬停在图片上时,如何每3秒更改一次图片,

并在鼠标移出后返回到原始图片?

I have this id in my html code:

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

how can i change the picture every 3 seconds once the mouse is over the picture,

and to go back to the original picture once the mouse is out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抱猫软卧 2024-12-26 04:07:15

您可以创建一个每 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 );
};

You can create a function that will change the image every 3 seconds. Then, when you mouse over the image, call the function and start a timer. When the mouse leaves the image, clear the timer.

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 );
};
请爱~陌生人 2024-12-26 04:07:15

以下是 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 的情况下检查数组索引与长度。

Here's a link to a quick solution on 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);
}

Edited to fix a dumb mistake where I was checking array index against length without subtracting 1.

夜空下最亮的亮点 2024-12-26 04:07:15

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; 
}

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; 
}
与之呼应 2024-12-26 04:07:15

就像,只需给 img 一个 myImgid (或任何你想要的):

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);
};

Something like, just give the img an id of myImg (or whatever you want):

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);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文