javascript - 时间和点击触发图像变化

标签 javascript html image

我想要一张5秒后没有被点击的图片变成另一张图片,点击后又恢复到原来的图片。

所以

图像>(5秒后用户未单击图像)>提示用户单击的图像>(用户单击)>图像恢复到原始状态。

有什么想法吗?

最佳答案

当然,这不是问题。您可以使用以下代码来完成:

HTML

<img id="clickable" src="http://fullahead.org/gallery/toronto-2010/image/thumb/DSC_3356.jpg" style="cursor: pointer" title="Clicky!"/>

Javascript

// Get a reference to the image and save its original source
var img = document.getElementById('clickable');
var origSrc = img.src;

// Create the timeout to change the source (2 seconds in code below)
var timeout = setTimeout(function(){
    img.src = 'http://fullahead.org/gallery/ilse-de-grand-calumet-2010/image/thumb/DSC_3163.jpg';   
}, 2000);

// Register the click event handler for the image and clear the timeout
img.onclick = function(){
    clearTimeout(timeout);
    this.src = origSrc;    
}

可以看到in action here .

关于javascript - 时间和点击触发图像变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3651476/

相关文章:

javascript - 在类型上输入自动对焦

html - 在 flexbox 元素之间添加空间

javascript - VueJS - 从递归树中获取输入值

php - 在 Laravel 注册中上传图片

javascript - 根据值加载页面时显示 div

javascript - 有没有办法在引导 TreeView 中更改节点文本?

javascript - 使用Javascript限制用户上传与格式不匹配的图像名称

Android:在 Button 或 ImageButton 上组合文本和图像

java - 用于过滤/排序大量数据的插件或模块?

javascript - 从 Chrome 打包应用程序保存文件