我有一个onepager网站,我使用scrollmagic加上所有必要的插件/库(和jQuery),用于动画,钉住,淡化过程等不同效果。由滚动位置触发。动画滚动脚本阻止从外部链接访问本地锚位置
我还将它用于动画滚动到页面上的定位点(从菜单和其他本地链接) - 请参阅以下脚本的相应部分。
的问题是,这个脚本抑制的“跳跃”直接到anchorpoint默认行为单击本地连接时,显然也当页面从外部通过直接链接或书签与锚访问附加到URL(如http://www.example.com/index.php#part3
)。尽管单击本地链接时需要此行为,但显然会阻止浏览器在其他位置链接锚点时显示锚点位置。
有没有什么办法可以让浏览器直接显示锚定位置,当点击上面例子中的链接时?
var sm_controller_1 = new ScrollMagic.Controller();
sm_controller_1.scrollTo(function(anchor_id) {
TweenMax.to(window, 2.0, {
scrollTo: {
y: anchor_id
autoKill: true
},
ease: Cubic.easeInOut
});
});
jQuery(document).on("click", "a[href^=#]", function(e) {
var id = jQuery(this).attr('href');
if(jQuery(id).length > 0) {
e.preventDefault();
sm_controller_1.scrollTo(id);
if (window.history && window.history.pushState) {
history.pushState("", document.title, id);
}
}
});
(由于问题在于从外部源调用原始URL,所以创建小提琴/ codepen没有任何意义)。
是的,这个工程!我只是使用了你发布的代码,而不是下面的建议,因为它已经按我想要的方式工作(即当页面加载URL中的锚点时直接跳到锚点)。非常感谢你! – Johannes