From dfba411d61fd482e13b98b1803514890471a3e1d Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Thu, 24 Dec 2020 22:51:08 +0800 Subject: [PATCH] Jump to the anchor position smoothly --- assets/css/_addon/main.scss | 3 ++- assets/js/_commons/back-to-top.js | 9 +++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/assets/css/_addon/main.scss b/assets/css/_addon/main.scss index a30722a..7fa3b5f 100644 --- a/assets/css/_addon/main.scss +++ b/assets/css/_addon/main.scss @@ -46,8 +46,9 @@ html[mode=dark] { } } -html, body { +:root { font-size: 16px; + scroll-behavior: smooth; } body { diff --git a/assets/js/_commons/back-to-top.js b/assets/js/_commons/back-to-top.js index 21a91da..377e35f 100644 --- a/assets/js/_commons/back-to-top.js +++ b/assets/js/_commons/back-to-top.js @@ -1,9 +1,8 @@ /* Reference: https://bootsnipp.com/snippets/featured/link-to-top-page */ - $(function() { - $(window).scroll(function() { + $(window).scroll(() => { if ($(this).scrollTop() > 50 && $("#sidebar-trigger").css("display") === "none") { $("#back-to-top").fadeIn(); @@ -12,10 +11,8 @@ $(function() { } }); - $("#back-to-top").click(function() { - $("body,html").animate({ - scrollTop: 0 - }, 800); + $("#back-to-top").click(() => { + $("body,html").scrollTop(0); return false; }); });