【问题标题】:Next/previous anchor link, how to update the next/previous links when the user scrolls下一个/上一个锚链接,当用户滚动时如何更新下一个/上一个链接
【发布时间】: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


【解决方案1】:
$('.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) {
  $('.js-prev').show();
  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);
  if (pos <= 4) {
    target = $(next);
    $(selected).removeClass("js-current-panel");
    $(next).addClass("js-current-panel");
    if (target.offset()) {
      $('html, body').animate({
        scrollTop: target.offset().top + 'px'
      }, 600);
    }
    if (pos == 4)
      $('.js-next').hide();
  }

  e.preventDefault();
});

$('.js-prev').click(function(e) {
  $('.js-next').show();
  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);
  console.log(pos)
  if (pos > 0) {
    target = $(prev);

    $(selected).removeClass("js-current-panel");
    $(prev).addClass("js-current-panel");

    if (target.offset()) {
      $('html, body').animate({
        scrollTop: target.offset().top + 'px'
      }, 600);
    }
  }
  if (pos == 1) {
    $('.js-prev').hide();
  }
  e.preventDefault();
});

下面是工作示例

http://codepen.io/krishnareddy668/pen/MyqwJz

【讨论】:

  • 我现在认为我在描述我想要的东西方面做得不好。我想要的是:1.当前系统按预期工作 2.鼠标滚动过去的锚点将当前位置更新到 jquery 脚本 3.当我决定向下滚动过去的文章然后突然想用可点击按钮移动时,它应该再往下走——不是往上走,我希望我能更好地解释一些东西。你的工作是有用的,所以没有工作是徒劳的!非常感谢!
  • 我认为您正在寻找 fullPage.js
  • 我之前一直在使用它,我认为这将是解决我问题的简单方法!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 2013-08-26
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多