h5滑动翻页(滚动到下一个元素)vue3.2


<template>
  <div class="page-container" ref="scrollRef" @touchstart="handleTouchStart" @touchend="handleTouchEnd">
    <!-- 页面内容 -->
    <template v-for="i in list" :key="i">
      <div class="" style="width:100%;height:100vh;">
        {{ i }}
      </div>
    </template>
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue';
const startY = ref(0)// 触摸开始的Y坐标
const endY = ref(0)//触摸结束的Y坐标
const threshold = ref(10)//判断为滑动的最小距离
const list = ref([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])//判断为滑动的最小距离
const currentIndex = ref(0)//触摸结束的Y坐标
const scrollRef = ref(null) //ref
const viewportHeightInPx = ref(0)//100vh的高度
onMounted(() => {
  viewportHeightInPx.value = window.innerHeight;

})
function handleTouchStart(e) {
  startY.value = e.touches[0].clientY;
  console.log('开始触摸', startY.value)
}


function handleTouchEnd(e) {
  endY.value = e.changedTouches[0].clientY;
  if (endY.value - startY.value > threshold.value) {
    // 向下滑动
    goToNextPage();
  } else if (startY.value - endY.value > threshold.value) {
    // 向上滑动
    goToPrevPage();
  }
}
function goToNextPage() {
  // 实现翻到下一页的逻辑
  console.log('下滑,翻到下一页');
  currentIndex.value -= 1;
  init()
}

function goToPrevPage() {
  // 实现翻到上一页的逻辑
  console.log('上滑,翻到上一页');
  currentIndex.value += 1;
  init()
}

const init = () => {
  if (currentIndex.value < 0) {
    currentIndex.value = 0
  }
  if (scrollRef.value) {
    scrollRef.value.scrollTo({
      top: (viewportHeightInPx.value * currentIndex.value),
      behavior: 'smooth', // 为了平滑滚动效果
    });
  }
}
</script>

<style>
.page-container {
  height: 100vh;
  /* 确保页面高度占满全屏 */
  overflow-y: auto;
  /* 允许滚动 */

}

.content {
  background: url('@/assets/1/bg.png');
  background-size: cover;
}
</style>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue-awesome-swiper是一个基于Vue.js的插件,用于实现H5滑动翻页效果。它是在swiper的基础上进行了封装,使得在Vue项目中使用swiper变得更加方便和灵活。 要在vue中使用vue-awesome-swiper,首先需要在main.js中引入VueAwesomeSwiper插件,并导入swiper的样式文件。具体的代码如下: import VueAwesomeSwiper from 'vue-awesome-swiper' import 'swiper/dist/css/swiper.css' Vue.use(VueAwesomeSwiper) 这样就可以在Vue组件中使用vue-awesome-swiper来实现H5滑动翻页效果了。你可以在需要使用swiper的组件中引入并注册vue-awesome-swiper的组件,然后使用组件的方式来配置和控制swiper的各种参数和功能。通过配置不同的选项,你可以实现各种不同的滑动翻页效果,满足你的需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue案例 - vue-awesome-swiper实现h5滑动翻页效果](https://blog.csdn.net/weixin_33881050/article/details/93267999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue移动端使用swiper+vue-awesome-swiper实现滑动选择](https://blog.csdn.net/weixin_45842078/article/details/119141775)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值