需求:在点击按钮时,将商品列表信息到达顶部
实例:
20230507_194641
//返回顶部
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>
)