📄 slider.js
字号:
YAHOO.widget.Slider = function (sElementId, sGroup, oThumb, sType) {if (sElementId) {this.type = sType;this.init(sElementId, sGroup, true);var self = this;this.thumb = oThumb;oThumb.onChange = function () {self.onThumbChange();};this.isTarget = false;this.animate = YAHOO.widget.Slider.ANIM_AVAIL;this.backgroundEnabled = true;this.tickPause = 40;if (oThumb._isHoriz && oThumb.xTicks && oThumb.xTicks.length) {this.tickPause = Math.round(360 / oThumb.xTicks.length);} else {if (oThumb.yTicks && oThumb.yTicks.length) {this.tickPause = Math.round(360 / oThumb.yTicks.length);}}oThumb.onMouseDown = function () {return self.focus();};oThumb.onMouseUp = function () {self.thumbMouseUp();};oThumb.onDrag = function () {self.fireEvents();};oThumb.onAvailable = function () {return self.setStartSliderState();};}};YAHOO.extend(YAHOO.widget.Slider, YAHOO.util.DragDrop);YAHOO.widget.Slider.getHorizSlider = function (sBGElId, sHandleElId, iLeft, iRight, iTickSize) {return new YAHOO.widget.Slider(sBGElId, sBGElId, new YAHOO.widget.SliderThumb(sHandleElId, sBGElId, iLeft, iRight, 0, 0, iTickSize), "horiz");};YAHOO.widget.Slider.getVertSlider = function (sBGElId, sHandleElId, iUp, iDown, iTickSize) {return new YAHOO.widget.Slider(sBGElId, sBGElId, new YAHOO.widget.SliderThumb(sHandleElId, sBGElId, 0, 0, iUp, iDown, iTickSize), "vert");};YAHOO.widget.Slider.getSliderRegion = function (sBGElId, sHandleElId, iLeft, iRight, iUp, iDown, iTickSize) {return new YAHOO.widget.Slider(sBGElId, sBGElId, new YAHOO.widget.SliderThumb(sHandleElId, sBGElId, iLeft, iRight, iUp, iDown, iTickSize), "region");};YAHOO.widget.Slider.ANIM_AVAIL = true;YAHOO.widget.Slider.prototype.setStartSliderState = function () {this.setThumbCenterPoint();this.baselinePos = YAHOO.util.Dom.getXY(this.getEl());this.thumb.startOffset = this.thumb.getOffsetFromParent(this.baselinePos);if (this.thumb._isRegion) {if (this.deferredSetRegionValue) {this.setRegionValue.apply(this, this.deferredSetRegionValue, true);} else {this.setRegionValue(0, 0, true);}} else {if (this.deferredSetValue) {this.setValue.apply(this, this.deferredSetValue, true);} else {this.setValue(0, true, true);}}};YAHOO.widget.Slider.prototype.setThumbCenterPoint = function () {var el = this.thumb.getEl();if (el) {this.thumbCenterPoint = {x:parseInt(el.offsetWidth / 2, 10), y:parseInt(el.offsetHeight / 2, 10)};}};YAHOO.widget.Slider.prototype.lock = function () {this.thumb.lock();this.locked = true;};YAHOO.widget.Slider.prototype.unlock = function () {this.thumb.unlock();this.locked = false;};YAHOO.widget.Slider.prototype.thumbMouseUp = function () {if (!this.isLocked() && !this.moveComplete) {this.endMove();}};YAHOO.widget.Slider.prototype.getThumb = function () {return this.thumb;};YAHOO.widget.Slider.prototype.focus = function () {var el = this.getEl();if (el.focus) {el.focus();}this.verifyOffset();if (this.isLocked()) {return false;} else {this.onSlideStart();return true;}};YAHOO.widget.Slider.prototype.onChange = function (firstOffset, secondOffset) {};YAHOO.widget.Slider.prototype.onSlideStart = function () {};YAHOO.widget.Slider.prototype.onSlideEnd = function () {};YAHOO.widget.Slider.prototype.getValue = function () {return this.thumb.getValue();};YAHOO.widget.Slider.prototype.getXValue = function () {return this.thumb.getXValue();};YAHOO.widget.Slider.prototype.getYValue = function () {return this.thumb.getYValue();};YAHOO.widget.Slider.prototype.onThumbChange = function () {var t = this.thumb;if (t._isRegion) {t.onChange(t.getXValue(), t.getYValue());} else {t.onChange(t.getValue());}};YAHOO.widget.Slider.prototype.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();if (t._isRegion) {return false;} else {if (t._isHoriz) {this.onSlideStart();newX = t.initPageX + newOffset + this.thumbCenterPoint.x;this.moveThumb(newX, t.initPageY, skipAnim);} else {this.onSlideStart();newY = t.initPageY + newOffset + this.thumbCenterPoint.y;this.moveThumb(t.initPageX, newY, skipAnim);}}return true;};YAHOO.widget.Slider.prototype.setRegionValue = function (newOffset, newOffset2, skipAnim) {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;};YAHOO.widget.Slider.prototype.verifyOffset = function () {var newPos = YAHOO.util.Dom.getXY(this.getEl());if (newPos[0] != this.baselinePos[0] || newPos[1] != this.baselinePos[1]) {this.thumb.resetConstraints();this.baselinePos = newPos;return false;}return true;};YAHOO.widget.Slider.prototype.moveThumb = function (x, y, skipAnim) {var t = this.thumb;var self = this;if (!t.available) {return;}t.setDelta(this.thumbCenterPoint.x, this.thumbCenterPoint.y);var _p = t.getTargetCoord(x, y);var p = [_p.x, _p.y];if (this.animate && YAHOO.widget.Slider.ANIM_AVAIL && t._graduated && !skipAnim) {this.lock();setTimeout(function () {self.moveOneTick(p);}, this.tickPause);} else {if (this.animate && YAHOO.widget.Slider.ANIM_AVAIL && !skipAnim) {this.lock();var oAnim = new YAHOO.util.Motion(t.id, {points:{to:p}}, 0.4, YAHOO.util.Easing.easeOut);oAnim.onComplete.subscribe(function () {self.endMove();});oAnim.animate();} else {t.setDragElPos(x, y);this.endMove();}}};YAHOO.widget.Slider.prototype.moveOneTick = function (finalCoord) {var t = this.thumb;var curCoord = YAHOO.util.Dom.getXY(t.getEl());var tmp;var nextCoord = null;if (t._isRegion) {nextCoord = this._getNextX(curCoord, finalCoord);var tmpX = (nextCoord) ? nextCoord[0] : curCoord[0];nextCoord = this._getNextY([tmpX, curCoord[1]], finalCoord);} else {if (t._isHoriz) {nextCoord = this._getNextX(curCoord, finalCoord);} else {nextCoord = this._getNextY(curCoord, finalCoord);}}if (nextCoord) {this.thumb.alignElWithMouse(t.getEl(), nextCoord[0], nextCoord[1]);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();}};YAHOO.widget.Slider.prototype._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 {}}return nextCoord;};YAHOO.widget.Slider.prototype._getNextY = function (curCoord, finalCoord) {var t = this.thumb;var thresh;var tmp = [];var nextCoord = null;if (curCoord[1] > finalCoord[1]) {thresh = t.tickSize - this.thumbCenterPoint.y;tmp = t.getTargetCoord(curCoord[0], curCoord[1] - thresh);nextCoord = [tmp.x, tmp.y];} else {if (curCoord[1] < finalCoord[1]) {thresh = t.tickSize + this.thumbCenterPoint.y;tmp = t.getTargetCoord(curCoord[0], curCoord[1] + thresh);nextCoord = [tmp.x, tmp.y];} else {}}return nextCoord;};YAHOO.widget.Slider.prototype.b4MouseDown = function (e) {this.thumb.autoOffset();this.thumb.resetConstraints();};YAHOO.widget.Slider.prototype.onMouseDown = function (e) {if (!this.isLocked() && this.backgroundEnabled) {var x = YAHOO.util.Event.getPageX(e);var y = YAHOO.util.Event.getPageY(e);this.focus();this.moveThumb(x, y);}};YAHOO.widget.Slider.prototype.onDrag = function (e) {if (!this.isLocked()) {var x = YAHOO.util.Event.getPageX(e);var y = YAHOO.util.Event.getPageY(e);this.moveThumb(x, y, true);}};YAHOO.widget.Slider.prototype.endMove = function () {this.unlock();this.moveComplete = true;this.fireEvents();};YAHOO.widget.Slider.prototype.fireEvents = function () {var t = this.thumb;t.cachePosition();if (!this.isLocked()) {if (t._isRegion) {var newX = t.getXValue();var newY = t.getYValue();if (newX != this.previousX || newY != this.previousY) {this.onChange(newX, newY);}this.previousX = newX;this.previousY = newY;} else {var newVal = t.getValue();if (newVal != this.previousVal) {this.onChange(newVal);}this.previousVal = newVal;}if (this.moveComplete) {this.onSlideEnd();this.moveComplete = false;}}};YAHOO.widget.Slider.prototype.toString = function () {return ("Slider (" + this.type + ") " + this.id);};YAHOO.widget.SliderThumb = function (id, sGroup, iLeft, iRight, iUp, iDown, iTickSize) {if (id) {this.init(id, sGroup);this.parentElId = sGroup;}this.isTarget = false;this.tickSize = iTickSize;this.maintainOffset = true;this.initSlider(iLeft, iRight, iUp, iDown, iTickSize);this.scroll = false;};YAHOO.extend(YAHOO.widget.SliderThumb, YAHOO.util.DD);YAHOO.widget.SliderThumb.prototype.getOffsetFromParent = function (parentPos) {var myPos = YAHOO.util.Dom.getXY(this.getEl());var ppos = parentPos || YAHOO.util.Dom.getXY(this.parentElId);return [(myPos[0] - ppos[0]), (myPos[1] - ppos[1])];};YAHOO.widget.SliderThumb.prototype.startOffset = null;YAHOO.widget.SliderThumb.prototype._isHoriz = false;YAHOO.widget.SliderThumb.prototype._prevVal = 0;YAHOO.widget.SliderThumb.prototype._graduated = false;YAHOO.widget.SliderThumb.prototype.initSlider = function (iLeft, iRight, iUp, iDown, iTickSize) {this.setXConstraint(iLeft, iRight, iTickSize);this.setYConstraint(iUp, iDown, iTickSize);if (iTickSize && iTickSize > 1) {this._graduated = true;}this._isHoriz = (iLeft > 0 || iRight > 0);this._isVert = (iUp > 0 || iDown > 0);this._isRegion = (this._isHoriz && this._isVert);};YAHOO.widget.SliderThumb.prototype.clearTicks = function () {YAHOO.widget.SliderThumb.superclass.clearTicks.call(this);this._graduated = false;};YAHOO.widget.SliderThumb.prototype.getValue = function () {if (!this.available) {return 0;}var val = (this._isHoriz) ? this.getXValue() : this.getYValue();return val;};YAHOO.widget.SliderThumb.prototype.getXValue = function () {if (!this.available) {return 0;}var newOffset = this.getOffsetFromParent();return (newOffset[0] - this.startOffset[0]);};YAHOO.widget.SliderThumb.prototype.getYValue = function () {if (!this.available) {return 0;}var newOffset = this.getOffsetFromParent();return (newOffset[1] - this.startOffset[1]);};YAHOO.widget.SliderThumb.prototype.toString = function () {return "SliderThumb " + this.id;};YAHOO.widget.SliderThumb.prototype.onChange = function (x, y) {};if ("undefined" == typeof YAHOO.util.Anim) {YAHOO.widget.Slider.ANIM_AVAIL = false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -