vue基于better-scroll实现左右联动滑动页面

本文实例为大家分享了vue基于better-scroll实现左右联动滑动页面,供大家参考,具体内容如下

界面如下:

vue模板

<template>
 <div class="goods">
 <div class="menu-wrapper">
 <ul>
 <li class="menu-item" v-for="(good, index) in goods" :key="index"
  :class="{current: currentIndex===index}" @click="clickMenuItem(index)">
  <span class="text bottom-border-1px">
  <img :src="good.icon" class="icon" v-if="good.icon">
  {{good.name}}
  </span>
 </li>
 </ul>
 </div>
 <div class="foods-wrapper">
 <ul ref="foodsWrapperUl">
 <li class="food-list food-list-hook" v-for="(good, index) in goods" :key="index">
  <h1 class="title">{{good.name}}</h1>
  <ul>
  <li class="food-item bottom-border-1px" v-for="(food, index) in good.foods" :key="index">
  <div class="icon">
  <img :src="food.icon" width="57" height="57" alt="">
  </div>
  <div class="content">
  <h2 class="name">{{food.name}}</h2>
  <p class="desc">{{food.description}}</p>
  <div class="extra">
   <span class="count">月售{{food.sellCount}}份</span>
   <span>好评{{food.rating}}%</span>
  </div>
  <div class="price">
   <span class="now">¥{{food.price}}</span>
   <span class="old" v-if="food.oldPrice">¥{{food.oldPrice}}</span>
  </div>
  <div class="cartcontrol-wrapper"><cartcontrol :food="food"/></div>
  </div>
  </li>
  </ul>
 </li>
 </ul>
 </div>
 </div>
</template>

<script>
 import BScroll from 'better-scroll'
 import { mapState } from 'vuex'
 import CartControl from "../../../components/CartControl/CartControl"
 export default {
 data() {
 return {
 scrollY: 0,
 tops: []
 }
 },
 mounted () {
 this.$store.dispatch('getShopGoods', () => { //回调函数,等到action中执行
 this.$nextTick(() => { //页面更新后再使用滚动组件,获取DOM高度
  this._initScroll();
  this._initTops();
 })
 })
 },
 methods: {
 _initScroll() {
 new BScroll('.menu-wrapper', {
  click:true
 })

 this.foodsScroll = new BScroll('.foods-wrapper', {
  probeType: 2,
  click: true
 })

 this.foodsScroll.on('scroll', (pos) => {
  this.scrollY = Math.abs(pos.y);
 })

 this.foodsScroll.on('scrollEnd', (pos) => {
  this.scrollY = Math.abs(pos.y);
 })
 },

 _initTops () {
 const tops = [];
 let top = 0;
 tops.push(top);
 const lis = this.$refs.foodsWrapperUl.getElementByClassName('food-list-hook');
 Array.prototype.slice.call(lis).forEach((li, index) => {
  top += li.clientHeight;
  tops.push(top);
 })
 this.tops = tops;
 },

 clickMenuItem (index) {
 const top = this.tops[index];
 this.scrollY = top;
 this.foodsScroll.scrollTo(0, -top, 300)
 }
 },

 computed: {
 ...mapState(['goods']),
 currentIndex () {
 return this.tops.findIndex((top, index) => {
  return this.scrollY>=top && this.scrollY<this.tops[index+1]
 })
 }
 }
 }
</script>//也可以不用计算属性,直接在data中定义currentIndex,改动时赋新值,那页面自然跟着更新

state.js

goods: [], // 商品列表

action.js

//异步获取商品列表
 async getShopGoods ({commit}, callback) {
 const result = await reqShopGoods();
 const goods = result.data;
 commit(RECEIVE_GOODS, {goods});
 callback && callback();
 }

mutation.js

[RECEIVE_GOODS](state, {goods}) {
 state.goods = goods
 }

