1. 程式人生 > 其它 >JS實現側邊欄點選回到指定位置的功能

JS實現側邊欄點選回到指定位置的功能

技術標籤:javascriptweb前端javascriptjs

  1. DEMO
    在這裡插入圖片描述
  2. 程式碼寫的不太好,大佬可以幫忙修改修改哈
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</
title
>
</head> <style> html { scroll-behavior: smooth } * { margin: 0; padding: 0; box-sizing: border-box; } .item { position: relative; width: 100%; font-size: 30px; text-align: center; height: 100vh;
line-height: 100px; } .tab-item { cursor: pointer; line-height: 100px; } .red { background-color: red; } .blue { background-color: blue; } .black { background-color: black; color: white; } .yellow {
background-color: yellow; } .tab { position: fixed; width: 100px; height: 400px; background-color: white; top: 30%; right: 10px; display: flex; flex-direction: column; justify-content: space-around; text-align: center; } .show { background-color: red; } .backTop { cursor: pointer; position: fixed; right: 30px; bottom: 30px; display: none; background-color: #ccc; width: 100px; line-height: 40px; color: white; animation: show .1s ease; text-align: center; z-index: 10; } @keyframes show { from { transform: scale(0); opacity: 0; } to { transform: scale(1); opacity: 1; } }
</style> <body> <div class="backTop">回到頂部</div> <div id="list"> <div class="item red"> 樓區一 </div> <div class="item blue"> 樓區二 </div> <div class="item black"> 樓區三 </div> <div class="item yellow"> 樓區四 </div> </div> <div class="tab"> <div class="tab-item show">一層</div> <div class="tab-item">二層</div> <div class="tab-item">三層</div> <div class="tab-item">四層</div> </div> <script> var show = ['red', 'blue', 'black', 'yellow'] var tab_item = document.querySelectorAll('.tab-item') var item = document.querySelectorAll('.item') var flag = true // 禁止連續點選 var backTop = document.querySelectorAll('.backTop')[0] tab_item.forEach((item, index) => { item.addEventListener('click', scrollColor, false) //點選改變介面顏色方法 item.index = index }); function scrollColor() { if (flag) { flag = false console.log(flag); var way = item[this.index].offsetTop// 根據自定義屬性index 獲取側邊欄對應的大色塊距離瀏覽器上面的距離 var timer = setInterval(() => { scrollTo(0, way) // html css設定 scroll-behavior: smooth 或者封裝動畫類 參考文章 https://blog.csdn.net/weixin_43625763/article/details/103478291 for (var i = 0; i < item.length; i++) { if (way == innerHeight * i) { // bug 當這兩個值相等 清除定時器 此處需優化 似乎還存在點選 但效果還好 clearInterval(timer) flag = true } } }, 100); } } document.onscroll = function () { for (var i = 0; i < tab_item.length; i++) { if (scrollY > innerHeight * i - 20) { // -20設定的稍微提前點 因為最後一次無法觸底 for (var j = 0; j < tab_item.length; j++) { // 排他思想 tab_item[j].style.backgroundColor = 'white' tab_item[j].style.color = 'black' } tab_item[i].style.backgroundColor = show[i] if (i === 2) { // 單獨處理黑色塊 tab_item[i].style.color = 'white' } } } if (scrollY > innerHeight) { // 按鈕在滑動超過第二個色塊出現 backTop.style.display = 'block' } else { backTop.style.display = 'none' } } backTop.onclick = function () { if (flag) { // 禁止多重點選 flag = false var timetop = setInterval(() => { // 設定移動到頂部 scrollTo(0, 0) if (scrollY == 0) { clearInterval(timetop) flag = true } }, 300); } } </script> </body> <html>