From 17259c001830688e7cfa775a3abff9519183a8a3 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Thu, 9 May 2024 23:21:24 +0800 Subject: [PATCH] Better use of parseInt and Number --- source/js/bookmark.js | 2 +- source/js/utils.js | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/source/js/bookmark.js b/source/js/bookmark.js index 8e3ae6a..dea2a91 100644 --- a/source/js/bookmark.js +++ b/source/js/bookmark.js @@ -9,7 +9,7 @@ document.addEventListener('DOMContentLoaded', () => { const scrollToMark = () => { let top = localStorage.getItem('bookmark' + location.pathname); - top = parseInt(top, 10); + top = Number(top); // If the page opens with a specific hash, just jump out if (!isNaN(top) && location.hash === '') { // Auto scroll to the position diff --git a/source/js/utils.js b/source/js/utils.js index 4706f80..7209139 100644 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -60,7 +60,7 @@ NexT.utils = { }); }); } - const height = parseInt(window.getComputedStyle(element).height.replace('px', ''), 10); + const height = parseInt(window.getComputedStyle(element).height, 10); // Skip pre > .mermaid for folding but keep the copy button // Note that it only works before mermaid.js loaded (race condition) const needFold = CONFIG.fold.enable && (height > CONFIG.fold.height) && !element.querySelector('.mermaid'); @@ -221,9 +221,9 @@ NexT.utils = { // Comment system selection tab does not contain .active class. const activeTab = tabContent.querySelector('.active') || tabContent.firstElementChild; // Hight might be `auto`. - const prevHeight = parseInt(window.getComputedStyle(activeTab).height.replace('px', ''), 10) || 0; - const paddingTop = parseInt(window.getComputedStyle(activeTab).paddingTop.replace('px', ''), 10); - const marginBottom = parseInt(window.getComputedStyle(activeTab.firstElementChild).marginBottom.replace('px', ''), 10); + const prevHeight = parseInt(window.getComputedStyle(activeTab).height, 10) || 0; + const paddingTop = parseInt(window.getComputedStyle(activeTab).paddingTop, 10); + const marginBottom = parseInt(window.getComputedStyle(activeTab.firstElementChild).marginBottom, 10); tabContent.style.height = prevHeight + paddingTop + marginBottom + 'px'; // Add & Remove active class on `nav-tabs` & `tab-content`. [...nav.children].forEach(target => { @@ -240,7 +240,7 @@ NexT.utils = { })); // Get the height of `tab-pane` which is activated now. const hasScrollBar = document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight); - const currHeight = parseInt(window.getComputedStyle(tabContent.querySelector('.active')).height.replace('px', ''), 10); + const currHeight = parseInt(window.getComputedStyle(tabContent.querySelector('.active')).height, 10); // Reset the height of `tab-content` and see the animation. tabContent.style.height = currHeight + paddingTop + marginBottom + 'px'; // Change the height of `tab-content` may cause scrollbar show / disappear, which may result in the change of the `tab-pane`'s height @@ -248,7 +248,7 @@ NexT.utils = { if ((document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight)) !== hasScrollBar) { tabContent.style.transition = 'height 0.3s linear'; // After the animation, we need reset the height of `tab-content` again. - const currHeightAfterScrollBarChange = parseInt(window.getComputedStyle(tabContent.querySelector('.active')).height.replace('px', ''), 10); + const currHeightAfterScrollBarChange = parseInt(window.getComputedStyle(tabContent.querySelector('.active')).height, 10); tabContent.style.height = currHeightAfterScrollBarChange + paddingTop + marginBottom + 'px'; } // Remove all the inline styles, and let the height be adaptive again.