我一直在搜索堆栈溢出,但似乎找不到解决方案。基本上,我是想让一个平滑的滚动效果在多个页面上工作。例如,平滑滚动效果是在网站主页上-首页关于联系商店是导航链接,例如,平滑滚动关于&联系,这驻留在主页上。当我在商店页面上点击about & contact链接时,什么也没有发生!
这个效果在首页上效果很好,有没有办法调整下面的代码,以便当用户在商店页面上单击“关于”或“联系”时,它将返回到“主页”页面,然后滚动到适当的链接?提前感谢您的帮助。
var jump=function(e)
{
if (e){
e.preventDefault();
var target = $(this).attr("href");
}else{
var target = location.hash;
}
$('html,body').animate(
{
scrollTop: $(target).offset().top
},2000,function()
{
location.hash = target;
});
}
$('html, body').hide();
$(document).ready(function()
{
$('a[href^=#]').bind("click", jump);
if (location.hash){
setTimeout(function(){
$('html, body').scrollTop(0).show();
jump();
}, 0);
}else{
$('html, body').show();
}
});发布于 2016-05-21 22:36:05
我想像这样的东西可能行得通:
//This is the function to scroll to your anchor
function scrollToElement(ele) {
$(window).scrollTop(ele.offset().top).scrollLeft(ele.offset().left);
}
//This is to get the anchor
if(window.location.hash) {
var elements = document.getElementsByTagName('a');
for (var i = 0; i < elements.length; i++) {
if(elements[i].href == window.location.hash){
scrollToElement(window.location.hash);
}
}
} 你可能需要做一些安排,我还没有测试过,但这是一般的想法。
https://stackoverflow.com/questions/37363015
复制相似问题