vue:元素滚动到底部触发事件,隐藏滚动条及滚动条显示和隐藏切换

一、设置一个div元素,绑定滚动事件

在这里插入图片描述

@scroll="scrollEvent"

二、在methods中描述滚动事件
在这里插入图片描述

scrollEvent (e) {
  if (Math.ceil(e.srcElement.scrollTop) + e.srcElement.clientHeight === e.srcElement.scrollHeight) {
    // this.scrollHandler(this.dataWorkQueue[this.clickItemIndex], this.clickItemIndex)
    // 这里可以写滚轮滚到元素底部时触发的操作,示例中触发的是一个methods方法
  }
},

三、设置CSS,让其内容超出高度时存在滚动条(overflow: auto;),然后隐藏滚动条(::-webkit-scrollbar{ display: none; }
在这里插入图片描述

.queue-content {
  height: calc(100vh - 278px);
}
.scroll {
  height: calc(100vh - 285px);
  overflow: auto;
  width: 100%;
}
.scroll::-webkit-scrollbar {
  display: none;
}

四、上述方式,显然还可以优化,即鼠标移上去显示滚动条,鼠标离开隐藏滚动条,参考文章:鼠标事件通过class改变样式

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个 Vue 滚动区域滚动底部触发事件并且节流的 demo: ```vue <template> <div class="scroll-area" ref="scrollArea" @scroll="handleScroll"> <!-- 滚动区域内容 --> </div> </template> <script> export default { data() { return { throttleTimer: null, throttleInterval: 300, // 节流时间间隔 }; }, methods: { handleScroll() { // 滚动区域距离顶部的高度 const scrollTop = this.$refs.scrollArea.scrollTop; // 滚动区域的总高度 const scrollHeight = this.$refs.scrollArea.scrollHeight; // 滚动区域的可见高度 const clientHeight = this.$refs.scrollArea.clientHeight; // 判断是否滚动底部 if (scrollTop + clientHeight >= scrollHeight) { this.throttle(this.handleScrollToBottom); } }, handleScrollToBottom() { // 滚动底部后要执行的操作 }, throttle(func) { if (!this.throttleTimer) { this.throttleTimer = setTimeout(() => { this.throttleTimer = null; func(); }, this.throttleInterval); } }, }, }; </script> ``` 在这个 demo 中,我们使用了 `@scroll` 事件来监听滚动区域的滚动事件。在 `handleScroll` 方法中,我们获取了滚动区域距离顶部的高度、滚动区域的总高度和滚动区域的可见高度,并判断是否滚动底部。如果滚动底部,则调用 `throttle` 方法来执行 `handleScrollToBottom` 方法。 `throttle` 方法是一个简单的节流函数,它会在一定的时间间隔内只执行一次传入的函数。在这个 demo 中,我们使用了 `this.throttleInterval` 来定义节流的时间间隔。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值