简体   繁体   English

防止jQuery浏览器滚动到点击顶部

[英]Prevent jquery browser scrolling to top on click

I have the following script for a thumbnail image viewer. 我有以下用于缩略图查看器的脚本。 I've named each thumbnail image and large parent image ending in thumb and large, respectively. 我已经分别命名了每个缩略图和大父图像(分别以thumb和large结尾)。 The script replaces the large image with whatever thumbnail image is clicked on by changing the filepath. 该脚本通过更改文件路径将大图像替换为单击的缩略图。

<script>
$('.thumbs').delegate('img','click', function(){
    $('.large').attr('src',$(this).attr('src').replace('thumb','large'));
    $('.large').hide().fadeIn(500);
});
</script>

Each time a thumbnail is clicked, the page scrolls back to the top. 每次单击缩略图,页面都会滚动回到顶部。 I've tried to prevent this with 我试图防止这种情况

return false;

It works, however, then large image won't update. 它有效,但是,大图像将不会更新。 Is there another way to prevent the page from scrolling to the top? 还有另一种方法可以防止页面滚动到顶部吗?

Thanks for your help. 谢谢你的帮助。

I doesn't seem to be doing that in IE9. 我似乎没有在IE9中这样做。 Try preventing the default action : 尝试阻止默认操作:

<script> 
$('.thumbs').delegate('img','click', function(event){ 
    $('.large').attr('src',$(this).attr('src').replace('thumb','large')); 
    $('.large').hide().fadeIn(500); 
    event.preventDefault();
}); 
</script>

The page does not scroll to the top of page, it scroll to the top of large image because you change the image source. 该页面不会滚动到页面顶部,而是会滚动到大图像的顶部,因为您更改了图像来源。

You can try to disable and hide and fadeIn effects and see if the problem persists. 您可以尝试禁用,隐藏和淡入效果,然后查看问题是否仍然存在。

If the problem persists simply add the following code that the issue is resolved. 如果问题仍然存在,只需添加以下代码即可解决问题。

<script>
$('.thumbs').delegate('img','click', function(){
    var y = window.pageYOffset;
    $('.large').attr('src',$(this).attr('src').replace('thumb','large'));
    $('.large').hide().fadeIn(500);
    window.scrollTo(0, y)
});
</script>

Sorry any english mistake. 对不起,任何英语错误。 I'm using google translate. 我在用谷歌翻译。

Where exactly did you place the return false? 您将假货归到何处? If you did it at the end, it should have worked. 如果您在最后完成了该操作,则它应该可以正常工作。 Like this: 像这样:

<script>
$('.thumbs').delegate('img','click', function(){
    $('.large').attr('src',$(this).attr('src').replace('thumb','large'));
    $('.large').hide().fadeIn(500);
    return false;
});
</script>

You can also use e.preventDefault() to stop the default link action: 您还可以使用e.preventDefault()停止默认链接操作:

<script>
    $('.thumbs').delegate('img','click', function(e){
        $('.large').attr('src',$(this).attr('src').replace('thumb','large'));
        $('.large').hide().fadeIn(500);
        e.preventDefault();
        return false;
    });
</script>

This happens because when you replace large image src, for a moment it dissapears, page got smaller and scrolls are no needed so go up. 发生这种情况的原因是,当您替换大图像src时,它消失了片刻,页面变小了,不需要滚动,因此向上滚动。 Try zooming page and then you will see it doesnt happen(ctrl + mouse scroll up). 尝试缩放页面,然后您将看到它没有发生(Ctrl +鼠标向上滚动)。

Just put you large image with DIV with fixes size that does not change: 只需将DIV放大后的图像大小固定不变即可:

<div style="height: 490px; width: 490px;float: left;">
    <img style="display: block;" class="large" src="images/06_large.png">
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM