1 let bodyEl = document.body 2 let top = 0 3 4 function stopBodyScroll (isFixed) { 5 if (isFixed) { 6 top = window.scrollY 7 8 bodyEl.style.position = 'fixed' 9 bodyEl.style.top = -top + 'px' 10 } else { 11 bodyEl.style.position = '' 12 bodyEl.style.top = '' 13 14 window.scrollTo(0, top) // 回到原先的top 15 } 16 }