簡體   English   中英

跳轉鏈接,如何跳轉到頁面上的下一個可用錨點?

[英]Jump links, how to jump to next available anchor on page?

我正在用wordpress編寫一個單頁網站,該網站使用跳轉鏈接轉到頁面上的特定錨點。 我這樣做是通過向wordpress菜單添加一個自定義的walker來將菜單鏈接更改為錨點。 我想創建一個跳轉到頁面上下一個錨點的按鈕,而不考慮錨點的名稱是什么。 是否有捷徑可尋?

到目前為止,這是我的代碼:

jQuery平滑滾動

$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
  var target = $(this.hash);
  target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
  if (target.length) {

    $('html,body').animate({
      scrollTop: target.offset().top
    }, 2000,'swing');
    return false;
  }
}

Walker in functions.php

class description_walker extends Walker_Nav_Menu{
  function start_el(&$output, $item, $depth, $args){
       global $wp_query;
       $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
       $class_names = $value = '';
       $classes = empty( $item->classes ) ? array() : (array) $item->classes;
       $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
       $class_names = ' class="'. esc_attr( $class_names ) . '"';
       $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
       $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
       $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
       $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
       if($item->object == 'page')
       {
            $varpost = get_post($item->object_id);
            if(is_home()){
              $attributes .= ' href="#' . $varpost->post_name . '"';
            }else{
              $attributes .= ' href="'.home_url().'/#' . $varpost->post_name . '"';
            }
       }
       else
            $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID );
        $item_output .= $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
 }

}

在wordpress菜單中呼叫沃克

    wp_nav_menu(array(
            'theme_location' => 'primary_navi',
            'echo' => true,
            'walker'=> new Description_Walker,
            'depth' => 4) );    

在Wordpress模板中創建錨點

<div id="<?php echo $post->post_name;?>">

如果您需要用某種方式編碼,例如,如果tab無法給您足夠的控制權,請嘗試類似的方法

純JavaScript

現場演示

var lastClicked=0-1,anchors=document.getElementsByTagName("a");

window.onload=function() {
  document.getElementById("jumpbut").onclick=function() {
    var hash = "";
    for (var i=lastClicked+1;i<anchors.length;i++) {
      if (anchors[i].hash) {
        hash=anchors[i].hash;
        lastClicked=i;
        break;
      }
    }
    if (hash) location.hash=hash;
  } 
}

jQuery的

現場演示

$(function() {
  var $anchors = $('a[href*=#]:not([href=#])'),
      $target = $anchors.first(),
      current=0;
  $("#jumpbut").on("click",function() {
    $('html,body').animate({
      scrollTop: $target.offset().top
    }, 2000,'swing');
    $target = $anchors.eq(current+1);
  });
});

嘗試按TAB鍵。 酷嘿?

您可以嘗試使用: tabindex

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM