JQuery下錨點的平滑跳轉
對於錨點的平滑跳轉,在一般的商業性質的網站上,權衡來說,要謹慎使用。
例如:讓頁面平滑滾動到一個id為box的元素處,則JQuery代碼只要一句話,關鍵位置 如下:
$(“html,body”).animate({scrollTop:$(“#box”).offset().top},1000)
其中animate為JQuery的自定義動畫方法,$(“#box”).offset().top表示id為box的JQuery對象距離頁面頂部的偏移值,1000表示平滑動畫執行的時間為1000毫秒,也就是1秒。
// 頁面內向導 $(".brief p").click(function (e) { $(this).addClass("active_p").siblings().removeClass("active_p"); var v_id = e.target.id; if (v_id === 'se_advantage') { $("html, body").animate({ scrollTop: $(".advantage").offset().top }, 1000) } else if (v_id === 'flow') { $("html, body").animate({ scrollTop: $(".procedure").offset().top }, 1000) } else { $("html, body").animate({ scrollTop: $(".objective").offset().top }, 1000) } });
var v_id = e.target.id; 是獲取被點擊元素的id