react 手机端touch 图片轮播

import React, { Component, PropTypes } from 'react';

const styles = require('./DoctorSchedulesCard.scss');

let startX;
export default class DoctorSchedulesCard extends Component {
  static propTypes = {
    doctorSchedules: PropTypes.array,
  }

  constructor(props) {
    super(props);
    this.state = {
      touchIndex: 0, // 当前未隐藏div索引值
    };
  }

  componentDidMount() {
    // this.props.loadScheduleByDoctorName();
  }

  componentWillReceiveProps() {
  }

  touchStart(event) {
    startX = event.changedTouches[0].pageX;
  }

  // 因为无论move 还是start 都会触发end事件。所以判断方向就在end事件中
  touchEnd(event, index) {
    event.preventDefault();
    const endX = event.changedTouches[0].pageX;
    if (endX - startX > 20) {
      // alert('点击左');
      this.clickLeft(index);
    } else if (startX - endX > 20) {
      // alert('点击右');
      this.clickRight(index);
    }
  }

  clickLeft(indexParam) {
    const allItemCounts = 3; // 总共轮播个数
    const index = indexParam || this.state.touchIndex;
    console.log(index);
    this.setState({
      touchIndex: index === 0 ? allItemCounts : index - 1,
    });
  }

  clickRight() {
    const allItemCounts = 3; // 总共轮播个数
    const index = this.state.touchIndex;
    this.setState({
      touchIndex: index === allItemCounts ? 0 : index + 1,
    });
  }

  render() {
    const {touchIndex} = this.state;
    return (
      <div className={'list ' + styles.doctorSchedulesCardPage}>
        <div onClick={() => this.clickLeft()}>left</div>
        <div onClick={() => this.clickRight()}>right</div>
        <ul>
          <li onTouchStart={this.touchStart.bind(this)}
            onTouchEnd={(event) => this.touchEnd(event, 0)}
            style={{display: touchIndex === 0 ? 'block' : 'none'}}>第0张</li>
          <li onTouchStart={this.touchStart.bind(this)}
            onTouchEnd={(event) => this.touchEnd(event, 1)}
            style={{display: touchIndex === 1 ? 'block' : 'none'}}>第1张</li>
          <li onTouchStart={this.touchStart.bind(this)}
            onTouchEnd={(event) => this.touchEnd(event, 2)}
            style={{display: touchIndex === 2 ? 'block' : 'none'}}>第2张</li>
          <li onTouchStart={this.touchStart.bind(this)}
            onTouchEnd={(event) => this.touchEnd(event, 3)}
            style={{display: touchIndex === 3 ? 'block' : 'none'}}>第3张</li>
        </ul>
      </div>
    );
  }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值