vue手写列表竖向滚动

看看效果图

 

最近由于项目需求,需要做一个列表的无线循环滚动,由于我们项目没有用UI库,所以手写了一个列表滚动,不过虽然网上很多,但还是想记录下来,话不多说了,上代码!

样式层

<template>
  <div class="scrolList" ref="box" :style="scrolStyle">
    <div class="top" v-if="showMask"></div> // 这里是加了一个遮罩过度
    <div class="bottom" v-if="showMask"></div>
    <div class="ul anim" ref="ul" :style="ulStyle">
      <template v-for="(item,index) in list">
        <p class="li" ref="li" :key="index">
          <span class="item1">{{ item.name }}</span>
          <span class="item2">{{ item.phone }}</span>
          <span class="item3">{{ item.desc }}</span>
        </p>
      </template>
    </div>
  </div>
</template>

js层

export default {
  props:{
    list: {
      type: Array,
      require: true
    },
    scrolStyle: Object, //可配置容器样式
    showMask: Boolean
  },
  data() {
    return {
      config:{
        height: null
      },
      step: 0,
      liIndex: 0
    }
  },
  mounted() {
    this.initInfo()
    setInterval(() => {
      this.$nextTick(()=>{
        this.scrol()
      })
      this.liIndex++
    }, 500);
  },
  computed: {
    ulStyle() {
      return `height: ${this.config.height}px`
    }
  },
  methods: {
    /**
     * 初始化高度
     */
    initInfo() {
      this.config.height = this.$refs.li[0].clientHeight * this.list.length;
    },
    /**
     * 每次20px滚动
     */
    scrol() {
      this.step += 20
      //循环数据
      this.list.push(this.list[this.liIndex])
      this.$refs.ul.style.transform = `translateY(-${this.step}px)`
    }
  }
}

之前是写在页面里,但想了想还是封装成了组件,便于以后再用

样式层

<style lang="scss">
  .scrolList{
    position: relative;
    overflow: hidden;
    .top{
      position: absolute;
      z-index: 66;
      top: 0;
      height: 0.8rem;
      width: 100%;
      background-image: linear-gradient(to top,rgba(0,0,0,0) 0%,red 50%);;
    }
    .bottom{
      position: absolute;
      bottom: 0;
      height: 0.8rem;
      width: 100%;
      z-index: 66;
      background-image: linear-gradient(to bottom,rgba(0,0,0,0) 0%,red 50%);;
    }
    .ul{
      margin-top: 0.6rem;
      .li{
        display: flex;
        transform: translate(0);
        height: 0.8rem;
        font-size: 0.4rem;
        text-align: left;
        line-height: 0.8rem;
        .item1 {
          flex: 1;
        }
        .item2{
          flex: 3;
        }
        .item3{
          flex: 3;
        }
      }
    }
  }
  .anim {
    transition: all 0.5s linear;
  }
</style>

其实实现的思路有很多,利用margin,position等都可以实现,我利用的是tsranslate,谢谢阅读

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Vue中的竖向无缝滚动轮播图,你可以使用一些Vue插件或自定义组件来实现。下面是一个简单的示例,演示了如何使用Vuevue-awesome-swiper插件来实现这个效果: 首先,确保你已经安装了vue-awesome-swiper插件。你可以通过npm或yarn进行安装: ```bash npm install vue-awesome-swiper --save ``` 然后,在你的Vue组件中,引入vue-awesome-swiper: ```javascript import 'swiper/css/swiper.css'; import VueAwesomeSwiper from 'vue-awesome-swiper'; Vue.use(VueAwesomeSwiper); ``` 接下来,你可以在Vue组件中使用vue-awesome-swiper来创建一个竖向无缝滚动的轮播图: ```html <template> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="news in newsList" :key="news.id"> <!-- 这里是你的新闻内容 --> <h3>{{ news.title }}</h3> <p>{{ news.content }}</p> </div> </div> </div> </template> <script> export default { data() { return { newsList: [ // 这里是你的新闻数据 { id: 1, title: '新闻标题1', content: '新闻内容1' }, { id: 2, title: '新闻标题2', content: '新闻内容2' }, { id: 3, title: '新闻标题3', content: '新闻内容3' }, // ... ] }; } }; </script> <style> .swiper-container { height: 300px; /* 设置轮播图容器的高度 */ } </style> ``` 最后,你可以在组件的生命周期钩子中初始化swiper: ```javascript mounted() { this.$nextTick(() => { new Swiper('.swiper-container', { direction: 'vertical', loop: true, autoplay: { delay: 3000, // 自动切换的间隔时间 disableOnInteraction: false // 用户操作后是否停止自动切换 } }); }); } ``` 上述代码中的swiper-container类名和swiper-wrapper类名是vue-awesome-swiper插件要求的类名,需要保持一致。 希望这个示例能帮助到你实现竖向无缝滚动的轮播图。如果有任何问题,请随时向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值