vue上下滑动翻页组件

摘要:
this.isDown){this.isDown=true;this.startX=e.touches[0].clientX;this.startY=e.touches[0];clientY;return;}否则{this.endX=e.touches[0].clientX;this.endY=e.touch[0].cclientY;}},touchend(){if(!

demo地址 http://106.13.201.82/demo/page_change.html

vue上下滑动翻页组件第1张

page_change.vue

<template>
  <div   :  :  ref="hei">
    <slot></slot>
  </div>
</template>
<script>
  var windowHeight = document.documentElement.clientHeight||window.innerHeight;
  var windowWidth =document.documentElement.clientWidth||window.innerWidth;
  export default {
    data:function () {
      return {
        preIndex:0,
        curIndex:0,
        startX:0,
        startY:0,
        endX:0,
        endY:0,
        len:0,
        '',
        height:'',
        isDown:false,
        num:this.direction.indexOf('reverse')>-1?-1:1,
        distance:50,
      }
    },
    props:{
      //距离
      distance:{
        type: Number,
        default:50
      },
      //方向
      direction:{
        type: String,
        default:'column'//row|row-reverse|column|column-reverse
      },
    },
    mounted: function () {
      this.scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      this.scrollLeft = document.documentElement.scrollLeft|| document.body.scrollLeft;

      this.len=this.$refs.hei.children.length;
      if(this.direction==='row'||this.direction==='row-reverse'){
        this.width=this.len*windowWidth+'px';
        this.height=windowHeight+'px';
      }else{
        this.width=windowWidth+'px';
        this.height=this.len*windowHeight+'px';
      }

      window.addEventListener('touchstart', this.touchstart);
      window.addEventListener('touchmove', this.touchmove);
      window.addEventListener('touchend', this.touchend);
      this.$nextTick(()=>{
        if(this.direction==='row'||this.direction==='row-reverse') {
          this.curIndex=Math.round(this.scrollLeft/windowWidth)
        }else{
          this.curIndex=Math.round(this.scrollTop/windowHeight)
        }
      })
    },
    methods:{
      touchstart (e) {
        this.isDown=true;
        this.startX = e.touches[0].clientX;
        this.startY = e.touches[0].clientY;
      },
      touchmove(e){
        if(!this.isDown){
          this.isDown=true;
          this.startX = e.touches[0].clientX;
          this.startY = e.touches[0].clientY;
          return;
        }else{
          this.endX = e.touches[0].clientX;
          this.endY = e.touches[0].clientY;
        }
      },
      touchend() {
        if(!this.isDown){
          return;
        }
        this.isDown=false;
        this.preIndex=this.curIndex;
        if(this.direction==='row'||this.direction==='row-reverse'){
          if(this.startX-this.endX>this.distance){
            this.curIndex=this.curIndex+this.num;
          }else if(this.startX-this.endX<-this.distance){
            this.curIndex=this.curIndex-this.num;
          }
          window.scrollTo({
            left:this.curIndex*windowWidth,
            behavior:'smooth'
          })
        }else{
          if(this.startY-this.endY>this.distance){
            this.curIndex=this.curIndex+this.num;
          }else if(this.startY-this.endY<-this.distance){
            this.curIndex=this.curIndex-this.num;
          }
          window.scrollTo({
            top:this.curIndex*windowHeight,
            behavior:'smooth'
          })
        }
        if(this.curIndex>this.len-1){
          this.curIndex=this.len-1;
        }else if(this.curIndex<0){
          this.curIndex=0;
        }
        this.$emit('pageChange',{
          direction:this.direction,
          curIndex:this.curIndex,
          preIndex:this.preIndex,
        })
      }
    }
  };
</script>
<style  type="text/css" lang="less" scoped>
  .flex{
    display:flex;
  }
  .column{
    flex-direction: column;
  }
  .column-reverse{
    flex-direction: column-reverse;
  }
  .row{
    flex-direction: row;
  }
  .row-reverse{
    flex-direction: row-reverse;
  }
</style>

免责声明:文章转载自《vue上下滑动翻页组件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇写一个工具生成数据库实体类[笔记]--Xbrowser远程连接Linux桌面环境下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Vue项目中引入electron发布桌面应用

1. 在原先已有的Vue项目里面,打开终端执行vue add electron-builder ; 2. 选择合适的版本安装好这个插件,执行npm run electron:serve ; 3. 如果页面没有什么错误,就会弹出一个vue的应用窗口,说明准备工作已经完成; 4. 如果需要修改桌面应用界面或窗口之类的一些配置,则需要在package.json文...

Vue 获取URL链接后面的参数值

PC端如以下地址获取ptype和orderId的值 http://localhost:20472//AppWeb/ToPay?ptype=2&orderId=e6c1f659-94cd-4e4d-b7c9-56b3c8a7a8d6 function getQueryString(name) { var reg = new RegExp("...

vue 插件tab选项卡(转载)

<template> <tab :options="tabOpt" :state.sync="stateIndex"></tab> </template> <script type="text/babel"> import tab from 'components/tab_touc...

(四)Vue指令

常用内置指令   v:text : 更新元素的 textContent   v-html : 更新元素的 innerHTML   v-if : 如果为true, 当前标签才会输出到页面   v-else: 如果为false, 当前标签才会输出到页面   v-show : 通过控制display样式来控制显示/隐藏   v-for : 遍历数组/对象   v...

Vue 使用 Antd 简单实现左侧菜单栏和面包屑功能

参考: https://www.cnblogs.com/wjw1014/p/13925969.html    路由调用 import Hello from '@/Home/Hello' Vue.use(Router) export default new Router({ routes: [ { path: "/2",...

vue之templete模板

1.templete里要用data里的数据的话,不要加this. 2.按理说Js是写在<script></script>标签体内的。但是Vue的templete模板中对所有的数据绑定,都提供了完全的js支持。 代码示例如下: 强调"tableData.slice((currentPage-1)*pageSize,currentPag...