实现翻页组件

本文详细解析了一个基于Vue的翻页组件实现,该组件通过接收父组件的数据进行动态展示,并支持左右翻页功能。组件利用v-model实现了数据的双向绑定,同时提供了翻页数量、显示数量及是否完全翻页等配置选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
  <div class="turnpage-container">
    <!-- 向左移动 -->
    <div @click="onBeforenavClick()">
    //插入左边内容
      <slot name="turnBefore">
        <i class="el-icon-d-arrow-left turn-icon"></i>
      </slot>
    </div>
    <!-- 内容的显示 -->
    <div v-for="(item,index) in newList" @click="onItemClick(item)" :key="index">
    //插槽的知识点,采用v-bind来抛出相应的item与index
      <slot name="turnContent" v-bind:item="item" v-bind:index="index">
        <span class="turn-font">{{item}}</span>
      </slot>
    </div>
    <!-- 向右移动 -->
    <div @click="onAfternavClick()">
      <slot name="turnAfter">
        <i class="el-icon-d-arrow-right turn-icon " />
      </slot>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    //显示父组件原本的数组,必传
    parentData: {
      type: Array,
      required: true
    },
    //数据双向绑定,点击当前的状态 v-model
    value: {
      type: Number,
      required: true,
      default: 0
    },
    //父组件传来一页翻转多少个, 必传
    turnPageNumber: {
      type: Number,
      required: true
    },
    //一页显示多少个,必传
    showPageNumber: {
      type: Number,
      required: true
    },
    //是否完全翻页,可以不传,不传默认是false不完全翻页。true表示完全翻页
    isfullPage: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      activedItem: -1,
      index: 0, //数组的下标
      newList: [] //跳转后的数组
    }
  },

  methods: {
    //点击显示当前的id
    onItemClick(item) {
      //this.activedItem = item
      //数据双向绑定数据当前的index
      this.$emit('input', this.index)
      //点击抛出事件,父组件自己做相应的处理
      this.$emit('turnPageChange', item)
    },

    // 导航条向左滚动
    onBeforenavClick() {
      if (this.index > 0) {
        this.index = this.index - this.turnPageNumber
        this.onItemClick() //抛出v-model
      }
    },
    //导航条向右滚动
    onAfternavClick() {
      if (this.index < this.parentData.length - this.turnPageNumber) {
        this.index = this.index + this.turnPageNumber
      }
      this.onItemClick() //抛出v-model
    }
  },
  watch: {
    parentData: {
      immediate: true, // 这句重要
      handler(val) {
        this.showList = val
        this.newList = val.slice(0, this.showPageNumber) //初始化的时候截取
      }
    },
    //监视value的值的改变
    value(val, oldValue) {
      this.index = val
      //下标改变,就开始截取数据
      this.newList = this.parentData.slice(this.index, this.index + this.showPageNumber)
      if (this.newList.length < this.showPageNumber && this.isfullPage === true && val > oldValue) {
        this.index = this.parentData.length - this.showPageNumber
        this.newList = this.parentData.slice(-this.showPageNumber)
      }
      if (this.newList.length < this.showPageNumber && this.isfullPage === true && val < oldValue) {
        this.newList = this.parentData.slice(0, this.showPageNumber)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.turnpage-container {
  height: 100%;
  width: 100%;
  position: relative;
  display: flex;
}
.turn-icon {
  font-size: 18px;
  color: #888888;
}
.turn-font {
  color: #eeeeee;
  font-size: 16px;
}
</style>

案例

    <template v-slot:dragContainer>
      <TurningPage class="attribute-query-container" v-model="currentPage" :parentData="attributeContent" :turnPageNumber='1' :showPageNumber="1"  >
        <template v-slot:turnBefore>
          <div class="attribute-turning">
            <i class="el-icon-arrow-left turning-btn"></i>
          </div>
        </template>
        <template v-slot:turnContent="{ item, index }">
          <ScrollDiv class="attribute-query-content" :isHorizontal="true">
            <div v-for="(value,key) in item" :key="key" class="attribute-query-results">
              <div class="attribute-key">{{key}}:</div>
              <div class="attribute-key">{{value}}</div>
            </div>
          </ScrollDiv>
        </template>
        <template v-slot:turnAfter>
          <div class="attribute-turning">
            <i class="el-icon-arrow-right turning-btn"></i>
          </div>
        </template>
      </TurningPage>
    </template>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值