2012-08-05 100 views
0

我创建了一个简单的jQuery图像转换查看器,并且每次加载新照片时,窗口自动滚动到刚加载的新图像的顶部。我试图阻止这一点,以便过渡不会中断用户在页面上的任何位置。jquery禁用自动窗口滚动

这是到目前为止我的代码:

$(document).ready(function() { 

var slideShow = function() 
{ 
current_img = $("#current_img"); // <img> element with src to be changed 
height_width = 'width="100%" height="100%"'; 
photos = new Array('images/home_s1.png','images/home_s2.png', 'images/home_s3.png'); 
setTimeout(function() { current_img.fadeOut('fast', function() { current_img.attr('src', photos[0]); current_img.fadeIn('slow'); }); }, 0); 
setTimeout(function() { current_img.fadeOut('fast', function() { current_img.attr('src', photos[1]); current_img.fadeIn('slow'); }); }, 5000); 
setTimeout(function() { current_img.fadeOut('fast', function() { current_img.attr('src', photos[2]); current_img.fadeIn('slow'); }); }, 10000); 
setTimeout(function() { slideShow(); }, 15000); 
} 

$("img").ready(slideShow); 

}); 

我试图操纵的preventDefault()函数的多种不同的方式,而且我自己也尝试加载图像(即加载到HTML的不同变化( )而不是img.attr('src')),但没有运气...任何有关实现此目的的最有效方式的建议?

+0

你有没有发现这个答案吗? http://stackoverflow.com/questions/1391833/how-to-prevent-window-from-scrolling-up-on-jquery-slidetoggle?rq=1 所有最好 – devanand 2012-08-05 09:21:43

回答

1

您似乎遇到的问题是默认实现.fadeIn().fadeOut()display属性设置为none。这会使元素的高度和宽度都为0,这会导致一种跳跃效果。

您应该使用类似.fadeTo()的东西,并且在切换图像时不要将不透明度一直设置为0。类似于0.01的值也足够,并且不会使jQuery将display属性更改为none

这里有您需要setTimeout回调里面做什么的例子:

setTimeout(function() { 
    current_img.fadeTo('fast', 0.01, function() { 
     current_img.attr('src', photos[0]); 
     current_img.fadeTo('slow', 1); 
    }); 
}); 
+0

工作非常好听!我之前没有使用fadeTo函数。谢谢! – 2012-08-05 09:42:25

+0

@deraad欢迎您:) - P.S.,如果问题解决了,您应该将此答案标记为已接受。 – 2012-08-05 09:51:40