欢迎来到代码驿站!

vue

当前位置:首页 > 网页前端 > vue

vue2.0中goods选购栏滚动算法的实现代码

时间:2021-03-05 12:42:24|栏目:vue|点击:

不多说,直接代码,以便以后重复利用:

<script type="text/ecmascript-6">
import BScroll from 'better-scroll';
const ERR_OK = 0;
export default {
 props: {
  sell: {
   type: Object
  }
 },
 data() {
  return {
   goods: [],
   listHeight: [],
   scrollY: 0
  };
 },
 computed: {
  currentIndex() {
   for (let i = 0; i < this.listHeight.length; i++) {
    let height1 = this.listHeight[i];
    let height2 = this.listHeight[i + 1];
    if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
     return i;
    };
   }
   return 0;
  }
 },
 created() {
  this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee'];
  this.$http.get('/api/goods').then((response) => {
   response = response.body;
   if (response.errno === ERR_OK) {
    this.goods = response.data;
    this.$nextTick(() => {
     this._initScroll();
     this._calculateHeight();
    });
   }
  });
 },
 methods: {
  _initScroll() {
   this.menuScroll = new BScroll(this.$refs.menuwrapper, {
    click: true
   });
   this.foodScroll = new BScroll(this.$refs.foodswrapper, {
    probeType: 3
   });
   this.foodScroll.on('scroll', (pos) => {
    this.scrollY = Math.abs(Math.round(pos.y));
   });
  },
  _calculateHeight() {
   let foodList = this.$refs.foodswrapper.getElementsByClassName('food-list-hook');
   let height = 0;
   this.listHeight.push(height);
   for (let i = 0; i < foodList.length; i++) {
    let item = foodList[i];
    height += item.clientHeight;
    this.listHeight.push(height);
   }
  },
  selectMenu(index, event) {
   if (!event._constructed) {
    return;
   };
   console.log(index);
   let foodList = this.$refs.foodswrapper.getElementsByClassName('food-list-hook');
   let el = foodList[index];
   this.foodScroll.scrollToElement(el, 300);
  }
 }
};
</script>

上一篇:关于vue-router的beforeEach无限循环的问题解决

栏    目:vue

下一篇:Vue中ref和$refs的介绍以及使用方法示例

本文标题:vue2.0中goods选购栏滚动算法的实现代码

本文地址:http://www.codeinn.net/misctech/74708.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有