我在网站上有一个使用ScrollMagic的 anchor 链接滚动功能,我试图将scrollTo目标沿y轴偏移100px,我对jquery很陌生,不知道在哪里放置这种说明:
'scrollTop': $target.offset().top - 100
这是我的工作代码(来自:https://github.com/janpaepke/ScrollMagic/wiki/Tutorial-:-Anchor-Navigation):
$(document).ready(function() {
// Init controller
var controller = new ScrollMagic.Controller();
// Change behavior of controller
// to animate scroll instead of jump
controller.scrollTo(function(target) {
TweenMax.to(window, 2, {
scrollTo : {
y : target, // scroll position of the target along y axis
autoKill : true, // allows user to kill scroll action smoothly
},
ease : Cubic.easeInOut
});
});
// Bind scroll to anchor links
$(document).on("click", "a[href^=#]", function(e) {
var id = $(this).attr("href");
if($(id).length > 0) {
e.preventDefault();
// trigger scroll
controller.scrollTo(id);
// If supported by the browser we can also update the URL
if (window.history && window.history.pushState) {
history.pushState("", document.title, id);
}
};
});
});
非常感谢任何指点。 谢谢
最佳答案
哦亲爱的我。
经过大量不必要的修补和修改,答案比我想象的要简单得多。
只需将 y : target
替换为 y : target-100
。
是的,我是个白痴。
关于jquery - 滚动魔法 : Add offset to anchor scrolling?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32353599/