メニュー

🏷️WordPressでよく使うアンカーリンク向けjQuery

ヘッダー(#header)の高さはあるものとする。
外部サイトへのアンカーリンクには注意(´・ω・`)

(() => {
'use strict';
	
	jQuery( function($) {
		const headerH = $( '#header' ).height();
		$( 'a[href*="#"]' ).on( 'click', function() {
			const speed = 700;
			const target = $( this.hash === '#' || '' ? 'html' : this.hash );
			const pos = !target[0] ? 0 : target.offset().top - headerH;
			$( 'html, body' ).animate( { scrollTop:pos }, speed, 'swing' );
			return false;
		});
	});
	
})()