简体   繁体   English

如何使用HTML和/或JS转到下一页锚点?

[英]How to go to next page anchor with HTML and/or JS?

是否可以使用HTML和/或Javascript自动计算出下一页锚点是什么并转到它?

Assuming you mean relative to a known anchor/item; 假设你的意思是相对于一个已知的锚/项; if you are using jQuery, they have a selector for "Next Sibling"; 如果你使用jQuery,他们有一个选择“Next Sibling”; find the documentation here: 在这里找到文档:

http://api.jquery.com/next-siblings-selector/ http://api.jquery.com/next-siblings-selector/

I am just running out; 我刚刚跑出去; otherwise I would build up the full selector for you, but if you don't have an answer later today, I will put it together. 否则我会为你建立完整的选择器,但如果你今天晚些时候没有答案,我会把它放在一起。

Edit 编辑

Actually, based on further reading, I am quessing you are looking for the next anchor relative to the current scroll position on the page... that is a little more work... but is doable... (again, I will try to get on later today to provide the JS). 实际上,在进一步阅读的基础上,我正在考虑你正在寻找相对于页面当前滚动位置的下一个锚...这是一个更多的工作...但是可行...(再次,我会尝试今天晚些时候继续提供JS)。

Edit 编辑

These are not 100% complete; 这些并非100%完整; but should be enough so you get the general idea. 但应该足够让你得到一般的想法。 If your page is static content, I would recommend going through the page and caching all offsets on document ready; 如果您的页面是静态内容,我建议您浏览该页面并缓存文档准备就绪的所有偏移量; determining an elements offset can be pretty slow (especially in older browsers). 确定元素偏移量可能非常慢(特别是在较旧的浏览器中)。

var _anchorOffets = {};

$(document).ready(function () {
  $("A[id]:not(A[href])").each(function (index, value) {
    var $anchor = $(value);
    _anchorOffets[$anchor.attr("id")] = $anchor.offset().top;
  });
});

function getNextAnchorId() {
  var $window = $(window);
  var minOffset = $window.scrollTop() + $window.height();

  for (var anchor in _anchorOffets) {
    if (_anchorOffets[anchor] >= minOffset) {
      return anchor;
    }
  }

  return null;
}

alternatively, if you page has dynamic content and the anchor locations are not fixed within the document, then you would be looking at something closer to 或者,如果您的页面具有动态内容并且锚点位置未在文档中修复,那么您将看到更接近的内容

function getNextAnchorId() {
  var result = null;

  $("A[id]:not(A[href])").each(function (index, value) {
    var $anchor = $(value);
    var $window = $(window);
    var minOffset = $window.scrollTop() + $window.height();

    if($anchor.offset().top >= minOffset) {
      result = $anchor.attr("id");
      return false;
    }
  });

  return result;
}

Depending on your page, you may need to expand on this idea. 根据您的页面,您可能需要扩展此想法。 Both of the above solutions assume a pretty basic document layout. 上述两种解决方案都采用了非常基本的文档布局。 If you have anything fancy going on with relative/absolute positing, or scrollable divs etc, you will need to beef this up (I can provide code if that is the case). 如果您对相对/绝对定位或可滚动的div等有任何想象,那么您需要加强它(如果是这种情况,我可以提供代码)。 For now though I am assuming that we are talking about a basic find the anchors on the page, and figure out which anchor would be next based on scroll position. 现在,虽然我假设我们正在讨论基本找到页面上的锚点,并根据滚动位置确定下一个锚点。 Also, I didn't extensively test, so some polish may also be required :D 此外,我没有进行广泛的测试,因此也可能需要进行一些抛光:D

Reading your comment to Calgary Coder, I believe - if I understand you correctly - you're looking to find out the anchor in the current URL. 读到你对Calgary Coder的评论,我相信 - 如果我理解正确的话 - 你正在寻找当前网址中的锚点。 You can do this with window.locaiton.hash . 你可以用window.locaiton.hash做到这一点。 Eg 例如

alert(window.location.hash);

Thanks for the answers everyone, but I managed to get this working by implementing http://marcgrabanski.com/articles/scrollto-next-article-button-jquery . 感谢大家的答案,但我设法通过实施http://marcgrabanski.com/articles/scrollto-next-article-button-jquery来实现这一目标。 This works by going to the next header, rather than the next anchor but I imagine if it's critical to use anchors it could be easily adapted to do so. 这是通过转到下一个标题,而不是下一个锚点,但我想如果使用锚点很关键,它可以很容易地适应这样做。

I think I understand what you're looking for. 我想我明白你在寻找什么。 If you know the id's you can use this... 如果你知道id,你可以使用这个......

function Next(){
    var lnkNext = document.getElementById("yourNextAnchorId");
    window.location.href = lnkNext.src;
}

if you don't know the id's maybe get the anchors by the innerText 如果你不知道id可能是通过innerText获得锚点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM