/ 代码 / 53浏览

键盘Shift+←→跳转文章分页

熟悉js的博主都知道可以利用js代码实现按键盘←→键跳转上一篇或下一篇文章。那么思路广一些,可否修改代码实现键盘Shift+←→跳转文章分页呢?答案是肯定的——感谢DeepSeek,在我两步思路启发下给出了原始代码,有点小bug,稍作修改放在single.php就可以用了。

<!--键盘左右文章分页 start-->
<?php wp_reset_query(); if ( is_single() ) { ?>
<script type="text/javascript">
	document.onkeydown = function(e) {
		e = e || event;
		var keycode = e.which || e.keyCode;
		var shiftPressed = e.shiftKey;
		var x = document.getElementById('comment');        
		if (!shiftPressed) return;        
		<?php
		global $page, $numpages;
		if ( $numpages > 1 ) {
		if ( $page > 1 ) {
			$prev_link = get_permalink() . '/' . ( $page - 1 == 1 ? '' : $page - 1 );
			}
		if ( $page < $numpages ) {
			$next_link = get_permalink() . '/' . ( $page + 1 );
			}
		}
		?>        
		// Shift + 左箭头(上一页)
		if ( keycode == 37 && x != document.activeElement && <?php echo $page > 1 ? 'true' : 'false'; ?> ) {
			location.href = '<?php echo isset( $prev_link ) ? $prev_link : '#'; ?>';
		}
		// Shift + 右箭头(下一页)
		else if ( keycode == 39 && x != document.activeElement && <?php echo $page < $numpages ? 'true' : 'false'; ?> ) {
			location.href = '<?php echo isset( $next_link ) ? $next_link : '#'; ?>';
		}
	};
</script>
<?php } ?>
<!--键盘左右文章分页 end-->

效果如下。

更新于

4

  1. 十六

    试了一下,能用,好用~

    Google Chrome 138 · Windows 10
    1. S

      @十六哈哈

      Safari 18 · iPad iOS 18.5
  2. Kevin's Space

    直接不分,长篇大论走起!

    Google Chrome 133 · Windows 10
    1. S

      @Kevin's Space直接拉垮了

      Safari 18 · iPad iOS 18.5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注