【发布时间】:2016-08-18 10:22:58
【问题描述】:
第一次在这里发帖!堆栈溢出告诉我包含来自 codepen.io 的代码,所以我做了,但我认为实际链接比从这里读取代码更有用。
我将这个http://codepen.io/haustraliaer/pen/leKny/ javascript 代码应用到我的网站,效果很好。我想滚动过去的页面来更新链接,这样当我向下滚动并单击下一步时,它不会回到上一个锚链接所在的位置。
我尝试使用滚动事件和 getBoundingClientRect 但似乎无法让它像那样工作。
对解决方案的任何帮助都会对我有很大帮助。
这是我主页的链接http://quki.kapsi.fi/wasd
$('.js-scroll-to').click(function(e) {
target = $($(this).attr('href'));
if (target.offset()) {
$('html, body').animate({
scrollTop: target.offset().top + 'px'
}, 600);
}
e.preventDefault();
});
$('.js-next').click(function(e) {
var selected = $(".js-list-item.js-current-panel");
var anchors = $(".js-list-item");
var pos = anchors.index(selected);
var next = anchors.get(pos + 1);
var prev = anchors.get(pos - 1);
target = $(next);
$(selected).removeClass("js-current-panel");
$(next).addClass("js-current-panel");
if (target.offset()) {
$('html, body').animate({
scrollTop: target.offset().top + 'px'
}, 600);
}
e.preventDefault();
});
$('.js-prev').click(function(e) {
var selected = $(".js-list-item.js-current-panel");
var anchors = $(".js-list-item");
var pos = anchors.index(selected);
var next = anchors.get(pos + 1);
var prev = anchors.get(pos - 1);
target = $(prev);
$(selected).removeClass("js-current-panel");
$(prev).addClass("js-current-panel");
if (target.offset()) {
$('html, body').animate({
scrollTop: target.offset().top + 'px'
}, 600);
}
e.preventDefault();
});
【问题讨论】:
-
我认为您正在寻找 fullPage.js
标签: javascript jquery css