php - 跳转链接,如何跳转到页面上的下一个可用 anchor ?

标签 php jquery wordpress anchor

我正在 WordPress 中编写一个单页网站,该网站使用跳转链接转到页面上的特定 anchor 。我通过向 WordPress 菜单添加自定义步行器来实现此目的,该自定义步行器将菜单链接更改为 anchor 。我想创建一个按钮,无论 anchor 的名称是什么,都可以跳转到页面上的下一个 anchor 。有没有简单的方法可以做到这一点?

这是迄今为止我的代码:

用于平滑滚动的 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;
  }
}

functions.php 中的 Walker

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 模板中创建 anchor

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

最佳答案

如果您需要编写代码来摆脱它,例如如果选项卡没有给您足够的控制权,请尝试类似的方法

纯 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);
  });
});

关于php - 跳转链接,如何跳转到页面上的下一个可用 anchor ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19726932/

相关文章:

php - WooCommerce:在结帐页面中默认设置国家/地区

mysql - 在 wordpress 中使用 get_user 进行排序

php - ExtJS 在 Ajax 返回 (json) 上加载数据并为 grid.panel 加载存储

php - Mysql存储过程的查询优化

php - 在 AJAX 调用中显示来自 PHP 的消息

php - 亚马逊 SES 错误

javascript - Fullcalendar 和 jQuery 日期选择器

JQuery 这个 + class.animate()

javascript - 第一次点击后事件处理程序解除绑定(bind)

css - 需要在手机和平​​板设备上显示不同的标识