vue2中实现列表垂直滚动

由于近期公司项目需要做大屏开发 需要用到垂直滚动 先写一下示例代码 后期在进行优化
在这里插入图片描述
先上代码子组件

在这里插入代码片<template>
  <div class="project">
    <div class="scroll">
      <div
        :class="len === 1 ? 'item_list' : 'item_list am'"
        :style="{ top: top }"
      >
        <div
          class="item"
          v-for="(item, index) in dataList"
          :key="index"
          @mouseleave="move"
          @mouseenter="seup"
          @mousemove="moves"
        >
          我的{{ item.name }} 点赞{{ item.id }}
        </div>
      </div>
    </div>
  </div>
</template>
<script>
function throttle(time, fn) {
  let times = null
  return function () {
    let that = this
    let ars = arguments
    if (times) {
      clearTimeout(times)
    }
    times = setTimeout(() => {
      times = null
      fn.apply(that, ars)
    }, time)
  }
}
export default {
  name: 'itemlist',
  props: {
    data: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      len: 1,
      time: null,
    }
  },
  computed: {
    dataList() {
      return JSON.parse(JSON.stringify(this.data))
    },
    top() {
      return -this.len * 45 + 'px'
    },
  },
  methods: {
    moves: throttle(1500, function () {
      this.setTop()
    }),
    move(e) {},
    seup(e) {},
    setTop() {},
    getScorll() {
      this.time = setInterval(() => {
        if (this.dataList.length - 1 > this.len) {
          this.len++
        } else {
          this.len = 1
        }
      }, 1500)
    },
  },
  mounted() {
    this.getScorll()
  },
}
</script>
<style lang="scss" scoped>
.project {
  padding: 15px;
  /* margin: 35px 15px; */
  background: #0000cd;
  height: 100%;
  display: flex;
}
.scroll {
  flex: 1;
  /* width: 100%; */
  height: 400px;
  overflow: auto;
  margin: 0 auto;
  border: 1px solid #fff;
  padding: 0 10px;
  position: relative;
  .item_list {
    position: absolute;
    top: -100px;
    width: 100%;
  }
  .am {
    transition: all ease-out 0.3s;
  }
  .item {
    height: 45px;
    line-height: 45px;
    font-size: 13px;
    color: #fff;
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  }
  .item:hover {
    background: #eee;
    color: #19197d;
  }
}
</style>

父组件

在这里插入代码片
<template>
  <div class="project">
    <div style="width: 100%">
      <itemList :data="dataList"> </itemList>
    </div>
  </div>
</template>
<script>
import itemList from './component/itemlist.vue'
export default {
  components: {
    itemList,
  },
  name: 'list',
  data() {
    return {
      len: 1,
      time: null,
    }
  },
  computed: {
    dataList() {
      let list = []
      for (let index = 0; index < 30; index++) {
        list.push({ name: `${index}奥林老师`, id: index })
      }
      return list
    },
  },
  methods: {},
  mounted() {},
}
</script>
<style lang="scss" scoped>
.project {
  padding: 15px;
  /* margin: 35px 15px; */
  background: #0000cd;
  height: 100%;
  display: flex;
}
</style>

借鉴网友的无缝滚动
在这里插入图片描述

由于还在实例阶段 后期准备优化为无缝滚动
码云: https://gitee.com/db121/vue_admin
github: https://github.com/121066/vue-admin-pc

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值