Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

H5下image onload 无法获取e.detail相关内容 #4116

Closed
nyrf opened this issue Aug 9, 2019 · 5 comments
Closed

H5下image onload 无法获取e.detail相关内容 #4116

nyrf opened this issue Aug 9, 2019 · 5 comments
Assignees

Comments

@nyrf
Copy link

nyrf commented Aug 9, 2019

问题描述
在小程序里可以直接用e.detail 获取相关内容,转为h5后,e.detail为空

import Taro, { Component } from '@tarojs/taro'
import { View, Swiper, SwiperItem, Image } from '@tarojs/components'
import { imageUrlFormat } from '@/utils'

class Carousel extends Component {
  static defaultProps = {
    slides: [],
    format: 'big'
  }
  state = {
    //默认
    current: 0,
    imgheights: []
  }
  componentWillMount() {}
  imageLoad(e) {
    console.log(e)
    //获取图片真实宽度
    let imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比
      ratio = imgwidth / imgheight
    // console.log(imgwidth, imgheight)
    //计算的高度值
    let viewHeight = 750 / ratio
    imgheight = viewHeight
    let imgheights = this.state.imgheights
    //把每一张图片的对应的高度记录到数组里
    imgheights[e.target.dataset.id] = imgheight
    this.setState({
      imgheights: imgheights
    })
  }

  bindchange(e) {
    this.setState({ current: e.detail.current })
  }
  render() {
    return (
      <View>
        <Swiper
          className='swiper-screen'
          indicatorActiveColor='#ff6600'
          circular
          indicatorDots
          autoplay
          // style='height: auto'
          style={`height: ${this.state.imgheights[this.state.current]}rpx`}
          onChange={this.bindchange}
        >
          {this.props.slides.map((item, index) => {
            return (
              <SwiperItem key={item.id}>
                <Image
                  data-id={index}
                  mode='widthFix'
                  style='width: 100%;'
                  src={imageUrlFormat(item.image, this.props.format)}
                  onLoad={this.imageLoad.bind(this)}
                />
              </SwiperItem>
            )
          })}
        </Swiper>
      </View>
    )
  }
}
export default Carousel

报错信息

Uncaught TypeError: Cannot read property 'width' of undefined
    at Carousel.imageLoad (carousel.js?5095:20)
    at HTMLImageElement.eventHandler (index.esm.js?eb37:594)
···

**系统信息**
❯ taro info
👽 Taro v1.3.11


  Taro CLI 1.3.11 environment info:
    System:
      OS: macOS 10.14.4
      Shell: 3.0.2 - /usr/local/bin/fish
    Binaries:
      Node: 12.6.0 - ~/.config/nvm/12.6.0/bin/node
      Yarn: 1.12.1 - /usr/local/bin/yarn
      npm: 6.9.0 - ~/.config/nvm/12.6.0/bin/npm

@taro-bot
Copy link

taro-bot bot commented Aug 9, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@nyrf nyrf changed the title H5下swiper onload H5下swiper onload 无法获取e.detail相关内容 Aug 9, 2019
@nyrf nyrf changed the title H5下swiper onload 无法获取e.detail相关内容 H5下image onload 无法获取e.detail相关内容 Aug 9, 2019
@taro-bot
Copy link

taro-bot bot commented Aug 11, 2019

CC @jinjinjin0731

@Littly
Copy link
Contributor

Littly commented Aug 11, 2019

有道理,可以加一下。当然更欢迎PR。

@jinjinjin0731
Copy link
Contributor

已修复

@dolphin0618
Copy link

e.detail中的宽高都是0啊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants