简体   繁体   English

动画以仅在第二页加载时定位

[英]Animate to target only on second page load

I have created a WordPress site that displays a list of posts. 我创建了一个显示帖子列表的WordPress网站。 I created a div with the id of archive-tracks, then I use some jQuery that scrolls the page down to that div when the page is loaded. 我创建了一个带有archive-tracks ID的div,然后使用一些jQuery在页面加载时将页面向下滚动到该div。

I want this functionality to only happen when one of my pagination links is clicked, at the moment it happens as soon as someone enters the page on the site. 我希望仅当单击我的分页链接之一时才发生此功能,而当有人进入站点页面时,此功能就发生。

Here is my JS 这是我的JS

jQuery(function(){
  var target = "archive-tracks";
  jQuery('html, body').animate({
    scrollTop: jQuery('#'+target).offset().top
  }, 1500,'swing');
  return false;
});

Is there a way of stopping this animation if the user comes from an external page but then allowing it when they click one of my pagination links to go through my posts? 如果用户来自外部页面,但是当用户单击我的分页链接之一浏览我的帖子时,是否可以停止该动画?

Here is my pagination code. 这是我的分页代码。

function dem_pagination( $pages = '', $range = 2 ) {
  $showItems = ($range * 2)+1;
  global $paged;

  if (empty($paged)) $paged = 1;

  if ($pages == '') {
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if (!$pages) {
      $pages = 1;
    }
  }

  if ( $pages != 1 ) {
    echo "<div class='pagination'>";

    if ($paged > 2 && $paged > $range+1 && $showItems < $pages) {
      echo "<a href='".get_pagenum_link(1). '#latest' . "'>&laquo;</a>";
    }

    if ($paged > 1 && $showitems < $pages) {
      echo "<a href='".get_pagenum_link($paged - 1). '#latest' ."'>&lsaquo;</a>";
    }

    for ($i=1; $i <= $pages; $i++) {
      if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
        echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i). '#latest' ."' class='inactive' >".$i."</a>";
      }
    }

    if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1). '#latest' . "'>&rsaquo;</a>";
    if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages). '#latest' ."'>&raquo;</a>";

    echo "</div>\n";
  }
}

Not sure but you can use this: 不确定,但是您可以使用以下方法:

jQuery(function(){
    if(window.location.hash){ //<------if hash values are location then only executes
       var target = "archive-tracks";
       jQuery('html, body').animate({
           scrollTop: jQuery('#'+target).offset().top
       }, 1500,'swing');
       return false;
    }
});

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

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