• 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;
    },
  • 相关阅读:
    文本框只能输入数字
    Excel 文件读取
    C# 时间格式
    window 锁屏
    窗体程序 (控件随窗体变化而变化)
    dataGridView操作数据(增加 读取 绑定 获取选择的单元格 选中整行单元格时颜色变换 更新数据 锁定列的位置 添加行 列头的方法)
    集合
    登录窗体关闭 show() showDialog()
    查询字符串中字母出现的个数
    pytorch F.cross_entropy(x,y)理解
  • 原文地址:https://www.cnblogs.com/qing0228/p/14089650.html
Copyright © 2020-2023  润新知