前端滚动组件分享

本文介绍了一个前端开发中常用的卡片列表滚动组件,具有鼠标悬停暂停滚动和移开恢复滚动的功能。提供了组件的源码示例以及如何在项目中使用的指南,同时列出了关键属性的说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一个前端可视化常用的卡片列表滚动组件,常用于可视化项目左右两侧的卡片列表的滚动。效果如下图所示:

在这里插入图片描述

组件描述
  1. 当鼠标移入滚动区域时,滚动行为停止当鼠标再次离开时,滚动继续
源码展示
<template>
  <div ref="moocBox" class="custom-scroll-content" @mouseout="mouseOut" @mouseover="mouseOver">
    <slot></slot>
  </div>
</template>
<script>
export default {
  name: 'CustomScroll',
  data() {
    return {
      speed: 50,
      myScroll: null
    };
  },
  created() {
  },
  mounted() {
    this.myScroll = setInterval(() => {
      this.scrollUp();
    }, this.speed);
  },
  destroyed() {
    clearInterval(this.myScroll);
    //  removeEventListener("scroll", this.myScroll);
  },
  beforeDestoryed() {
    clearInterval(this.myScroll);
    this.myScroll = null
  },
  methods: {
    /**
     * @description: 滚动
     * @return void 滚动
     */
    scrollUp() {
      this.$refs.moocBox.scrollTop += 1;
      // 判断滚动条是否到底
      if (this.$refs.moocBox.scrollTop + this.$refs.moocBox.clientHeight >= this.$refs.moocBox.scrollHeight) {
        this.$refs.moocBox.scrollTop = 0;
      }
    },
    /**
     * @description: 鼠标滑过暂停滚动
     * @return void 清除定时器,实现暂停
     */
    mouseOver() {
      clearInterval(this.myScroll);
    },
    /**
     * @description: 鼠标移开重新滚动
     * @return void 设定定时器,实现滚动
     */
    mouseOut() {
      this.myScroll = setInterval(() => {
        this.scrollUp();
      }, this.speed);
    },
  }
};
</script>
<style>
.custom-scroll-content {
  height: 100%;
  overflow: auto;
}
</style>

使用指南
<template>
  <div class="home">
    <div class="container__div_height-500">
      <CustomScroll>
        <div class="item__div_height-100" v-for="i in 10" :key="i">{{ i+1 }}</div>
      </CustomScroll>
    </div>
  </div>
</template>
import CustomScroll from '../components/customScroll.vue';

export default {
  name: 'Home',
  components: {
    CustomScroll,
  },
}
</script>
<style>
.home {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.container__div_height-500 {
  height: 500px;
  width: 400px;
}
.item__div_height-100 {
  height: 100px;
  width: 100%;
  background-color: pink;
  margin-bottom: 12px;
  cursor: pointer;
}
.item__div_height-100:last-child {
  margin-bottom: 0;
}
::-webkit-scrollbar {
  width: 0!important;
}
</style>
属性说明
属性属性值类型
speed设置滚动速度,默认50Number
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值