如果我有以下 JS 代碼:
//Scroll Down button
$(window).scroll(function () {
if ($(this).scrollDown() > 100) {
$('.containerScroll').fadeIn('slow');
} else {
$('.containerScroll').fadeOut('slow');
}
});
$('.containerScroll').click(function () {
$('html, body').animate({
scrollTop: 0
}, 1500, 'easeInOutExpo');
return false;
});
這基本上是用于 Back-top-button 影片,如果用戶單擊該按鈕,則它會將用戶帶回平滑滾動的主頁。我想相反。有沒有辦法修改上面的JS代碼,當用戶點擊按鈕時,它會將用戶帶到他們想要的頁面?現在,這會一直滾動到頂部并僅將用戶帶到網站的主頁,但是我該如何制作才能將用戶帶到網站上的任何頁面?
這是我在 HTML 檔案中的內容:
<a href="#about" class="containerScroll">
<div class="first-scroll"></div>
<div class="second-scroll"></div>
</a>
基本上,我希望用戶進入頁面的 about 部分而不是主頁。關于如何完成這項任務的任何建議?
uj5u.com熱心網友回復:
您可以將 scrollTop 值設定為您想要的任何元素的偏移量。例如:
$('html, body').scrollTop($('#about').offset().top);
將about通過獲取其偏移量將 scrollTop 設定為具有 id 的元素的頂部。
或者您可以像在示例中一樣使用 animate:
$('html, body').animate({
'scrollTop': $('#about').offset().top
}, 1500);
小提琴
小提琴平滑滾動
uj5u.com熱心網友回復:
到達頂點
window.scrollTo({
top: 0,
behavior: 'smooth'
});
元素
const element = getElementById("someElement");
//you can do it by jquery. no matter
element.scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});
MDM:scrollIntoView
MDM:滾動到
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/400616.html
標籤:javascript html 查询 css
下一篇:有機會使用此代碼進行回圈嗎?
