--- tags: jQuery --- ###### tags: `jQuery` # jQuery錨點 ```jQuery $(".fda_list a[href^='#']").click(function (e) { e.preventDefault(); var position = $($(this).attr("href")).offset().top - 155; $("body, html").animate({ scrollTop: position; }); }); ```  --- # 錨點加偏移量 ``` document.querySelectorAll('a.track').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); const targetId = this.getAttribute('href').substring(1); const target = document.getElementById(targetId); if (target) { const headerHeight = document.querySelector('header').offsetHeight; // 取得 header 的高度 const offsetTop = target.getBoundingClientRect().top + window.scrollY; // 計算目標元素的位置 window.scrollTo({ top: offsetTop - headerHeight, behavior: 'smooth' // 平滑滾動效果 }); } }); }); ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up