修改主題裡的functions.php
add_action( 'wp_footer', function() { ?>
<script>
( function( $ ) {
'use strict';
$( document ).on( 'ready', function() {
$('a[href^="#"]').live('click',function(event){
event.preventDefault();
var target_offset = $(this.hash).offset() ? $(this.hash).offset().top : 0;
var customoffset = 110;
$('html, body').animate({scrollTop:target_offset - customoffset}, 500);
});
} );
} ( jQuery ) );
</script>
<?php } );
customoffset 為往上的位移,通常是上導覽列的高度。
.animate({scrollTop:target_offset - customoffset}, 500);
500大概是移動時間
錨點1(name只能用a標籤 ):
<a name="aaa">這裡是aaa</a>
錨點2(泛用):
<div id="aaa"></div>
超連結:
<a href="#aaa">點這裡跳到aaa</a>