NervJS / taro

开放式跨端跨框架解决方案,支持使用 React/Vue/Nerv 等框架来开发微信/京东/百度/支付宝/字节跳动/ QQ 小程序/H5/React Native 等应用。 https://taro.zone/
https://docs.taro.zone/
Other
35.38k stars 4.78k forks source link

swiper组件的onclick事件的问题 #3230

Closed xxyGoTop closed 4 years ago

xxyGoTop commented 5 years ago

问题描述 [问题描述:站在其它人的角度尽可能清晰地、简洁地把问题描述清楚]

以下问题都出在h5上,h5上在使用swiper绑定onClick事件的时候出现了以下问题:

  1. swiperItem上绑定onClick事件无效
  2. 如果在swiperIitem的children组件上绑定onClick事件,左滑第一个item的click事件失效(奇怪的问题)
  3. 如果使用 onAnimationfinish获取current,事件绑定到swiper的父级,swiper的切换下标有问题

复现步骤

  1. 如果在swiperIitem的children组件上绑定onClick事件,左滑第一个item的click事件失效
    <View>
    <Swiper
    className='home-banner__swiper'
    circular
    autoplay
    interval='3000'
    >
    <SwiperItem>
      <View onClick={ () => console.log(1) }>
        <Image className='home-banner__img' src={formatCdnImgPath('home_banner')}></Image>
      </View>
    </SwiperItem>
    <SwiperItem>
      <View>
        <Image className='home-banner__img' src={formatCdnImgPath('home_banner3')}></Image>
      </View>
    </SwiperItem>
    <SwiperItem>
      <View>
        <Image className='home-banner__img' src={formatCdnImgPath('home_banner2')}></Image>
      </View>
    </SwiperItem>
    </Swiper>
    </View>
  2. swiperItem上绑定onClick事件无效
<View>
  <Swiper
    className='home-banner__swiper'
    circular
    autoplay
    interval='3000'
  >
    <SwiperItem onClick={ () => console.log(1) }>
      <View>
        <Image className='home-banner__img' src={formatCdnImgPath('home_banner')}></Image>
      </View>
    </SwiperItem>
    <SwiperItem>
      <View>
        <Image className='home-banner__img' src={formatCdnImgPath('home_banner3')}></Image>
      </View>
    </SwiperItem>
    <SwiperItem>
      <View>
        <Image className='home-banner__img' src={formatCdnImgPath('home_banner2')}></Image>
      </View>
    </SwiperItem>
  </Swiper>
</View>

// 提供完整可复现的代码和整理好代码格式,有助于我们快速定位问题,节省你我时间。 // 代码提供不全或代码格式混乱的 issues 【有可能被忽略】,烦请各位注意。

期望行为 可以绑定onclick事件

报错信息

系统信息

Taro v1.2 及以上版本已添加 taro info 命令,方便大家查看系统及依赖信息,运行该命令后将结果贴下面即可。

补充信息 [可选] [根据你的调查研究,出现这个问题的原因可能在哪里?] 解决方案: 在源码上添加onClick事件,添加在swiper事件上, 代码如下:

on: {
  slideChange () {
    let e = new TouchEvent('touchend')
    Object.defineProperty(e, 'detail', {
      enumerable: true,
      value: {
        current: this.realIndex
      }
    })
    onChange && onChange(e)
  },
  transitionEnd () {
    let e = new TouchEvent('touchend')
    Object.defineProperty(e, 'detail', {
      enumerable: true,
      value: {
        current: this.realIndex
      }
    })
    onAnimationfinish && onAnimationfinish(e)
  },
  onClik() {
    let e = new TouchEvent('touchend')
    Object.defineProperty(e, 'detail', {
      enumerable: true,
      value: {
        current: this.realIndex
      }
    })
    onClik && onClik(e)
  }
}
taro-bot[bot] commented 5 years ago

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

taro-bot[bot] commented 5 years ago

CC @jinjinjin0731

yodagg commented 5 years ago

同样有这个问题,由于开启了衔接模式,swiper克隆了头尾节点,但是无法克隆事件。 导致克隆的SwiperItem子组件事件失效,感觉这个问题会很难处理。

ghyzx commented 5 years ago

同样有这个问题,H5模式下,iOS手机上倒着滑(从左向右滑,滑到上一个)第一个不响应点击事件,Android手机上有响应。

CraKeyBoy commented 5 years ago

这个问题也该修复一下了吧。。H5 确实不行。

fengyun2 commented 5 years ago

taro: 1.3.4/1.3.15版本里,我也在h5上遇到这个问题了,官方不修复一下吗?

onegaop commented 1 year ago

三年了,开启 circular 问题依然存在