1document.addEventListener('DOMContentLoaded', function () { 2 const backBtn = document.querySelector('.goTop'); 3 isShow(); 4 // 夿æ¯å¦æ¾ç¤ºè¿åé¡¶é¨æé® 5 window.addEventListener('scroll', function () { 6 isShow(); 7 }); 8 function isShow() { 9 if (window.scrollY > 100) { 10 backBtn.style.display = 'block'; 11 backBtn.style.transform = 'translateY(0)'; 12 } else { 13 backBtn.style.transform = 'translateY(120px)'; 14 } 15 } 16 // è¿å顶鍿é®ç¹å»äºä»¶ 17 backBtn.addEventListener('click', function () { 18 scrollToTop(500); // æ»å¨å°é¡¶é¨ï¼æ¶é¿ä¸º500ms 19 }); 20 21 // æ»å¨å°é¡¶é¨å½æ° 22 function scrollToTop(duration) { 23 const startingY = window.scrollY; 24 const startTime = performance.now(); 25 26 function easeInOutQuad(time, start, change, duration) { 27 time /= duration / 2; 28 if (time < 1) return change / 2 * time * time + start; 29 time--; 30 return -change / 2 * (time * (time - 2) - 1) + start; 31 } 32 33 function animate(currentTime) { 34 const elapsedTime = currentTime - startTime; 35 window.scrollTo(0, easeInOutQuad(elapsedTime, startingY, -startingY, duration)); 36 if (elapsedTime < duration) { 37 requestAnimationFrame(animate); 38 } 39 } 40 41 requestAnimationFrame(animate); 42 } 43});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.