vue自动播放(无限轮播)

效果(懒得放动图了)

父子页面实现

父页面代码

<template>
  <div class="scroll">
    <!-- 控制轮播速度 -->
    <list-scroll class="box" :speed="0.6">
      <div class="list">
        <div class="item" v-for="item in list" :key="item.xh">
          <span>{{ item.xh }}</span
          ><span>{{ item.label }}</span>
        </div>
      </div>
    </list-scroll>
  </div>
</template>

<script>
import listScroll from '@/components/listScroll.vue'
export default {
  name: "scroll",
  components: { listScroll },
  data() {
    return {
      list: new Array(60)
        .fill(1)
        .map((s, i) => ({ xh: i + 1, label: "hello wrold" })),
    };
  },
};
</script>

<style lang="less" scoped>
.box {
  height: 300px;
}
.list {
  padding: 0 10px;
  width: 300px;
  .item {
    display: flex;
    justify-content: space-between;
    padding: 5px 0;
    cursor: pointer;
    &:hover {
      background-color: #95a5a6;
    }
  }
}
</style>

子页面代码

<template>
  <div class="listScroll" ref="box">
    <slot></slot>
    <slot></slot>
  </div>
</template>
  
  <script>
export default {
  name: "listScroll",
  created() {},
  mounted() {
    this.$nextTick(() => {
      //在盒子内容高度小于可视高度时不滚动
      if (this.boxHeight < this.ele0.clientHeight) {
        this.start(this.height);
        this.setEvet();
      } else {
        this.isScroll = false;
      }
    });
  },
  props: {
    speed: {
      default: 1,
      type: Number,
    },
  },
  computed: {
    //第一个slot
    ele0() {
      return this.$refs.box.children[0];
    },
    //第二个slot
    ele1() {
      return this.$refs.box.children[1];
    },
    //盒子的可视高度
    boxHeight() {
      return this.$refs.box.clientHeight;
    },
  },
  data() {
    return {
      height: 0,
      isScroll: true,
    };
  },
  methods: {
    //鼠标移入停止滚动 移出继续滚动
    setEvet() {
      this.$refs.box.onmouseenter = () => {
        this.isScroll = false;
        // this.height = 0;
      };
      this.$refs.box.onmouseleave = () => {
        this.isScroll = true;
        this.$nextTick(() => {
          this.start(this.height);
        });
      };
    },
    //滚动方法
    start(height) {
      this.ele0.style = `transform:translateY(-${height}px);`;
      this.ele1.style = `height:${this.boxHeight}px;transform:translateY(-${height}px);overflow: hidden;`;
      if (height >= this.ele0.clientHeight) {
        this.height = 0;
      } else {
        this.height += this.speed;
      }
      if (!this.isScroll) return;
      window.requestAnimationFrame(() => {
        this.start(this.height);
      });
    },
  },
};
</script>
  
  <style lang="less" scoped>
.listScroll {
  overflow: hidden;
}
.hover {
  overflow-y: auto;
}
.hide {
  display: none;
}
</style>
  
  

原帖子(https://blog.youkuaiyun.com/ZuoZuoDangerou/article/details/123662676)

### Vue 3 和 TypeScript 实现的无限轮播组件 为了创建一个适用于 Vue 3 并采用 TypeScript 的无限轮播组件,可以遵循现代前端开发的最佳实践。下面是一个简单的例子来展示如何构建这样一个组件。 #### 安装依赖项 首先,在项目中安装必要的库: ```bash npm install vue-class-component vue-property-decorator ``` 这些包可以帮助更好地管理类风格的 Vue 组件以及属性装饰器的支持[^1]。 #### 创建 InfiniteCarousel.vue 文件 接下来定义 `InfiniteCarousel` 组件的内容如下所示: ```vue <template> <div class="carousel"> <transition-group tag="ul" name="slide"> <li v-for="(item, index) in itemsToShow" :key="index"> <!-- 插槽允许自定义每张幻灯片 --> <slot :item="item"></slot> </li> </transition-group> <button @click="prev">Previous</button> <button @click="next">Next</button> </div> </template> <script lang="ts"> import { defineComponent, ref, computed } from 'vue'; export default defineComponent({ props: { slides: Array as () => any[], }, setup(props) { const currentIndex = ref(0); function next() { if (currentIndex.value >= props.slides.length - 1) { currentIndex.value = 0; } else { currentIndex.value += 1; } } function prev() { if (currentIndex.value <= 0) { currentIndex.value = props.slides.length - 1; } else { currentIndex.value -= 1; } } const itemsToShow = computed(() => [ props.slides[currentIndex.value], ...props.slides.slice(currentIndex.value + 1), ...props.slides.slice(0, currentIndex.value) ]); return { itemsToShow, next, prev, }; }, }); </script> <style scoped> /* 添加一些样式 */ .carousel ul { list-style-type: none; } .slide-enter-active, .slide-leave-active { transition: all 0.5s ease-out; } .slide-enter-from, .slide-leave-to { opacity: 0; transform: translateX(-30%); } </style> ``` 此代码片段展示了如何利用 Vue 3 Composition API 来处理状态逻辑,并通过计算属性动态调整显示哪些滑动条目以实现无缝循环效果[^2]。 #### 使用组件 最后可以在父级组件里引入并注册这个新创建好的无限滚动组件,像这样使用它: ```html <infinite-carousel :slides="[imgSrc1, imgSrc2, imgSrc3]" /> <!-- 或者更复杂的用法 --> <infinite-carousel :slides="productImages"> <template v-slot="{ item }"> <figure> <img :src="item.url"/> <figcaption>{{ item.caption }}</figcaption> </figure> </template> </infinite-carousel> ``` 上述实现了基本功能的一个无限轮播插件,可以根据实际需求进一步扩展其特性,比如自动播放、指示点等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值