diff --git a/assets/index.less b/assets/index.less index fbf302b..3397155 100644 --- a/assets/index.less +++ b/assets/index.less @@ -3,9 +3,13 @@ // color @disabledColor: #e7eaec; +* { + -webkit-tap-highlight-color: rgba(0, 0, 0, 0); //remove tap highlight color for mobile safari +} + .@{prefixClass} { position: relative; - margin: 6px; + margin: 6px auto; height: 4px; width: 100%; border-radius: 2px; @@ -26,7 +30,7 @@ margin-top: -5px; width: 10px; height: 10px; - cursor: default; + cursor: pointer; border-radius: 50%; border: solid 2px #d9d9d9; background-color: #fff; diff --git a/package.json b/package.json index c029aca..51661a7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rc-slider", - "version": "1.2.2", + "version": "1.2.3", "description": "slider ui component for react", "keywords": [ "react", diff --git a/src/Slider.jsx b/src/Slider.jsx index 4a473f2..e0d68f8 100644 --- a/src/Slider.jsx +++ b/src/Slider.jsx @@ -142,6 +142,11 @@ var Slider = React.createClass({ return e.pageX || (e.clientX + document.documentElement.scrollLeft); }, + _getTouchPosition: function (e) { + var touch = e.touches[0]; + return touch.pageX; + }, + _triggerEvents: function(event) { var props = this.props; var hasMarks = props.marks && props.marks.length > 0; @@ -150,17 +155,27 @@ var Slider = React.createClass({ } }, - _addEventHandles: function() { - this._onMouseMoveListener = DomUtils.addEventListener(document, 'mousemove', this._onMouseMove); - this._onMouseUpListener = DomUtils.addEventListener(document, 'mouseup', this._onMouseUp); - }, - - _removeEventHandles: function () { - if (this._onMouseMoveListener) { - this._onMouseMoveListener.remove(); + _addEventHandles: function(type) { + if (type === 'touch') { + // just work for chrome iOS Safari and Android Browser + this._onTouchMoveListener = DomUtils.addEventListener(document, 'touchmove', this._onTouchMove); + this._onTouchUpListener = DomUtils.addEventListener(document, 'touchend', this._onTouchUp); } - if (this._onMouseUpListener) { + if (type === 'mouse') { + this._onMouseMoveListener = DomUtils.addEventListener(document, 'mousemove', this._onMouseMove); + this._onMouseUpListener = DomUtils.addEventListener(document, 'mouseup', this._onMouseUp); + } + }, + + _removeEventHandles: function (type) { + if (type === 'touch') { + this._onTouchMoveListener.remove(); + this._onTouchUpListener.remove(); + } + + if (type === 'mouse') { + this._onMouseMoveListener.remove(); this._onMouseUpListener.remove(); } }, @@ -178,18 +193,37 @@ var Slider = React.createClass({ }); }, - _end: function() { - this._removeEventHandles(); + _end: function(type) { + this._removeEventHandles(type); this.setState(this._triggerEvents.bind(this, 'onAfterChange')); }, _onMouseUp: function() { - this._end(); + this._end('mouse'); + }, + + _onTouchUp: function() { + this._end('touch'); }, _onMouseMove: function(e) { - pauseEvent(e); var position = this._getMousePosition(e); + this._handleMove(e, position); + }, + + _onTouchMove: function(e) { + if (e.touches.length > 1 || (e.type === 'touchend' && e.touches.length > 0)) { + return; + } + + var position = this._getTouchPosition(e); + + this._handleMove(e, position); + }, + + _handleMove: function(e, position) { + pauseEvent(e); + // var position = this._getMousePosition(e); var props = this.props; var state = this.state; @@ -222,6 +256,18 @@ var Slider = React.createClass({ }); }, + handleTouchStart: function(e) { + if (this.props.disabled || e.touches.length > 1 || (e.type === 'touchend' && e.touches.length > 0)) { + return; + } + + var position = this._getTouchPosition(e); + this.startPosition = position; + this._start(position); + this._addEventHandles('touch'); + pauseEvent(e); + }, + handleMouseDown: function() { return (e) => { if (this.props.disabled) { @@ -229,7 +275,7 @@ var Slider = React.createClass({ } var position = this._getMousePosition(e); this._start(position); - this._addEventHandles(); + this._addEventHandles('mouse'); pauseEvent(e); }; }, @@ -243,7 +289,7 @@ var Slider = React.createClass({ () => { this._triggerEvents('onChange'); this._start(position); - this._addEventHandles(); + this._addEventHandles('mouse'); } ); pauseEvent(e); @@ -344,7 +390,8 @@ var Slider = React.createClass({ ref = "handle" style = {handleStyle} href = "#" - onMouseDown={this.handleMouseDown}> + onMouseDown = {this.handleMouseDown} + onTouchStart = {this.handleTouchStart}> ); },