跳转链接,如何跳转到页面上的下一个可用锚点?

时间:2013-11-01 12:38:46

标签: php jquery wordpress anchor

我正在使用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菜单中调用walker

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

在Wordpress模板中创建锚点

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

2 个答案:

答案 0 :(得分:0)

如果您需要编写代码,例如如果tab没有给您足够的控制权,请尝试类似

的内容

普通JavaScript

Live Demo

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

Live Demo

$(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);
  });
});

答案 1 :(得分:0)

尝试按TAB键。酷嘿?

您可以尝试使用:tabindex

相关问题