uni-app(vue3)动态获取swiper的区域高度以及通过scroll-view实现区域滚动和scroll-view的置顶功能

计算方式:

swiper高度 = page高度 - tabs高度 - search高度

具体实现:

html 复制代码
      <swiper class="swiper-box" :duration="500" @change="changeSwiper" 
              :current="currentSwiperIndex" :style="'height:' + swiperHeight + 'px'">
        <swiper-item class="swiper-item">
          <scroll-view scroll-y="true" :show-scrollbar="false" 
                      :style="'height:' + swiperHeight + 'px'" 
                      @scrolltolower="lower" :scroll-top="scrollTop" @scroll="scroll">
            <Recommend :openUpdateFlag="openUpdateFlag" @closeUpdateFlag="handleDataUpdate" />
          </scroll-view>
        </swiper-item>
      </swiper>
    </view>
    <!-- 置顶 -->
    <view class="topIcon" v-if="isShowArrowUpward" @click="goScrollTop">
      <up-icon name="arrow-upward" color="#fff" size="28"></up-icon>
    </view>
  • swiperHeight:计算swiper动态高度
  • :scroll-top: 用于重置"scroll"高度
  • @scroll: 获取"scroll"滚动后的信息
  • @scrolltolower: 触底后自动触发,用于异步请求以加载更多数据
  • :show-scrollbar: 不显示滚动条

注意: scroll-view同样需要计算区域高度

js 复制代码
const isShowArrowUpward = ref(false); // 置顶图标
const swiperHeight = ref(0); // swiper高度
const tmpScrollHeight = ref(0); // 记录scroll临时高度
const scrollTop = ref(0); // 重置scroll高度

计算swiper的高度

js 复制代码
// 计算swiper的高度
onMounted(() => {
  let headerSearchHeight,
    headerTabsHeight = 0;
  // 头部搜索height: header-container
  let headerSearchView = uni.createSelectorQuery().select('.header-container');
  headerSearchView
    .boundingClientRect((data) => {
      headerSearchHeight = data.height;
    })
    .exec();
  // 顶部tabs-height:
  let headerTabsView = uni.createSelectorQuery().select('.u-tabs');
  headerTabsView
    .boundingClientRect((data) => {
      headerTabsHeight = data.height;
    })
    .exec();
  // swiper-height
  uni.getSystemInfo({
    success: (res) => {
      swiperHeight.value = res.windowHeight - headerSearchHeight - headerTabsHeight;
    }
  });
});

注意: 需要在dom渲染完成后(onMounted、onReady)才能获取到高度值

js 复制代码
/**
 * scroll-view 是区域滚动,所以无法去监听屏幕滚动
 */
const scroll = (e) => {
  // 记录"scroll-view"临时滚动的高度
  tmpScrollHeight.value = e.detail.scrollTop;
  if (e.detail.scrollTop > 400) {
    // 显示"置顶"图标 
    isShowArrowUpward.value = true;
  } else {
    isShowArrowUpward.value = false;
  }
};

scroll-view置顶(方案一: 无动画)

js 复制代码
// scroll-view高度置顶
const goScrollTop = () => {
  // 解决view层不同步的问题
  scrollTop.value = tmpScrollHeight.value;
  // 强制刷新
  nextTick(() => {
    scrollTop.value = 0;
  });
};

scroll-view置顶(方案二: 添加过渡动画)

JavaScript 模拟平滑滚动(适用于所有平台,包括微信小程序)‌

js 复制代码
const scrollDuration = 300; // 动画时长(ms)

const goScrollTop = () => {
  // 解决view层不同步的问题
  scrollTop.value = tmpScrollHeight.value;
  const startTime = Date.now();
  const startTop = scrollTop.value;
  const distance = startTop; // 需要滚动的距离

  const scrollStep = () => {
    const currentTime = Date.now();
    const timeElapsed = currentTime - startTime;
    const progress = Math.min(timeElapsed / scrollDuration, 1); // 0~1

    // 使用缓动函数(如 easeOutQuad)
    const easeProgress = 1 - Math.pow(1 - progress, 2);
    scrollTop.value = startTop - distance * easeProgress;

    if (timeElapsed < scrollDuration) {
      requestAnimationFrame(scrollStep);
    } else {
      // 强制刷新
      nextTick(() => {
        scrollTop.value = 0; // 确保最终位置准确
      });
    }
  };
  requestAnimationFrame(scrollStep);
};
css 复制代码
.topIcon {
  position: fixed;
  bottom: 120rpx;
  right: 30rpx;
  width: 44rpx;
  height: 44rpx;
  background-color: rgb(0, 0, 0, 0.5);
  border-radius: 40rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}
相关推荐
2501_9159184144 分钟前
苹果上架 iOS 应用的工程实践,一次从零到上线的完整记录
android·ios·小程序·https·uni-app·iphone·webview
2501_915918413 小时前
如何解析iOS崩溃日志:从获取到符号化分析
android·ios·小程序·https·uni-app·iphone·webview
且白17 小时前
uniapp离线打包问题汇总
uni-app
巴啦啦臭魔仙17 小时前
uniapp scroll-view自定义下拉刷新的坑
前端·javascript·uni-app
00后程序员张17 小时前
Swift 应用加密工具的全面方案,从源码混淆到 IPA 成品加固的多层安全实践
安全·ios·小程序·uni-app·ssh·iphone·swift
小禾青青18 小时前
在uniapp中使用pinia
uni-app
fakaifa20 小时前
【全开源】智慧共享农场源码独立版+uniapp前端
前端·uni-app·智慧农场·源码下载·智慧农场小程序·智慧共享农场
toooooop820 小时前
uniapp多个页面监听?全局监听uni.$emit/$on
前端·javascript·uni-app
骨子里的偏爱20 小时前
【案例】uniapp实现内部信息与外部的html网页双向通信的完整的过程,附加完整的代码部分
前端·uni-app·html