vue3写移动端无滚动条上下滑动,以及滚动到底部判断

1、在父元素设置宽度,最大高度,隐藏滚动条,子元素是滚动内容

<template>
   <div class="mail-container">
       <div class="mail-card-content">
            展示内容----
       </div>
       <div class="mail-card-content">
            展示内容----
       </div>
       <div class="mail-card-content">
            展示内容----
       </div>
   </div>
</template>

<style>
.mail-container {
  width: 100%;
  max-height: 200px;
  box-sizing: border-box;
  padding: 0.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
 /* 纵向滚动,隐藏滚动条 */
  overflow: auto;
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE10+ */
}
.mail-container::-webkit-scrollbar {
    display: none; /* ChromeSafari */
  }
.mail-card-content {
  width: 100%;
  height: 100px;
  background-color: #1b583f;
  display: flex;
  margin: 0.5rem 0;
  flex-direction: column;
  box-sizing: border-box;
  padding: 1rem;
}


/* 横向滚动,隐藏滚动条 */
/*
 overflow-x: auto;
 scrollbar-width: none; /* Firefox */
 -ms-overflow-style: none; /* IE10+ */
 &::-webkit-scrollbar {
    display: none; /* ChromeSafari */
 }
*/
</style>

2、判断滚动内容滚到父元素底部提示(提示:样式是scss写的)

<template>
  <div class="mail-container" ref="scrollWrapperRef" @scroll="handleScroll">
    <div style="width: 100%; max-height: 200px">
      <template v-if="listDate.length > 0">
        <div 
         v-for="item in listDate" 
         :key="item.id" 
         class="mail-card-content">
               内容展示区域---
        </div>
      </template>

      <template v-else>
        <div class="mail-no-message">暂无数据</div>
      </template>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
const listDate = ref([
  { id: 1, content: '内容1' },
  { id: 2, content: '内容2' },
  { id: 3, content: '内容3' },
])
const scrollWrapperRef = ref() //滚动区域的父元素
const handleScroll = () => { //滚动触发的事件
  const scrollWrapper = scrollWrapperRef.value // 获取滚动容器
  const { scrollTop, clientHeight, scrollHeight } = scrollWrapper // 获取滚动数据
  console.log('scrollWrapper', scrollTop, clientHeight, scrollHeight)
  // 判断是否滚动到底部
  if (scrollTop + clientHeight >= scrollHeight - 10) {
    // 滚动到底部时触发加载数据的函数
    console.log('滚动到底部啦!!!')
  }
}
</script>

<style scoped lang="scss">
.mail-container {
  width: 100%;
  max-height: 200px;
  box-sizing: border-box;
  padding: 0.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
 /* 隐藏滚动条 */
  overflow: auto;
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE10+ */
  &::-webkit-scrollbar {
    display: none; /* ChromeSafari */
  }
}
.mail-card-content {
  width: 100%;
  height: 100px;
  background-color: #1b583f;
  display: flex;
  margin: 0.5rem 0;
  flex-direction: column;
  box-sizing: border-box;
  padding: 1rem;
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值