使用jquery更改图像src 5秒

时间:2018-03-08 12:26:50

标签: javascript jquery

我需要帮助解决如何使用另一个更改图像src 5秒钟,然后使用jquery恢复为默认值。

下面是我正在尝试使用的代码。

$('#indicator').attr("src", 'notification.png');

我只需要改变它5秒钟。

3 个答案:

答案 0 :(得分:0)

您只需要两个脚本

$('#indicator').attr("src", 'notification-new.png');

setTimeout(function(){ $('#indicator').attr("src", 'notification.png'); }, 5000);

第一个将属性更改为新的src,第二个将其更改为5秒后的原始状态。

答案 1 :(得分:0)

var oldSrc = $('#indicator').attr("src"); //save old image source
$('#indicator').attr("src", 'notification.png'); //set new image source
setTimeout(function(){ $('#indicator').attr("src", oldSrc); }, 5000); // after 5 seconds, reset old image source

答案 2 :(得分:0)

var changeSrc = function(obj){
    var elem = jQuery(obj.selector), img = elem.attr('src'); elem.attr('src', obj.tmpSrc);
    setTimeout(function(){elem.attr('src', img);}, 1000*obj.revertAfter);
};

changeSrc({
    selector: 'your-img-selector-here',
    tmpSrc: 'your-tmp-img-src-here',
    revertAfter: 5
});