安装命令:
npm install better-scroll
引入better-scroll
import scroll from 'better-scroll';
methods:{ scrolls(){ this.menuScroll=new scroll(this.$refs['menu-wrap'],{}) this.foodScroll=new scroll(this.$refs['food-wrap'],{}) }, }
vue访饿了么滚动
<template> <div class="goods"> <div class="menu-wrap" ref="menu"> <ul> <li v-for="(item,index) in goodsList" class="menu-item" :class="{'current':currentIndex===index}" > <icon v-if="item.type>0" :types="item.type"></icon> <span class="text">{{item.name}}</span> </li> </ul> </div> <div class="foods-wrap" ref="food"> <ul> <li v-for="item in goodsList" class="food-list"> <h1 class="title">{{item.name}}</h1> <ul> <li v-for="food in item.foods" class="food-item"> <div class="icon"> <img width="57" height="57" :src="food.icon" alt=""> </div> <div class="content"> <h2 class="name">{{food.name}}</h2> <p class="desc">{{food.description}}</p> <div class="month"> <span class="count">预售{{food.sellCount}}份</span> <span>好评率{{food.rating}}%</span> </div> <div class="price"> <span class="new">¥{{food.price}}</span> <span class="old" v-show="food.oldPrice">{{food.oldPrice}}</span> </div> </div> </li> </ul> </li> </ul> </div> </div> </template> <script> import icon from '../icon/icon'; import scroll from 'better-scroll'; export default{ name:'goods', data(){ return { goodsList:[], heightList:[],//存入高度 scrollY:0 } }, props:{ seller:{ type:Object } }, computed:{ currentIndex(){ for(let i=0;i<this.heightList.length;i++){ let height1=this.heightList[i];//第一个高度 let height2=this.heightList[i+1];//第二个高度 //判断当前的滚动的高度是在存放高度数组中那个区间滚动,然后返回 if(this.scrollY>=height1&&this.scrollY<height2 ){ console.log(this.scrollY,height1,height2) //这里是判断数组的最后一个是否存在 if(height2){ return i; }else{ return 0; } } } } }, methods:{ scrolls(){ this.menuScroll=new scroll(this.$refs['menu'],{}) this.foodScroll=new scroll(this.$refs['food'],{ probeType:3 }) this.foodScroll.on('scroll',(pos)=>{ //当前滚动的位置,需要返回正数 this.scrollY=Math.abs(Math.round(pos.y)); }) }, heights(){ var foodList=this.$refs['food'].getElementsByClassName('food-list'); var h=0; this.heightList.push(h); for(let i=0;i<foodList.length;i++){ //记录每个dom元素的高度 h+=foodList[i].clientHeight; this.heightList.push(h); } } }, created(){ this.$axios.get('/api/goods').then(res=>{ if(res.status==200){ this.goodsList=res.data.data; // console.log(this.goodsList) this.$nextTick(function(){ this.scrolls(); //计算高度 this.heights(); }) }else{ console.log(res) } }).catch(error=>{ console.log(error) }) }, components:{ icon }, } </script> <style scoped lang="less"> .goods{ display: flex; position: absolute; width: 100%; top: 177px; bottom: 46px; overflow: hidden; .menu-wrap{ width: 80px; background: #f3f5f7; ul{ overflow-y: auto; } .menu-item{ display: flex; align-items: center; height: 54px; padding:0 12px; line-height: 14px; border-bottom: 1px solid rgba(7, 17, 27, .1); &.current{ background: #ccc; } .icon{ width: 30px; height: 30px; margin-right: 2px; } .text{ font-size: 12px; font-weight: 200; } } } .foods-wrap{ flex: 1; .title{ padding-left: 14px; height: 26px; line-height: 26px; border-left: 2px solid #d9dde1; font-size: 12px; color: rgb(143, 153, 159); background: #f3f5f7; } .food-item{ display: flex; margin: 18px; padding-bottom: 18px; border-bottom: 1px solid rgba(7, 17, 27, .1); &:last-child{ border-bottom: none; margin-bottom: 0; } .icon{ margin-right: 10px; } .content{ flex: 1; .name{ font-size: 14px; line-height: 14px; font-size: 14px; color: rgb(7, 17, 27); } .desc{ margin: 8px 0; line-height: 14px; font-size: 12px; color: rgb(7, 17, 27); } .month{ line-height: 10px; font-size: 10px; color: rgb(7, 17, 27); } .price{ font-weight: 700; line-height: 24px; .new{ margin-right: 8px; font-size: 14px; color: rgb(240, 20, 20) } .old{ text-decoration: line-through; font-size: 10px; color: rgb(143, 153, 159); } } } } } } </style>
官方API
https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/options.html#bouncetime
发表评论
侧栏公告
寄语
譬如朝露博客是一个分享前端知识的网站,联系方式11523518。
热评文章
标签列表
热门文章
友情链接