我创建了一个显示帖子列表的 WordPress 网站。我创建了一个带有 archive-tracks id 的 div,然后使用一些 jQuery 在加载页面时将页面向下滚动到该 div。
我希望此功能仅在单击我的分页链接之一时才会发生,目前,只要有人进入网站上的页面,它就会发生。
这是我的 JS
jQuery(function(){
var target = "archive-tracks";
jQuery('html, body').animate({
scrollTop: jQuery('#'+target).offset().top
}, 1500,'swing');
return false;
});
如果用户来自外部页面,但当他们单击我的分页链接之一浏览我的帖子时允许它,是否有办法停止此动画?
这是我的分页代码。
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' . "'>«</a>";
}
if ($paged > 1 && $showitems < $pages) {
echo "<a href='".get_pagenum_link($paged - 1). '#latest' ."'>‹</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' . "'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages). '#latest' ."'>»</a>";
echo "</div>\n";
}
}
最佳答案
不确定,但你可以使用这个:
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;
}
});
关于javascript - 仅在第二页加载时动画定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33368624/