1、目标效果
最近喝了不少的咖啡、奶茶,有一个效果我倒是挺好奇怎么实现的:
(1)点击左侧分类菜单,右侧滚动到该分类区域
(2)右侧滑动屏幕,左侧显示当前所处的分类区域
这种功能会出现在商城项目中或者分类数量较多的项目中,专业名称称电梯导航
目标效果:
(1)点击左侧的分类,右侧滑动到指定区域
(2)滚动右侧区域,左边分类显示当前所处的分类区域
2、原理
(1)这要用到原生js关于偏移量和位置相关的api,这些api建立在你的布局是定位的基础上,父亲相对定位,左边分类和右边商品都是绝对定位
(2)左边分类要与右侧商品模块数量一一相等(数量和位置都要对应相等),否则实现不了电梯导航效果
(3)点击左侧分类,右侧跳转到对应模块;这用到了window.scrollTo(水平方向距离,竖直方向距离)
(4)右侧滑动,左侧发生相应的变化,这要用到滚动事件,vue中使用滚动事件需要再onMounted()生命周期注册一下滚动事件
1 2 3 | onMounted(() => { window.addEventListener( 'scroll' , handleScroll); }) |
(5)如何判断滚动到什么程度左侧才显示对应的模块?
- dom.offsetTop:每个dom元素有该属性,表示距离顶部窗口的距离
- document.documentElement.scrollTop:表示页面滚动的距离
- document.documentElement.scrollTop >=dom.offsetTop:显示对应的模块,可以通过遍历商品模块数组,拿到对应的索引,然后设置左边分类对应的dom为激活状态
(6) 出现一个问题:window.scrollTo()将模块滚动至某一位置 与页面滚动事件 发生了冲突,此时可以添加一个互斥变量isLock,等window.scrollTo()滚动结束之后,再放开锁
1 2 3 4 5 6 7 8 9 10 | // 获取选中的dom元素 const typeItemDom = shop.value[val] // 开锁 isLock.value = true // 第一个参数为水平方向,第二个参数为纵轴方向 window.scrollTo(0, typeItemDom.offsetTop) setTimeout(() => { //关锁 isLock.value = false }, 0) |
(7)为什么放开锁要在setTimeout里面?根据js事件循环机制,同步任务(主线程代码、new Promise里面的代码)执行速度快于异步任务(setTimeout、setInterval、ajax、promise.then里面的任务),这样才能确保锁是在window.scrollTo() 执行完毕之后才打开的
3、源代码
App.vue
1 2 3 4 5 6 7 8 9 10 11 | <div> </div> // import ClassifyByJs from './components/ClassifyByJS.vue'; import ClassifyByVue from './components/ClassifyByVue.vue' ; * { padding: 0; margin: 0; } |
ClassifyByVue.vue
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | <div class = "classify" > <div class = "left" > <div class = "item" >{{ item }} </div> </div> <div class = "right" > <div> <div class = "title" >{{ item.category }}</div> <div class = "item" > <div class = "photo" > <img decoding= "async" src= "https://www.2it.club/wp-content/uploads/2023/05/frc-87f475854e685657f8e280b5d99567f7.jpg" > </div> <div class = "info" > <div class = "name" >{{ i.name }}</div> <div class = "type" >{{ i.type }}</div> <div class = "desc" >{{ i.desc }}</div> <div class = "buy" >购买</div> </div> </div> </div> </div> </div> import { ref, onMounted, onBeforeUnmount, nextTick } from 'vue' let isLock = ref( false ) // 分类 let types = ref([ '人气Top' , '爆款套餐' , '大师咖啡' , '小黑杯' , '中国茶咖' , '生椰家族' , '厚乳拿铁' , '丝绒拿铁' , '生酪拿铁' , '经典拿铁' , ]) // 商品 let shops = ref([ { category: '人气Top' , data: [ { name: '冰吸生椰拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '生椰拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '摸鱼生椰拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '茉莉花香拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '丝绒拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '小甘橘美式' , type: '咖啡' , desc: '咖啡' }, ] }, { category: '爆款套餐' , data: [ { name: '2杯套餐' , type: '咖啡' , desc: '咖啡' }, { name: '3杯套餐' , type: '咖啡' , desc: '咖啡' }, { name: '4杯套餐' , type: '咖啡' , desc: '咖啡' }, { name: '5杯套餐' , type: '咖啡' , desc: '咖啡' }, { name: '不喝咖啡套餐' , type: '咖啡' , desc: '咖啡' }, { name: '必喝套餐' , type: '咖啡' , desc: '咖啡' }, ] }, { category: '大师咖啡' , data: [ { name: '美式' , type: '咖啡' , desc: '咖啡' }, { name: '加浓美式' , type: '咖啡' , desc: '咖啡' }, { name: '橙C美式' , type: '咖啡' , desc: '咖啡' }, { name: '澳瑞白' , type: '咖啡' , desc: '咖啡' }, { name: '卡布奇诺' , type: '咖啡' , desc: '咖啡' }, { name: '玛奇朵' , type: '咖啡' , desc: '咖啡' }, ] }, { category: '小黑杯' , data: [ { name: '云南小柑橘' , type: '咖啡' , desc: '咖啡' }, { name: '广东小柑橘' , type: '咖啡' , desc: '咖啡' }, { name: '广西小柑橘' , type: '咖啡' , desc: '咖啡' }, { name: '福建小柑橘' , type: '咖啡' , desc: '咖啡' }, { name: '湖南小柑橘' , type: '咖啡' , desc: '咖啡' }, { name: '江西小柑橘' , type: '咖啡' , desc: '咖啡' }, ] }, { category: '中国茶咖' , data: [ { name: '碧螺知春拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '茉莉花香拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '菊花香拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '梅花香拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '兰花香拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '玫瑰花香拿铁' , type: '咖啡' , desc: '咖啡' }, ] }, { category: '生椰家族' , data: [ { name: '冰吸生椰拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '生椰拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '摸鱼生椰拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '椰云拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '丝绒拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '陨石拿铁' , type: '咖啡' , desc: '咖啡' }, ] }, { category: '厚乳拿铁' , data: [ { name: '厚乳拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '生椰拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '茉莉花香拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '椰云拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '丝绒拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '海盐拿铁' , type: '咖啡' , desc: '咖啡' }, ] }, { category: '丝绒拿铁' , data: [ { name: '丝绒拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '生椰丝绒拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '黑糖丝绒拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '椰云丝绒拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '香草丝绒拿铁' , type: '咖啡' , desc: '咖啡' } ] }, { category: '生酪拿铁' , data: [ { name: '生酪拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '绿豆拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '红豆拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '黑豆拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '黄豆拿铁' , type: '咖啡' , desc: '咖啡' } ] }, { category: '经典拿铁' , data: [ { name: '拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '陨石拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '焦糖拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '生椰拿铁' , type: '咖啡' , desc: '咖啡' }, { name: '美式' , type: '咖啡' , desc: '咖啡' } ] }, ]) // 获取右侧商品的ref实例 let shop = ref( null ) // 用来表示当前选中处于激活状态的分类的索引 let currentIndex = ref(0) // 切换类型 const changeType = val => { currentIndex.value = val // 获取选中的dom元素 const typeItemDom = shop.value[val] // 开锁 isLock.value = true // 第一个参数为水平方向,第二个参数为纵轴方向 window.scrollTo(0, typeItemDom.offsetTop) setTimeout(() => { //关锁 isLock.value = false }, 0) } // 监听页面滚动 const handleScroll = () => { // 锁关了滚动事件才有效 if (!isLock.value) { types.value.forEach((item, index) => { // console.dir(shop.value[index]); const shopItemDom = shop.value[index] // 每个模块距离顶部的距离 const offsetTop = shopItemDom.offsetTop // 页面滚动的距离 const scrollTop = document.documentElement.scrollTop if (scrollTop >= offsetTop) { // 给左边分类设置激活的效果 currentIndex.value = index } }) } } onMounted(() => { window.addEventListener( 'scroll' , handleScroll); }) onBeforeUnmount(() => { window.removeEventListener( 'scroll' , handleScroll); }) .classify { display: flex; position: relative; .left { display: flex; flex-direction: column; align-items: center; position: fixed; left: 0; top: 0; bottom: 0; width: 92px; overflow-y: scroll; border-right: 1px solid #C1C2C4; .item { display: flex; justify-content: center; align-items: center; width: 67px; height: 29px; font-size: 15px; font-family: PingFang SC-Semibold, PingFang SC; font-weight: 600; color: #333333; } .active { color: #00B1FF; } .item:not(:last-child) { margin-bottom: 25px; } } .right { flex: 1; position: absolute; top: 0; right: 17px; overflow-y: scroll; .title { font-size: 18px; margin-bottom: 5px; } .item { display: flex; justify-content: space-between; margin-bottom: 17px; width: 246px; height: 73px; .photo { width: 58px; height: 58px; img { width: 100%; height: 100%; border-radius: 12px; border: 1px solid gray; } } .info { display: flex; flex-direction: column; position: relative; width: 171px; height: 73px; box-shadow: 0px 1px 0px 0px rgba(221, 221, 221, 1); .name { padding-left: 0; font-size: 17px; font-weight: 600; color: #333333; } .type, .desc { font-size: 14px; font-weight: 400; color: #999999; } .buy { display: flex; align-items: center; justify-content: center; position: absolute; right: 0; top: 17px; width: 67px; height: 29px; background: #E7E8EA; border-radius: 21px; font-size: 15px; font-family: PingFang SC-Semibold, PingFang SC; font-weight: 600; color: #05AFFA; } } } } } |
到此这篇关于Vue实现电梯样式锚点导航效果流程详解的文章就介绍到这了,更多相关Vue电梯锚点导航内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!