关闭。这个问题需要更加关注。它目前不接受答案。**想改进这个问题吗?**编辑这篇文章,更新这个问题,使它只关注一个问题。
两天前关门了。改进这个问题我在jquery中有这个表达式
$('html, body').animate({ scrollTop: $('.price-block').offset().top}, 800);
$('html, body').animate({
scrollTop: $('.price-block').offset().top
}, 800);
所以,我需要相同的,但在本地js
yhived7q1#
您可以将htmlelement.offsettop与window.scrollto结合使用
const scrollToPriceBlock = () => { const EL_priceBlock = document.querySelector(".price-block"); window.scrollTo({ top: EL_priceBlock.offsetTop, behavior: 'smooth' });};scrollToPriceBlock();
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>
<p style="height: 200vh;">Scroll...</p>
<div class="price-block">PRICE BLOCK</div>
1条答案
按热度按时间yhived7q1#
您可以将htmlelement.offsettop与window.scrollto结合使用