返回顶部,姑且是这样,存在bug私信更改

60 阅读1分钟
//返回顶部
  const ftop = () => {
    const scrollToButton = document.querySelector('.ftop');
    const targetElement = document.querySelector('.seared');
    scrollToButton.addEventListener('click', function () {
      const targetElementRect = targetElement.getBoundingClientRect();
      window.scrollTo({
        top: targetElementRect.top + window.scrollY,
        left: targetElementRect.left + window.scrollX,
        behavior: 'smooth'
      });
    });
  }
 
return(
     <div className='seared'>
        <div>111</div>
        <div>111</div>
        <div>111</div>
         <div className="ftop"><h2><BackTop onClick={ftop} /></h2></div>
     </div>
)