我正在使用 jQuery 进行页面转换,方法是淡出内容并在页面加载时将其淡入,但我的问题是当我单击链接并调用单击函数并重定向它在顶部加载的页面时页。有什么方法可以阻止这种行为吗?这是我的页面转换的点击功能,提前感谢您的帮助!
链接
<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;
}
关于javascript - jQuery Click anchor 标记防止页面滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16536375/