vue 走马灯无缝滚动轮播图 vue-seamless-scroll

本文介绍了如何在Vue项目中使用vue-seamless-scroll组件创建无缝滚动效果,包括安装、引入、配置选项以及示例代码。在实际应用中遇到图片抖动的问题,可能是因为滚动速度设置、数据监控或DOM更新导致,解决方案可能涉及调整组件参数或优化数据处理。
摘要由CSDN通过智能技术生成

效果图 GIF软件有点问题出现抖动 (献丑了QAQ)

1. 安装 vue-seamless-scroll

npm install vue-seamless-scroll --save
//或
yarn add vue-seamless-scroll

2. 引入组件 

import vueSeamlessScroll from 'vue-seamless-scroll'

3 引用

// 注册
components: {
        vueSeamlessScroll
}

//使用
<vue-seamless-scroll></vue-seamless-scroll>

4. API

defaultOption() {
      return {
        step: 0.8, // 数值越大速度滚动越快
        limitMoveNum: Math.ceil(this.logos.length / 5), // 开始无缝滚动的数据量 this.dataList.length
        hoverStop: true, // 是否开启鼠标悬停stop
        direction: 1, // 0向下 1向上 2向左 3向右
        openWatch: true, // 开启数据实时监控刷新dom
        singleHeight: 0, // 单步运动停止的高度(默认值0是无缝不停止的滚动) direction => 0/1
        singleWidth: 0, // 单步运动停止的宽度(默认值0是无缝不停止的滚动) direction => 2/3
        waitTime: 1000, // 单步运动停止的时间(默认值1000ms)
      };
    },

5. 示例

页面部分

<vue-seamless-scroll
    ref="scroll"
    :data="logos"
    :class-option="defaultOption"
    style="width:1110px;height:300px;overflow:hidden; margin:0 auto;"
  >
    <ul class="ul-scoll">
      <li
        v-for="(item, index) in Math.ceil(logos.length / 5)"
        :key="index"
      >
        <img
          v-for="(item, i) in logos.slice(5 * index, 5 * (index + 1))"
          :key="i"
          :src="getImg(item)"
          
        />
      </li>
    </ul>
  </vue-seamless-scroll>

代码段

<script>

    import logos from './logos'
    import vueSeamlessScroll from 'vue-seamless-scroll'

    export default {
    components:{
      vueSeamlessScroll
    },
    data () {
      return {
        logos:logos,
      }
    },
    computed:{
      defaultOption() {
        return {
          step: 0.8, // 数值越大速度滚动越快
          limitMoveNum: Math.ceil(this.logos.length / 5), // 开始无缝滚动的数据量 this.dataList.length
          hoverStop: true, // 是否开启鼠标悬停stop
          direction: 1, // 0向下 1向上 2向左 3向右
          openWatch: true, // 开启数据实时监控刷新dom
          singleHeight: 0, // 单步运动停止的高度(默认值0是无缝不停止的滚动) direction => 0/1
          singleWidth: 0, // 单步运动停止的宽度(默认值0是无缝不停止的滚动) direction => 2/3
          waitTime: 1000, // 单步运动停止的时间(默认值1000ms)
        };
      },
    },
    methods: {
      getImg(item) {
          return require(`../assets/index/logos${item.split('logos')[1]}`);
        },
    }
  }
</script>

图片导入方法 logos.js

let requireModule = require.context(
  '../assets/index/logos',
  false,
  /\.png$/,
);
// console.log('len:', requireModule.keys().length, requireModule.keys());
let logos = [];
for (let i = 0; i < requireModule.keys().length; i++) {
  logos.push(
    `../assets/index/logos/${requireModule.keys()[i].split('./')[1]}`,
  );
}

export default logos;
以下是一个手动实现走马灯效果的Vue组件示例: ```vue <template> <div class="carousel"> <div class="carousel-container" :style="{ transform: `translateX(${offset}px)` }"> <div v-for="(item, index) in items" :key="index" class="carousel-item"> {{ item }} </div> </div> <button class="carousel-button prev" @click="prev">Prev</button> <button class="carousel-button next" @click="next">Next</button> </div> </template> <script> export default { data() { return { items: [], // 后端接口返回的数据 offset: 0, // 偏移量 itemWidth: 200, // 每个项目的宽度 visibleItems: 3, // 可见项目的数量 }; }, mounted() { // 调用后端接口获取数据 this.fetchData(); }, methods: { fetchData() { // 调用后端接口获取数据并更新items数组 // 示例代码: // axios.get('/api/data').then(response => { // this.items = response.data; // }); }, prev() { this.offset += this.itemWidth; if (this.offset > 0) { this.offset = -(this.items.length - this.visibleItems) * this.itemWidth; } }, next() { this.offset -= this.itemWidth; if (this.offset < -(this.items.length - this.visibleItems) * this.itemWidth) { this.offset = 0; } }, }, }; </script> <style> .carousel { position: relative; width: 100%; overflow: hidden; } .carousel-container { display: flex; transition: transform 0.5s; } .carousel-item { width: 200px; flex-shrink: 0; } .carousel-button { position: absolute; top: 50%; transform: translateY(-50%); padding: 10px; background-color: #ccc; border: none; color: #fff; cursor: pointer; } .carousel-button.prev { left: 10px; } .carousel-button.next { right: 10px; } </style> ``` 这是一个基本的走马灯组件,它使用了一个容器来包含所有的项目,并通过改变`offset`属性来实现滚动效果。点击"Prev"和"Next"按钮可以切换项目。 请注意,这只是一个示例,你需要根据实际需求进行适当的修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值