关于vue.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • vue基于mint-ui的城市选择3级联动的示例

    项目是基于 vue2 的移动端项目 1.实际效果 地址三级联动 mint-ui picker.png 2.首先你需要去下载一个包含中国省份,城市,区县的数据 如下: https://github.com/artiely/Administrative-divisions-of-China(里面包含二级联动数据,三级联动数据,四级联动数据等,找到自己需要的) 3.具体代码 主要是用到了mint-ui的picker组件,关于mint-ui的使用就自行看官网 Ⅰ .html组件 <div> <m

  • vue移动端的左右滑动事件详解

    本文实例为大家分享了vue移动端左右滑动事件,供大家参考,具体内容如下 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="https://unpkg.com/vue"></script> <meta name="viewport" c

  • vue mint-ui 实现省市区街道4级联动示例(仿淘宝京东收货地址4级联动)

    本文介绍了vue mint-ui 实现省市区街道4级联动示例(仿淘宝京东收货地址4级联动) 先去下载一个"省份.城市.区县.乡镇" 四级联动数据,然后 引入 import { Picker } from 'mint-ui'; //前提是npm install mint-ui -S Vue.component(Picker.name, Picker); 组件使用 <mt-picker :slots="addressSlots" class="picke

  • vue移动端实现手机左右滑动入场动画

    本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下 app.vue <template> <div id="app"> <transition :name="transitionName"> <keep-alive > <router-view v-if="$route.meta.keepAlive" class="Router">&

  • Vue.js组件tree实现省市多级联动

    小颖在上一篇随笔中写了两级的tree,下面给大家再分享一下用<ul><li>标签实现省市多级联动. 调用示例: <template> <div> <treeview :model='treedata'></treeview> </div> </template> <script> import treeview from './TreeView.vue' export default { compo

  • vue省市区三联动下拉选择组件的实现

    我们曾经经常会遇到需要选择省市区的需求,我们可能是找一个插件来实现,但是有了vue之后,我们自己完全可以简单的实现这个效果,并封装为独立的.vue组件,便于日后使用 我们今天来实现一个 利用vuejs开发的 省市区三联动的组件  CitySelect.vue组件 首先来看一下最终的效果(没有写太多的样式...) 组件所需要的省市区的JSON数据(已经封装为commonjs模块了):    provinces.js 这个数据中有这样几个字段: code: 当前省市区的编码 sheng: 当前所在的

  • 详解Vue、element-ui、axios实现省市区三级联动

    现在大部分电商的网站.app都需要用户或者管理者去选择设置地区等位置信息.下面我就介绍一下前端开发者用vue,axios,element-ui开发一个省市区三级联动的组件. 1.准备工作,首先我们需要全中国的省市区资源的json数据(科普一下:前六位数字是身份证前六位) 2.搭建vue-cli,安装axios,element-ui,创建vue,webpack项目 1). 在控制台或者终端执行以下代码,其中只需要路由(y),其他e2e,eslint这些不需要(y) vue init webpack

  • vue基于mint-ui实现城市选择三级联动

    项目是基于vue2 的移动端项目,供大家参考,具体内容如下 1.实际效果 地址三级联动 mint-ui picker.png 2.首先你需要去下载一个包含中国省份,城市,区县的数据 如下: (这个地址里面包含二级联动数据,三级联动数据,四级联动数据等,找到自己需要的) (一个更好的中国地区数据,推荐用这个) 3.具体代码 主要是用到了mint-ui的picker组件,关于mint-ui的使用就自行看官网 Ⅰ .html组件 <div> <mt-picker :slots="my

  • 基于Vue实现页面切换左右滑动效果

    基于Vue的页面切换左右滑动效果,具体内容如下 HTML文本页面: <template> <div id="app> <transition :name="direction" mode="out-in"> <!--动态获得transition 的name值--> <router-view class="app-view" wechat-title></router-vi

  • Vue实现移动端左右滑动效果的方法

    1. 最近得到一个新需求,需要在Vue项目的移动端页面上加上左右滑动效果,在网上查阅资料,最终锁定了vue-touch 2. 下载vue-touch (https://github.com/vuejs/vue-touch/tree/next) 注意:如果Vue是2.x 的版本的话,一定要下next分支上的. 3. 使用: 3.1 npm install vue-touch@next --save 3.2 在main.js 中 引入: import VueTouch from 'vue-touch

随机推荐