繁体   English   中英

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

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

我正在通过jQuery进行页面转换,方法是淡出内容并在页面加载时将其淡入,但是我的问题是单击链接并调用click函数并将页面加载到页面顶部时重定向页面。 有没有办法可以防止这种行为? 这是我进行页面转换的点击功能,在此先感谢您的帮助!

链接

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

jQuery的

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

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

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

好的解决方案:结合使用历史API和hashbangs后备

错误的解决方案:作为一种简单的技巧,您可以使用以下命令捕获当前滚动位置

    $(".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;

感谢团队,请在单击功能启动后将其放在两行以上,这将帮助您进行滚动。

暂无
暂无

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

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