我开发了以下代码,它只是每隔 x 秒将一幅图像换成另一幅图像。一切正常,在 Chrome 中没有返回任何错误。然而,在 IE 中(最新回到 IE8)它什么都不做,不返回任何错误,只停留在第一张图片上。
有什么建议吗?我记得在某处读到 setInterval 会导致 IE 出现问题。
考虑:
$(function () {
var fElement = $('.fadein');
fElement.find('img:gt(0)').hide();
setInterval(function () {
if (!fElement.data('paused')) {
fElement.find(':first-child')
.stop(true, true) // fixes le tabbed/hidden animation queue
.fadeOut()
.next('img')
.fadeIn()
.end()
.appendTo('.fadein');
} else {
console.log('waiting...');
}
}, 2000);
$('map').hover(
function () {
console.log('pausing');
fElement.data('paused', 1);
},
function () {
console.log('unpausing');
fElement.data('paused', 0);
}
);
});
if (!console && !console.log) {
console = {};
console.log = function () {};
}
和CSS:
.fadein {
display: block;
height: 49px;
width: 287px;
float: left;
}
.fadein img {
position:absolute;
}
.stripContainer {
display:block;
height: 49px;
}
和 HTML:
<div class="fadein">
<img src="img1.jpg" border="0" width="287" height="49" alt="" style="" id="level2Menu"/>
<img src="img2.jpg" border="0" width="287" height="49" alt="" style="" id="level2Menu"/>
</div>
<div class="stripContainer">
<img src="img3.jpg" usemap="#secondM" border="0" width="385" height="49" alt="" style="float:left;" id="level2Menu"/>
<img src="img4.jpg" usemap="#secondM" border="0" width="288" height="49" alt="" style="float:right;" id="level2Menu"/>
</div>
编辑:这个 fiddle 显示它在 Chrome 中按预期工作:http://jsfiddle.net/8f5uU/
最佳答案
嗯,setInterval 应该不会在早期版本的 IE 中引起任何问题。然而,jQuery 可能。如 jQuery Doc: 中所述
Edge: (Current - 1) and Current
Firefox: (Current - 1) and Current
Internet Explorer: 9+
它接着说:
If you need to support older browsers like Internet Explorer 6-8, Opera 12.1x or Safari 5.1+, use jQuery 1.12.
尝试使用旧版本的 jQuery,看看效果如何。
关于JavaScript 无法在 IE、.data 中运行?设置间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20249613/