From 026abc452f32a38c8d697b877a29553753cd27ec Mon Sep 17 00:00:00 2001 From: wenghaocan Date: Sat, 29 Sep 2018 15:46:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=95=86=E5=93=81=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E9=80=89=E4=B8=AD=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/view/goods-classify.vue | 40 ++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/view/goods-classify.vue b/src/view/goods-classify.vue index 238f152..20bf3d6 100644 --- a/src/view/goods-classify.vue +++ b/src/view/goods-classify.vue @@ -61,11 +61,12 @@ goodsList: [], imghost: "http://medou.oss-cn-shenzhen.aliyuncs.com/", listHeight: [], //存储区块的高度 - scrollY: {} + scrollY: {}, + currentNum: 0 } }, computed: { - currentIndex () { + /*currentIndex () { const { scrollY, listHeight } = this let index = listHeight.findIndex((height, index) => { return scrollY >= listHeight[index] && scrollY < listHeight[index + 1] @@ -74,6 +75,26 @@ index++ } return index + }*/ + + currentIndex(){ + for (let i = 0; i < this.listHeight.length; i++){ + let height = this.listHeight[i] + let height2 = this.listHeight[i + 1] + //当height2不存在的时候,或者落在height和height2之间的时候,直接返回当前索引 + //>=height,是因为一开始this.scrollY=0,height=0 + if( !height2 || (this.scrollY >= height && this.scrollY < height2)) { + return i + } + if(this.listHeight[this.listHeight.length-1] - this.$refs.foodWrapper.clientHeight <= this.scrollY ) { + if(this.clickTrue) { + return this.currentNum + } else { + return (this.listHeight.length-1) + } + } + } + return 0 } }, components: { @@ -152,13 +173,26 @@ this.listHeight.push(height); } }, - selectMenu(index, event) { + /*selectMenu(index, event) { if(event && !event._constructed) { return ; } let foodList = this.$refs.foodWrapper.getElementsByClassName("food-list-hook"); let el = foodList[index]; this.foodsScroll.scrollToElement(el,300) + },*/ + selectMenu(index, event){ + this.clickTrue = true //触发过剑姬事件之后,clickTrue的值变为true + this.currentNum = index + //在better-scroll的派发事件的event和普通浏览器的点击事件event有个属性区别_constructed + // 浏览器原生点击事件没有_constructed所以当时浏览器监听到该属性的时候return掉 + if (event && !event._constructed){ + return + } else { + let foodList = this.$refs.foodWrapper.getElementsByClassName("food-list-hook"); + let el = foodList[index]; + this.foodsScroll.scrollToElement(el,300) + } }, autoSelect() { for (let i = 0; i < this.goodsList.length; i++) {