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

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

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

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

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

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

yhived7q

yhived7q1#

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

  1. const scrollToPriceBlock = () => {
  2. const EL_priceBlock = document.querySelector(".price-block");
  3. window.scrollTo({
  4. top: EL_priceBlock.offsetTop,
  5. behavior: 'smooth'
  6. });
  7. };
  8. scrollToPriceBlock();
  1. <p style="height: 200vh;">Scroll...</p>
  2. <div class="price-block">PRICE BLOCK</div>
  3. <p style="height: 200vh;">Scroll...</p>

相关问题