react 手机端touch 图片轮播
2016-11-10 15:53
253 查看
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> ); } }
相关文章推荐
- React Native-图片轮播
- 一分钟搞定AlloyTouch图片轮播
- 一分钟搞定AlloyTouch图片轮播
- 一分钟搞定AlloyTouch图片轮播
- 一分钟搞定AlloyTouch图片轮播组件
- 蚂蚁分类信息系统 5.8 修改蚂蚁分类信息系统手机端信息图片浏览方式,自动轮播,带当前图片数字和张数,支持放大浏览
- React Native之如何实现图片轮播效果
- 手机端图片轮播插件
- 一分钟搞定AlloyTouch图片轮播组件
- 移动端使用touchstart,touchmove,touchend实现图片轮播
- 手机端实现Bootstrap简单图片轮播效果
- react 实现手机端首页无缝轮播
- RollViewPager图片轮播效果开源框架使用方法详解
- jQuery实现图片轮播效果(仿QQ商城首页、天猫首页)
- SharePoint 门户网站的图片轮播-页面定制
- 可以通过手势放大缩小图片的TouchImageView
- Jquery代码实现图片轮播效果(一)
- Angular2组件与指令的小实践——实现一个图片轮播组件
- JQuery图片轮播详细说明和实现
- react native 路径对,但读取图片错误问题