想改善這個問題?通過編輯這篇文章,更新問題,使其只關注一個問題。
嘿,你可以這樣試試
<button class="sticky top-0" on:click={scrollUp}>Scroll Up</button>
<!-- Your divs here -->
<button class="sticky bottom-0" on:click={scrollDown}>Scroll Down</button>
嘗試運行以下JS
<script>
let scrollIndex = 0; // Initial scroll index
function scrollUp() {
if (scrollIndex > 0) {
scrollIndex--;
scrollToDiv();
}
}
function scrollDown() {
if (scrollIndex < 4) { // Adjust the value based on the number of divs
scrollIndex++;
scrollToDiv();
}
}
function scrollToDiv() {
const div = document.getElementById(`div${scrollIndex}`);
div.scrollIntoView({ behavior: 'smooth' });
}
</script>
我相信它會對你有所幫助