简体   繁体   English

jQuery Click锚标记可防止页面滚动到顶部

[英]jQuery Click anchor tag prevent page from scrolling to top

I'm doing page transitions with jQuery by fading out content and fading it back in when the page loads but my problem is when I click my link and call my click function and redirecting the page it loads at the the top of the page. 我正在通过jQuery进行页面转换,方法是淡出内容并在页面加载时将其淡入,但是我的问题是单击链接并调用click函数并将页面加载到页面顶部时重定向页面。 Is there a way I can prevent this behavior? 有没有办法可以防止这种行为? Here is my click function for my page transition, thanks in advance for any help! 这是我进行页面转换的点击功能,在此先感谢您的帮助!

LINK 链接

<a href="../categories/categories.php"></a>

jQuery jQuery的

$(".content").fadeIn(750);

$("a").click(function(event){
    event.preventDefault();
    linkLocation = this.href;
    $(".content").fadeOut(500, redirectPage);  
});

function redirectPage() {
    window.location = linkLocation;
}

Good solution : Use history api with hashbangs fallback 好的解决方案:结合使用历史API和hashbangs后备

Bad solution: as an easy hack you can capture the current scroll position with 错误的解决方案:作为一种简单的技巧,您可以使用以下命令捕获当前滚动位置

    $(".content").fadeIn(750);
    var offset = window.location.href.match(/offset=(\d+)/)
    if(offset){
       $(document).scrollTop(offset[1])
    }
    $("a").click(function(event){
        event.preventDefault();
        linkLocation = this.href + "?offset="+$(document).scrollTop();//pay 
//special attentions to this line will work only for a link without get parameters. 
        $(".content").fadeOut(500, redirectPage);  
    });

    function redirectPage() {
        window.location = linkLocation;
    }
event.preventDefault();
linkLocation = this.href;

Thanks to the Team, please kindly put this above two lines after the click function start That will help you to make the scroll off. 感谢团队,请在单击功能启动后将其放在两行以上,这将帮助您进行滚动。

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

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