【问题标题】:Goto Next Anchor Button转到下一个锚按钮
【发布时间】:2011-08-13 09:41:15
【问题描述】:

我在顶部设置了一些锚点和一个小菜单。当我点击一个菜单项时,它会滚动到那个锚点。

我想做的是在菜单上有一个下一个箭头来确定我页面上的下一个锚点并在点击时滚动到它。

我的锚点是#header、#box1 - #box5

如果可能的话,我想用 JavaScript 来做。 这是我的页面
My Page

【问题讨论】:

    标签: javascript html button next


    【解决方案1】:

    有一个名为 document.anchors 的 HTML 集合。要转到下一个锚点,请从 URL 中获取当前锚点名称并在 document.anchors 中查找它。如果你找到它,下一个将是下一个索引。如果您位于最后一个索引处,请将锚点设置为第一个。否则,如果没有匹配,则将其设置为第一个。

    这允许您使用任何方案来命名锚点,它们将按照它们在 DOM 中出现的顺序被访问。

    例如

    <head>
    <!-- Hide script-dependent content -->
    <style type="text/css">
    .requiresScript-block, .requiresScript-inLine {
      display: none; 
    }
    div.spacer {
      height: 500px;
      border: 1px solid blue;
    }
    </style>
    <script type="text/javascript">
    
    function goToNextAnchor() {
      var anchors = document.anchors;
      var loc = window.location.href.replace(/#.*/,'');
      var nextAnchorName;
    
      // Get name of the current anchor from the hash
      // if there is one
      var anchorName = window.location.hash.replace(/#/,'');
    
      // If there is an anchor name...
      if (anchorName) {
    
        // Find current element in anchor list, then
        // get next anchor name, or if at last anchor, set to first
        for (var i=0, iLen=anchors.length; i<iLen; i++) {
          if (anchors[i].name == anchorName) {
            nextAnchorName = anchors[++i % iLen].name;
            break;
          }
        }
      }
    
      // If there was no anchorName or no match,
      // set nextAnchorName to first anchor name
      if (!nextAnchorName) {
        nextAnchorName = anchors[0].name;
      }
    
      // Go to new URL
      window.location.href = loc + '#' + nextAnchorName;
    }
    
    // Display script-dependent content if javascript available
    document.write(
      '\u003Cstyle type="text/css"\u003e' +
      '.requiresScript-block {display: block;}' +
      '.requiresScript-inLine {display: inline;}' +
      '\u003C/style\u003e'
    );
    </script>
    </head>
    <body>
      <ol>
        <li><a href="#header">Go to header</a>
        <li><a href="#box1">Go to box 1</a>
        <li><a href="#box2">Go to box 2</a>
        <li><a href="#box3">Go to box 3</a>
        <li><a href="#box4">Go to box 4</a>
        <li><a href="#box5">Go to box 5</a>
      </ol>
    
      <!-- Only shown if javascript available -->
      <button class="requiresScript-inLine" onclick="goToNextAnchor()">Next</button>
    
      <a name="header"></a><h1>Header</h1>
      <div class="spacer">content</div>
      <p><a name="box1"></a><p>Box 1</p>
      <div class="spacer">content</div>
      <p><a name="box2"></a><p>Box 2</p>
      <div class="spacer">content</div>
      <p><a name="box3"></a><p>Box 3 </p>
      <div class="spacer">content</div>
      <p><a name="box4"></a><p>Box 4</p>
      <div class="spacer">content</div>
      <p><a name="box5"></a><p>Box 5</p>
    </body>
    

    【讨论】:

      【解决方案2】:

      在你的 HTML 中使用 onClick 的东西:

      <a href="#" onclick="goToNext();return false;">===></a>
      

      ...然后是 JavaScript:

      var max = 5;
      
      function goToNext() {
          var hash = String(document.location.hash);
          if (hash && hash.indexOf(/box/)) {
              var newh = Number(hash.replace("#box",""));
              (newh > max-1) ? newh = 0 : void(null);
              document.location.hash = "#box" + String(newh+1); 
          } else {
              document.location.hash = "box1";        
          }
      }
      

      max更改为您想要的最高数字(对于box1box2等...)。不确定这是否会保留动画,但您可以查看一个示例 here。看地址栏就行了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-12
        • 1970-01-01
        • 2012-01-14
        • 2018-01-25
        • 2013-02-01
        • 2021-10-08
        相关资源
        最近更新 更多