1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { padding-bottom: 500px; }
.tab-box { width: 500px; height: auto; border-radius: 6px; overflow: hidden; margin: 10px; }
.tab-nav { display: flex; list-style: none; border-bottom: 1px solid #333; background: rgb(233, 233, 233); height: 40px; flex-direction: row; flex-wrap: nowrap; justify-content: center; align-items: center; position: relative; }
.tab-nav a { margin: 5px; padding: 0 10px; text-decoration: none; color: #000; height: 40px; line-height: 40px; }
.tab-nav a.active { color: rgb(23, 180, 215); }
.tab-bar { position: absolute; width: 40px; height: 3px; background: rgb(23, 180, 215); bottom: -1px; transition: all 0.3s; }
.item-box { background: rgb(220, 220, 220); }
.item { text-align: center; padding: 10px; display: none; }
.item.active { display: block; height: 100px; }
#nav { width: 100%; height: 50px; text-align: center; line-height: 50px; background: rgba(120, 182, 191, 0.5); font-size: 20px; position: fixed; top: -50px; transition: all 0.3s; z-index: 9999; }
.box { margin-top: 10px; width: 200px; height: 200px; background: rgb(144, 193, 230); margin-bottom: 200px; border-radius: 6px; }
.ele-nav { position: fixed; bottom: 20%; background: rgb(233, 233, 233); right: 10%; border: 1px solid #333; border-bottom: 0; border-radius: 6px; overflow: hidden; transition: all 0.3s; opacity: 0; z-index: -99; } .ele-item.active{ color: rgb(23, 180, 215); background: rgb(252, 200, 200); } .ele-item, .back-top { padding: 10px; border-bottom: 1px solid #333; user-select: none; transition: all 0.3s; } .ele-item:hover, .back-top:hover{ color: rgb(23, 180, 215); } </style> </head>
<body> <div id="nav">导航栏</div> <div class="tab-box"> <div class="tab-nav"> <a href="javascript:;" class="active">栏1</a> <a href="javascript:;">栏2</a> <a href="javascript:;">栏3</a> <a href="javascript:;">栏4</a> <a href="javascript:;">栏114514</a> <div class="tab-bar"></div> </div>
<div class="item-box"> <div class="item active">项目一</div> <div class="item">项目二</div> <div class="item">项目三</div> <div class="item">项目四</div> <div class="item">项目114514</div> </div> </div> <div class="ele-nav"> <div class="ele-item">去第一个</div> <div class="ele-item">去第二个</div> <div class="ele-item">去第三个</div> <div class="ele-item">去第四个</div> <div class="back-top">回到顶部</div> </div> <div class="box">第一个盒子,滚动到此(往上55像素)显示导航栏和电梯导航</div> <div class="box">第二个盒子</div> <div class="box">第三个盒子</div> <div class="box">第四个盒子</div> <script> const nav = document.querySelector('#nav'); const box = document.querySelectorAll('.box'); const ele_nav = document.querySelector('.ele-nav'); window.addEventListener('scroll', () => { let n = window.scrollY; let m = box[0].offsetTop; let tf = n >= m-55; nav.style.top = tf ? '0px' : '-50px'; ele_nav.style.opacity = tf ? '1' : '0'; ele_nav.style.zIndex = tf ? '99' : '-99'; }); const bar = document.querySelector('.tab-bar'); const tabNav = document.querySelector('.tab-nav'); const tab_a = document.querySelectorAll('.tab-nav a'); const item = document.querySelectorAll('.item-box .item'); tab_a.forEach((item, index) => { item.dataset.id = index; }); tabNav.addEventListener('click', (e) => { let tag = e.target.tagName; if (tag === 'A') { document.querySelector('.tab-nav a.active').classList.remove('active'); document.querySelector('.item.active').classList.remove('active'); e.target.classList.add('active'); item[e.target.dataset.id].classList.add('active'); bar.style.width = `${e.target.offsetWidth}px`; bar.style.left = `${e.target.offsetLeft}px`; } }); tab_a[0].click(); document.querySelectorAll('.ele-item').forEach((item, index) => { item.dataset.name = `b${index+1}`; }); box.forEach((item, index) => { item.classList.add(`b${index+1}`); }); function reactive(){ let ele_active = document.querySelector('.ele-item.active'); if(ele_active){ ele_active.classList.remove('active'); } } ele_nav.addEventListener('click', (e) => { let cname = e.target.className; if (cname === 'ele-item'||cname === 'ele-item active') { reactive(); e.target.classList.add('active'); let box = document.querySelector(`.${e.target.dataset.name}`); box_num = e.target.dataset.name.replace(/[^\d]/g, " "); window.scrollTo({ top: box.offsetTop - 50, left: 0, behavior: 'smooth' }); } if (cname === 'back-top') { reactive(); window.scrollTo({ top: 0, left: 0, behavior: 'smooth' }); box_num = -1; } }); const box_top_arr = Array.from(box).map((item)=>{ return item.offsetTop-55; }) var box_num = -1; function scroll_light_ele(){ let n = window.scrollY; let num = box_top_arr.reduce((prev, item, index)=>{ return n >= item ? index : prev; },-1); if(num === box_num){ return; }else{ box_num = num; } if(num > -1){ reactive(); document.querySelector(`[data-name="b${num+1}"]`).classList.add('active'); }else{ box_num = -1; } } window.addEventListener('scroll', scroll_light_ele); </script> </body>
</html>
|