手机浏览器下better-scroll中click事件,链接无效的问题

本文介绍如何解决在Vue.js项目中使用Better Scroll时遇到的点击穿透问题,通过设置tap为true来启用tap事件,从而避免点击div内的a链接或click事件失效的情况。

现在大多数vuejs . 项目都使用better-scroll

但是大多数出现一个问题,就是在大的div里面的a 链接或者click事件都不用了

这是 html 部分

 <div ref="wrapper" class="list-wrapper">
      <div class="setting-list" ref="setting" >
        <div class="setting-list-li" v-for="(value,key) in theme">
          <router-link :to="`/theme/${value.c_type}`" @click.native="change" class="item">
          </router-link>
        </div>
      </div>
    </div>

这是js部分

 import BScroll from 'better-scroll'

  export default {
    mounted () {
      this.$nextTick(function () {
        setTimeout(() => {
          const options = {
            scrollY: false, // 因为scrollY默认为true,其实可以省略
            scrollX: true,
            mouseWheel: true,
            click: true,
            taps: true
          }
          this.scroll = new BScroll(this.$refs.wrapper, options)
        }, 20)
      })
    }
  }

因为 better-scroll 会阻止原生的 click 事件,我们可以设置 tap 为 true,它会在区域被点击的时候派发一个 tap 事件

更多参数可以看下这里:文档
https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/options.html#starty

