我正在使用 javascript 来旋转背景图像。问题是,每次图像更改时,页面都会跳到顶部。希望这是一个简单的修复!
这是我的代码:
<script type='text/javascript'>
$(window).load(function(){
var initialBg = $('#slider').css("background-image"); // added
var firstTime = true;
var arr = [initialBg, "url(/wp-content/uploads/2013/03/slider2-explore.png)", "url(/wp- content/uploads/2013/03/slider3-experience.png)"];
(function recurse(counter) {
var bgImage = arr[counter];
if (firstTime == false) {
$("#slider").fadeOut("slow", function(){
$('#slider').css('background-image', bgImage);
});
$("#slider").fadeIn("slow");
} else {
firstTime = false;
}
delete arr[counter];
arr.push(bgImage);
setTimeout(function() {
recurse(counter + 1);
}, 4500);
})(0);
});
</script>