Vue2 滚动列表

<template>
  <div class="ItemScroll" ref="card">
    <slot></slot>
    <slot></slot>
  </div>
</template>
   
  <script>
export default {
  name: "ItemScroll",
  created() {},
  props: {
    // 控制动画滚动 速度
    speed: {
      default: 1,
      type: Number,
    },
    // 控制 鼠标移入滚动停止功能
    isMouseControl: {
      default: true,
      type: Boolean,
    },
    // 控制 滚动到底部是跳到下一页 还是无缝循环滚动
    isNextPage: {
      default: false,
      type: Boolean,
    },
  },
  mounted() {
    this.start(this.height);
    // 根据props 控制鼠标移入功能
    if (this.isMouseControl) {
      this.setEvet();
    }
  },
  computed: {
    //第一个slot
    ele0() {
      return this.$refs.card.children[0];
    },
    //第二个slot
    ele1() {
      return this.$refs.card.children[1];
    },
    //盒子的可视高度
    cardHeight() {
      return this.$refs.card.clientHeight;
    },
  },
  data() {
    return {
      height: 0,
      isScroll: true,
    };
  },
  methods: {
    //鼠标移入停止滚动 移出继续滚动
    setEvet() {
      this.$refs.card.onmouseenter = () => {
        this.isScroll = false;
      };
      this.$refs.card.onmouseleave = () => {
        this.isScroll = true;
        this.$nextTick(() => {
          this.start(this.height);
        });
      };
    },
    //滚动方法
    start(height) {
      this.ele0.style = `transform:translateY(-${height}px);`;
      this.ele1.style = `height:${this.cardHeight}px;transform:translateY(-${height}px);overflow: hidden;`;
      if (height >= this.ele0.clientHeight) {
        // 判断滚动到底部是跳到下一页 还是无缝循环滚动
        if (this.isNextPage) {
          // 让父组件跳到下一页
          this.$emit("nextPage", "hello");
        } else {
          this.height = 0;
        }
      } else {
        this.height += this.speed;
      }

      if (!this.isScroll) return;
      window.requestAnimationFrame(() => {
        this.start(this.height);
      });
    },
  },
};
</script>
   
  <style lang="less" scoped>
.ItemScroll {
  overflow: hidden;
}
.hover {
  overflow-y: auto;
}
.hide {
  display: none;
}
</style>
<template>
  <div class="scroll">
    <ItemScroll
      class="card"
      :speed="1"
      ref="ItemScroll"
    >
      <div class="list">
        <div class="item" v-for="item in dataList" :key="item.time">
          <span>{{ item.label }}</span>
          <span>{{ item.time }}</span>
        </div>
      </div>
    </ItemScroll>
  </div>
</template>
 
<script>
import ItemScroll from "@/components/ItemScroll";
export default {
  name: "HomeView",
  components: { ItemScroll },
  data() {
    return {
      dataList: [
        {label: "恭喜发财", time: "20240122"},
        {label: "恭喜发财", time: "20240123"},
        {label: "恭喜发财", time: "20240124"},
        {label: "恭喜发财", time: "20240125"},
        {label: "恭喜发财", time: "20240126"},
        {label: "恭喜发财", time: "20240127"},
        {label: "恭喜发财", time: "20240128"},
        {label: "恭喜发财", time: "20240129"},
        {label: "恭喜发财", time: "20240110"},
        {label: "恭喜发财", time: "20240111"},

      ],
      // 控制 切换下一页是丝滑切换数据 还是回到顶部再滚动
      isSmoothSwitch:false
    };
  },
};
</script>
 
<style lang="less" scoped>
.card {
  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>
 
  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钓了猫的鱼儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值