<template> <!-- 品牌馆 --> <view class="outBox"> <s-layout title="" navbar="inner"> <view class="brand"> <view class="brand-header" :style="{ backgroundImage: `url('${headerImg}')` }"> <view class="header-box1 flex jus-con center column"> <image class="box1-img" :src="headerTitle"></image> <view class="box1-text">严选品牌好货</view> </view> <view class="main-search flex center"> <image :src="headerSearch" class="search-icon" @click="searchInfoClick"></image> <view class="search-input flex center"> <input class="uni-input" placeholder="请输入品牌名称查找搜索" :value="inputClearValue" @input="clearInput" placeholder-style="color: #baaf9fc9;" /> <uni-icons type="clear" color="#dbdbdb" size="26" v-if="showClearIcon" @click="clearIcon" ></uni-icons> </view> <view class="search-text flex center jus-con" @click="searchInfoClick">搜索</view> </view> </view> <view class="brand-main"> <view class="main-nav flex"> <view class="inner-box" v-for="(item, index) in mainNavTitle" :key="index" @click="qiehuanNav(index)" :class="item.inline ? 'innerBoxActive' : ''" > {{ item.title }} </view> <!-- <SuTab v-model="activeTab" :tab="mainNavTitle" scroll :titleStyle="{ activeBg: '#734c2f', activeColor: '#FFF', color: '#fee4bf' }" @change="handleTabChange" /> --> </view> <view class="main-content"> <scroll-view scroll-y @scroll="handleScrollClick" style="height: 60vh"> <view class="content-box" v-for="value in 4"> <view class="conBox-title flex center"> <view class="title-left flex center"> <image class="leftImg" src="#"></image> <view class="leftText-box"> <view class="box-text1">良品铺子官方旗舰店</view> <view class="tuike flex center"> <view class="pinpai">品牌自营</view> <view>推客专享</view> </view> </view> </view> <view class="title-right flex center jus-con"> <view class="right-text1">进店</view> <uni-icons type="right" size="14" color="#fff"></uni-icons> </view> </view> <view class="conBox-swiper"> <swiper class="swiper flex" circular :autoplay="autoplay" :interval="interval" :duration="duration" > <swiper-item class="swiper-item" v-for="item in 4"> <view class="item-inner"> <image src="#" class="inner-img"></image> <view class="inner-text flex column"> <view class="inner-top">【良品铺子】猪肉脯肉干高蛋白肉脯靖江特...</view> <view class="inner-bottom"> <view class="bottom-textBox1 flex center"> <view class="textBox1-money">¥</view> <view>39.90</view> </view> <view class="flex center"> <view class="bottom-textBox2">预估返</view> <view class="bottom-textBox3">¥3.20</view> </view> </view> </view> </view> </swiper-item> </swiper> </view> </view> </scroll-view> </view> </view> </view> </s-layout> </view> </template> <script setup> import { ref } from 'vue'; import SuTab from '../../sheep/ui/su-tab/su-tab.vue'; const headerImg = ref('/static/img/brand/header-bg.png'); const headerTitle = ref('/static/img/brand/header-title.png'); const headerSearch = ref('/static/img/brand/search.png'); const inputClearValue = ref(''); const showClearIcon = ref(false); const clearInput = (e) => { inputClearValue.value = e.detail.value; if (e.detail.value.length > 0) { showClearIcon.value = true; } else { showClearIcon.value = false; } }; const clearIcon = () => { inputClearValue.value = ''; showClearIcon.value = false; }; const searchInfoClick = () => { console.log('点击了搜索'); }; const currentNavIndex = ref(null); const mainNavTitle = ref([ { title: '全部', inline: true }, { title: '个护清洁', inline: false }, { title: '美食饮品', inline: false }, { title: '服装配饰', inline: false }, { title: '运动', inline: false }, ]); // 标签切换回调 const activeTab = ref(0); const handleTabChange = (e) => { console.log('选中标签索引:', e.index, '标签数据:', e.data); currentNavIndex.value = e.index; }; const handleScrollClick = () => { console.log('滚动'); }; const qiehuanNav = (val) => { mainNavTitle.value.forEach((item) => { item.inline = false; }); mainNavTitle.value[val].inline = true; }; </script> <style lang="scss" scoped> .outBox { width: 100%; height: 100vh; overflow: hidden; position: relative; .flex { display: flex; } .center { align-items: center; } .jus-con { justify-content: center; } .column { flex-direction: column; } :deep(.page-body) { width: 100%; height: 99.7vh; background: #230a00 !important; } .brand { height: 88.5vh; .brand-header { width: 750rpx; height: 800rpx; position: absolute; top: 0; left: 0; background-repeat: no-repeat; background-size: 100% 100%; .header-box1 { width: 424rpx; height: 190rpx; margin: 190rpx 162rpx 0 164rpx; .box1-img { width: 100%; height: 136rpx; object-fit: cover; } .box1-text { font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 32rpx; color: #fee4bf; text-align: left; font-style: normal; text-transform: none; margin-top: -30rpx; } } .main-search { width: 694rpx; height: 56rpx; border-radius: 36rpx; margin: 95rpx 28rpx 0; border: 1px solid #fee4bfb3; .search-icon { width: 36rpx; height: 36rpx; margin: 18rpx 0rpx 18rpx 42rpx; } .search-input { width: 62vw; height: 44rpx; font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 28rpx; color: #666666; text-align: left; font-style: normal; text-transform: none; margin: 0 16rpx; .uni-input { width: 100%; height: 100%; color: #baaf9f; } } .search-text { width: 136rpx; height: 100%; border-radius: 36rpx; background-color: #fff2e0; font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 28rpx; color: #ff9600; text-align: left; font-style: normal; text-transform: none; } } } .brand-main { position: absolute; top: 560rpx; left: 28rpx; z-index: 1; height: 65vh; .main-nav { width: 95.4vw; height: 56rpx; overflow-x: auto; white-space: nowrap; -webkit-overflow-scrolling: touch; // :deep(.ui-tab.ui-tab-scrolls .ui-tab-scroll-warp .ui-tab-scroll) { // line-height: 0 !important; // } // :deep(.ui-tab-item) { // padding: 0rpx 1rpx !important; // } // :deep(.ui-tab-item .ui-tab-text) { // margin-right: 16rpx; // padding: 8rpx 24rpx; // box-sizing: border-box; // border-radius: 70rpx; // background-color: #562c1599; // font-family: PingFang SC, PingFang SC; // font-weight: 400; // font-size: 28rpx; // color: #fee4bf; // text-align: center; // font-style: normal; // text-transform: none; // } .inner-box { margin-right: 16rpx; padding: 8rpx 24rpx; box-sizing: border-box; border-radius: 70rpx; margin-right: 16rpx; background-color: #562c1599; font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 28rpx; color: #fee4bf; text-align: center; font-style: normal; text-transform: none; } .innerBoxActive { color: #ffffff; background-color: #734c2f; } } .main-content { margin-top: 20rpx; margin-bottom: 16rpx; height: 60vh; overflow-y: auto; .content-box { width: 694rpx; height: auto; border-radius: 32rpx; background-color: #fde3be; padding: 32rpx 0 32rpx 32rpx; box-sizing: border-box; margin-bottom: 32rpx; .conBox-title { margin-bottom: 30rpx; margin-right: 32rpx; justify-content: space-between; .title-left { .leftImg { width: 96rpx; height: 96rpx; object-fit: cover; border-radius: 50%; border: 1px solid red; margin-right: 8rpx; } .leftText-box { .box-text1 { font-family: PingFang SC, PingFang SC; font-weight: 500; font-size: 32rpx; color: #000000; text-align: center; font-style: normal; text-transform: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; width: 300rpx; margin-bottom: 8rpx; } .tuike { font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 24rpx; color: #734c2f; text-align: center; font-style: normal; text-transform: none; .pinpai { margin-right: 8rpx; } } } } .title-right { width: 130rpx; height: 56rpx; border-radius: 28rpx; background: #734c2f; margin-top: 20rpx; .right-text1 { margin-right: 2rpx; margin-left: 10rpx; font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 28rpx; color: #ffffff; text-align: left; font-style: normal; text-transform: none; } } } .conBox-swiper { height: 372rpx; .swiper { height: 100%; overflow: hidden; .swiper-item { width: 236rpx !important; height: 100%; .item-inner { margin-right: 16rpx; width: 220rpx; height: 100%; border-radius: 16rpx; .inner-img { border-radius: 16rpx 16rpx 0 0; width: 220rpx; height: 220rpx; object-fit: cover; background-color: pink; } .inner-text { padding: 4rpx 12rpx 8rpx 10rpx; box-sizing: border-box; justify-content: space-between; background-color: #fff; margin-top: -8rpx; height: 142rpx; border-radius: 0 0 16rpx 16rpx; .inner-top { width: 100%; font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 20rpx; color: #333333; text-align: left; font-style: normal; text-transform: none; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; } .inner-bottom { margin-top: 8rpx; .bottom-textBox1 { font-family: PingFang SC, PingFang SC; font-weight: 600; font-size: 20rpx; color: #f00403; text-align: left; font-style: normal; text-transform: none; .textBox1-money { margin-top: 5rpx; } } .bottom-textBox2 { padding: 4rpx; box-sizing: border-box; border-radius: 8rpx; background-color: #fe294c; font-family: PingFang SC, PingFang SC; font-weight: 500; font-size: 16rpx; color: #ffffff; text-align: left; font-style: normal; text-transform: none; } .bottom-textBox3 { width: 67rpx; height: 28rpx; background: #ffe5e9; border-radius: 8rpx; font-family: PingFang SC, PingFang SC; font-weight: 500; font-size: 20rpx; color: #fe294c; text-align: left; font-style: normal; text-transform: none; } } } } } } } } } } } } </style> 为什么中间的 SuTab的滚动为什么不生效
最新发布
09-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿冰介

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值