javaScript前进后退点击事件

<template>
    <div>
      <div v-for="(item,index) in this.titleVisibleData">
        <div class="title">{{item}}</div>
      </div>
      <div>
        <button @click="moveLeft"><</button>
        <button @click="moveRight"> ></button>
      </div>
    </div>
</template>

<script>
    export default {
        name: "index",
        data(){
          return{
            titleData:['元素1','元素2','元素3','元素4','元素5','元素6'],
            titleVisibleData:[]
          }
        },
        methods:{
          moveLeft(e){
            if(this.titleData.length <= 3) return;
            this.handleMove(-1)
          },
          moveRight(e){
            if(this.titleData.length <= 3) return;
            this.handleMove(1)
          },
          handleMove(type){
            let _name = this.titleVisibleData[0],_idx=0,_temp=[];
            console.log(type,_name)
            this.titleData.forEach((element,idx)=>{
              if(element == _name){
                _idx = idx;
              }
            })
            let _start = _idx + type;
            for(let i=0;i<3;i++){
              let _maxLenth = this.titleData.length;
              if(_start >= _maxLenth){
                _start = 0;
              }else if(_start<0){
                _start = _maxLenth - 1;
              }
              _temp.push(this.titleData[_start]);
              _start++;
            }
            this.titleVisibleData = _temp;
          },

        },
        mounted(){
          this.titleVisibleData = this.titleData.slice(0,3)
          console.log(this.titleVisibleData,'***')
        }
    }
</script>

<style lang="less">
  .title{
    margin-left: 20px;
    width: 100px;
    float:left;
    height: 50px;
    margin-right: 10px;
    border: 1px solid dodgerblue;
    line-height: 50px;
  }
</style>

结果:

  亦可根据实际情况,选择要展示的数据数量,以及每次向左或向右移动元素的个数,下面代码针对methods中的函数,进行向左向右移动展示元素个数(3个)个数量,并设定限制,当向左移动到首个元素时不再往前翻页,向右移动到某位元素时,不在向后翻页,此时尾页元素可能存在少于3个的情况,代码如下:

moveLeft(e){
    if(this.titleData.length <= 3) return;
    this.handleMove(-3);
    }
},
moveRight(e){
    if(this.titleData.length <= 3) return;
    this.handleMove(3)
    }
},
handleMove(type){
    let _name = this.titleVisibleData[0],_idx=0,_temp=[];
    console.log(type,_name)
    this.titleData.forEach((element,idx)=>{
      if(element == _name){
        _idx = idx;
      }
    })
    let _maxLenth = this.titleData.length;
    let _start = _idx + type;
    if(this.titleVisibleData.length < 3 && type > 0){
      _temp = this.titleVisibleData
    }else{
      if(_start < 0){
        _start = 0;
        if(_maxLenth>=3){
          for(let i=0;i<3;i++){
            _temp.push(this.titleData[_start]);
            _start++;
          }
        }else{
          for(let i=0;i<_maxLenth-_start+1;i++){
            _temp.push(this.titleData[_start]);
            _start++;
          }
        }
      }else if(_start >=0 ){
        if(_start + 3 <= _maxLenth){
          for(let i=0;i<3;i++){
            _temp.push(this.titleData[_start]);
            _start++;
          }
        }else{
          for(let i=0;i<_maxLenth-_start+1;i++){
            _temp.push(this.titleData[_start]);
            _start++;
          }
        }
      }
    }
    this.titleVisibleData = _temp;
},

 

posted on 2020-12-05 14:44  青小记  阅读(285)  评论(0)    收藏  举报