无需jquery即可平滑滚动到元素

cu6pst1q  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(166)

关闭。这个问题需要更加关注。它目前不接受答案。
**想改进这个问题吗?**编辑这篇文章,更新这个问题,使它只关注一个问题。

两天前关门了。
改进这个问题
我在jquery中有这个表达式

$('html, body').animate({
  scrollTop: $('.price-block').offset().top
}, 800);

所以,我需要相同的,但在本地js

yhived7q

yhived7q1#

您可以将htmlelement.offsettop与window.scrollto结合使用

const scrollToPriceBlock = () => {
  const EL_priceBlock = document.querySelector(".price-block");
  window.scrollTo({
    top: EL_priceBlock.offsetTop,
    behavior: 'smooth'
  });
};

scrollToPriceBlock();
<p style="height: 200vh;">Scroll...</p>
<div class="price-block">PRICE BLOCK</div>
<p style="height: 200vh;">Scroll...</p>

相关问题