我做了一個固定的滾動到頂部按鈕,一旦你滾動 300 像素,它就會顯示出來。我想更進一步,讓按鈕只粘到某個部分,因為固定位置使它一直到頁腳。有沒有辦法讓它在頁腳之前停止?我在這里找到了一個很好的例子:https ://jumia.co.ke
HTML
<a href="#" class="scroll-to-top">Scroll Up</a>
CSS
.show-scroll-btn {
position: fixed;
transition: 350ms ease-in;
bottom: 20px;
z-index: 999;
}
JS
const scrollToTop = document.querySelector('.scroll-to-top');
// Add event listener to make button appear when scrolling down
const showBtn = window.addEventListener('scroll', () => {
if (window.scrollY > 300) {
scrollToTop.classList.add('show-scroll-btn');
} else {
scrollToTop.classList.remove('show-scroll-btn');
}
});
uj5u.com熱心網友回復:
您的方法很好,唯一不起作用的是您有條件window.scrollY > 300,這意味著程式僅show-scroll-btn在用戶向下滾動 300px 后才提供課程。需要更改的只是條件,window.scrollY < 300只要站點的 scrollY 低于 300,元素就具有類,并且當它超過 300 時,將其洗掉。
const scrollToTop = document.querySelector('.scroll-to-top');
// Add event listener to make button appear when scrolling down
const showBtn = window.addEventListener('scroll', () => {
if (window.scrollY < 300) {
scrollToTop.classList.add('show-scroll-btn');
} else {
scrollToTop.classList.remove('show-scroll-btn');
}
});
.show-scroll-btn {
position: fixed;
top: 0;
transition: 350ms ease-in;
z-index: 999;
<a href="#" class="scroll-to-top">Scroll Up</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
uj5u.com熱心網友回復:
您可以使用position: stickyCSS 屬性。
這是有關如何使用它的指南
uj5u.com熱心網友回復:
添加這個 CSS
.scroll-to-top {
position: sticky;
transition: 350ms ease-in;
top:calc(100% - 60px);
z-index: 999;
background:#fff;
padding:5px;
right:20px;
}
.scroll-to-top {
position: sticky;
transition: 350ms ease-in;
top:calc(100% - 60px);
z-index: 999;
background:#fff;
padding:5px;
right:20px;
}
<p>
<a href="#" class="scroll-to-top">Scroll Up</a>
I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :</p>
<div>
stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain section as a fixed position makes it go all the way to the footer. Is there a way I can make it stop just before the footer? A good example I found was here :I made a fixed scroll to top button, once you scroll 300px it shows itself. I wanted to go further and make the button only stick till a certain se further and make the button only stick till a certain section as a fixed found was here :
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/451924.html
標籤:javascript html css
下一篇:為什么背景顏色隱藏邊框?[復制]
