📄 slider.js
字号:
x: parseInt(el.offsetWidth/2, 10), y: parseInt(el.offsetHeight/2, 10) }; } }, /** * Locks the slider, overrides YAHOO.util.DragDrop * @method lock */ lock: function() { this.thumb.lock(); this.locked = true; }, /** * Unlocks the slider, overrides YAHOO.util.DragDrop * @method unlock */ unlock: function() { this.thumb.unlock(); this.locked = false; }, /** * Handles mouseup event on the slider background * @method thumbMouseUp * @private */ thumbMouseUp: function() { if (!this.isLocked() && !this.moveComplete) { this.endMove(); } }, /** * Returns a reference to this slider's thumb * @method getThumb * @return {SliderThumb} this slider's thumb */ getThumb: function() { return this.thumb; }, /** * Try to focus the element when clicked so we can add * accessibility features * @method focus * @private */ focus: function() { // Focus the background element if possible var el = this.getEl(); if (el.focus) { try { el.focus(); } catch(e) { // Prevent permission denied unhandled exception in FF that can // happen when setting focus while another element is handling // the blur. @TODO this is still writing to the error log // (unhandled error) in FF1.5 with strict error checking on. } } this.verifyOffset(); if (this.isLocked()) { return false; } else { this.onSlideStart(); return true; } }, /** * Event that fires when the value of the slider has changed * @method onChange * @param {int} firstOffset the number of pixels the thumb has moved * from its start position. Normal horizontal and vertical sliders will only * have the firstOffset. Regions will have both, the first is the horizontal * offset, the second the vertical. * @param {int} secondOffset the y offset for region sliders * @deprecated use instance.subscribe("change") instead */ onChange: function (firstOffset, secondOffset) { /* override me */ }, /** * Event that fires when the at the beginning of the slider thumb move * @method onSlideStart * @deprecated use instance.subscribe("slideStart") instead */ onSlideStart: function () { /* override me */ }, /** * Event that fires at the end of a slider thumb move * @method onSliderEnd * @deprecated use instance.subscribe("slideEnd") instead */ onSlideEnd: function () { /* override me */ }, /** * Returns the slider's thumb offset from the start position * @method getValue * @return {int} the current value */ getValue: function () { return this.thumb.getValue(); }, /** * Returns the slider's thumb X offset from the start position * @method getXValue * @return {int} the current horizontal offset */ getXValue: function () { return this.thumb.getXValue(); }, /** * Returns the slider's thumb Y offset from the start position * @method getYValue * @return {int} the current vertical offset */ getYValue: function () { return this.thumb.getYValue(); }, /** * Internal handler for the slider thumb's onChange event * @method handleThumbChange * @private */ handleThumbChange: function () { var t = this.thumb; if (t._isRegion) { t.onChange(t.getXValue(), t.getYValue()); this.fireEvent("change", { x: t.getXValue(), y: t.getYValue() } ); } else { t.onChange(t.getValue()); this.fireEvent("change", t.getValue()); } }, /** * Provides a way to set the value of the slider in code. * @method setValue * @param {int} newOffset the number of pixels the thumb should be * positioned away from the initial start point * @param {boolean} skipAnim set to true to disable the animation * for this move action (but not others). * @param {boolean} force ignore the locked setting and set value anyway * @return {boolean} true if the move was performed, false if it failed */ setValue: function(newOffset, skipAnim, force) { if (!this.thumb.available) { this.deferredSetValue = arguments; return false; } if (this.isLocked() && !force) { return false; } if ( isNaN(newOffset) ) { return false; } var t = this.thumb; var newX, newY; this.verifyOffset(true); if (t._isRegion) { return false; } else if (t._isHoriz) { this.onSlideStart(); // this.fireEvent("slideStart"); newX = t.initPageX + newOffset + this.thumbCenterPoint.x; this.moveThumb(newX, t.initPageY, skipAnim); } else { this.onSlideStart(); // this.fireEvent("slideStart"); newY = t.initPageY + newOffset + this.thumbCenterPoint.y; this.moveThumb(t.initPageX, newY, skipAnim); } return true; }, /** * Provides a way to set the value of the region slider in code. * @method setRegionValue * @param {int} newOffset the number of pixels the thumb should be * positioned away from the initial start point (x axis for region) * @param {int} newOffset2 the number of pixels the thumb should be * positioned away from the initial start point (y axis for region) * @param {boolean} skipAnim set to true to disable the animation * for this move action (but not others). * @param {boolean} force ignore the locked setting and set value anyway * @return {boolean} true if the move was performed, false if it failed */ setRegionValue: function(newOffset, newOffset2, skipAnim, force) { if (!this.thumb.available) { this.deferredSetRegionValue = arguments; return false; } if (this.isLocked() && !force) { return false; } if ( isNaN(newOffset) ) { return false; } var t = this.thumb; if (t._isRegion) { this.onSlideStart(); var newX = t.initPageX + newOffset + this.thumbCenterPoint.x; var newY = t.initPageY + newOffset2 + this.thumbCenterPoint.y; this.moveThumb(newX, newY, skipAnim); return true; } return false; }, /** * Checks the background position element position. If it has moved from the * baseline position, the constraints for the thumb are reset * @param checkPos {boolean} check the position instead of using cached value * @method verifyOffset * @return {boolean} True if the offset is the same as the baseline. */ verifyOffset: function(checkPos) { var newPos = YAHOO.util.Dom.getXY(this.getEl()); //var newPos = [this.initPageX, this.initPageY]; if (newPos[0] != this.baselinePos[0] || newPos[1] != this.baselinePos[1]) { this.thumb.resetConstraints(); this.baselinePos = newPos; return false; } return true; }, /** * Move the associated slider moved to a timeout to try to get around the * mousedown stealing moz does when I move the slider element between the * cursor and the background during the mouseup event * @method moveThumb * @param {int} x the X coordinate of the click * @param {int} y the Y coordinate of the click * @param {boolean} skipAnim don't animate if the move happend onDrag * @private */ moveThumb: function(x, y, skipAnim) { var t = this.thumb; var self = this; if (!t.available) { return; } // this.verifyOffset(); t.setDelta(this.thumbCenterPoint.x, this.thumbCenterPoint.y); var _p = t.getTargetCoord(x, y); var p = [_p.x, _p.y]; this.fireEvent("slideStart"); if (this.animate && YAHOO.widget.Slider.ANIM_AVAIL && t._graduated && !skipAnim) { // this.thumb._animating = true; this.lock(); // cache the current thumb pos this.curCoord = YAHOO.util.Dom.getXY(this.thumb.getEl()); setTimeout( function() { self.moveOneTick(p); }, this.tickPause ); } else if (this.animate && YAHOO.widget.Slider.ANIM_AVAIL && !skipAnim) { // this.thumb._animating = true; this.lock(); var oAnim = new YAHOO.util.Motion( t.id, { points: { to: p } }, this.animationDuration, YAHOO.util.Easing.easeOut ); oAnim.onComplete.subscribe( function() { self.endMove(); } ); oAnim.animate(); } else { t.setDragElPos(x, y); // this.fireEvents(); this.endMove(); } }, /** * Move the slider one tick mark towards its final coordinate. Used * for the animation when tick marks are defined * @method moveOneTick * @param {int[]} the destination coordinate * @private */ moveOneTick: function(finalCoord) { var t = this.thumb, tmp; // redundant call to getXY since we set the position most of time prior // to getting here. Moved to this.curCoord //var curCoord = YAHOO.util.Dom.getXY(t.getEl()); // alignElWithMouse caches position in lastPageX, lastPageY .. doesn't work //var curCoord = [this.lastPageX, this.lastPageY]; // var thresh = Math.min(t.tickSize + (Math.floor(t.tickSize/2)), 10); // var thresh = 10; // var thresh = t.tickSize + (Math.floor(t.tickSize/2)); var nextCoord = null; if (t._isRegion) { nextCoord = this._getNextX(this.curCoord, finalCoord); var tmpX = (nextCoord) ? nextCoord[0] : this.curCoord[0]; nextCoord = this._getNextY([tmpX, this.curCoord[1]], finalCoord); } else if (t._isHoriz) { nextCoord = this._getNextX(this.curCoord, finalCoord); } else { nextCoord = this._getNextY(this.curCoord, finalCoord); } if (nextCoord) { // cache the position this.curCoord = nextCoord; // move to the next coord // YAHOO.util.Dom.setXY(t.getEl(), nextCoord); // var el = t.getEl(); // YAHOO.util.Dom.setStyle(el, "left", (nextCoord[0] + this.thumb.deltaSetXY[0]) + "px"); // YAHOO.util.Dom.setStyle(el, "top", (nextCoord[1] + this.thumb.deltaSetXY[1]) + "px"); this.thumb.alignElWithMouse(t.getEl(), nextCoord[0], nextCoord[1]); // check if we are in the final position, if not make a recursive call if (!(nextCoord[0] == finalCoord[0] && nextCoord[1] == finalCoord[1])) { var self = this; setTimeout(function() { self.moveOneTick(finalCoord); }, this.tickPause); } else { this.endMove(); } } else { this.endMove(); } //this.tickPause = Math.round(this.tickPause/2); }, /** * Returns the next X tick value based on the current coord and the target coord. * @method _getNextX * @private */ _getNextX: function(curCoord, finalCoord) { var t = this.thumb; var thresh; var tmp = []; var nextCoord = null; if (curCoord[0] > finalCoord[0]) { thresh = t.tickSize - this.thumbCenterPoint.x; tmp = t.getTargetCoord( curCoord[0] - thresh, curCoord[1] ); nextCoord = [tmp.x, tmp.y]; } else if (curCoord[0] < finalCoord[0]) { thresh = t.tickSize + this.thumbCenterPoint.x; tmp = t.getTargetCoord( curCoord[0] + thresh, curCoord[1] ); nextCoord = [tmp.x, tmp.y]; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -