📄 slider.js
字号:
/*Copyright (c) 2006, Yahoo! Inc. All rights reserved.Code licensed under the BSD License:http://developer.yahoo.net/yui/license.txtversion: 0.12.2*//** * The Slider component is a UI control that enables the user to adjust * values in a finite range along one or two axes. Typically, the Slider * control is used in a web application as a rich, visual replacement * for an input box that takes a number as input. The Slider control can * also easily accommodate a second dimension, providing x,y output for * a selection point chosen from a rectangular region. * * @module slider * @title Slider Widget * @namespace YAHOO.widget * @requires yahoo,dom,dragdrop,event * @optional animation *//** * A DragDrop implementation that can be used as a background for a * slider. It takes a reference to the thumb instance * so it can delegate some of the events to it. The goal is to make the * thumb jump to the location on the background when the background is * clicked. * * @class Slider * @extends YAHOO.util.DragDrop * @uses YAHOO.util.EventProvider * @constructor * @param {String} id The id of the element linked to this instance * @param {String} sGroup The group of related DragDrop items * @param {SliderThumb} oThumb The thumb for this slider * @param {String} sType The type of slider (horiz, vert, region) */YAHOO.widget.Slider = function(sElementId, sGroup, oThumb, sType) { if (sElementId) { this.init(sElementId, sGroup, true); this.initSlider(sType); this.initThumb(oThumb); }};/** * Factory method for creating a horizontal slider * @method YAHOO.widget.Slider.getHorizSlider * @static * @param {String} sBGElId the id of the slider's background element * @param {String} sHandleElId the id of the thumb element * @param {int} iLeft the number of pixels the element can move left * @param {int} iRight the number of pixels the element can move right * @param {int} iTickSize optional parameter for specifying that the element * should move a certain number pixels at a time. * @return {Slider} a horizontal slider control */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");};/** * Factory method for creating a vertical slider * @method YAHOO.widget.Slider.getVertSlider * @static * @param {String} sBGElId the id of the slider's background element * @param {String} sHandleElId the id of the thumb element * @param {int} iUp the number of pixels the element can move up * @param {int} iDown the number of pixels the element can move down * @param {int} iTickSize optional parameter for specifying that the element * should move a certain number pixels at a time. * @return {Slider} a vertical slider control */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");};/** * Factory method for creating a slider region like the one in the color * picker example * @method YAHOO.widget.Slider.getSliderRegion * @static * @param {String} sBGElId the id of the slider's background element * @param {String} sHandleElId the id of the thumb element * @param {int} iLeft the number of pixels the element can move left * @param {int} iRight the number of pixels the element can move right * @param {int} iUp the number of pixels the element can move up * @param {int} iDown the number of pixels the element can move down * @param {int} iTickSize optional parameter for specifying that the element * should move a certain number pixels at a time. * @return {Slider} a slider region control */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");};/** * By default, animation is available if the animation library is detected. * @property YAHOO.widget.Slider.ANIM_AVAIL * @static * @type boolean */YAHOO.widget.Slider.ANIM_AVAIL = true;YAHOO.extend(YAHOO.widget.Slider, YAHOO.util.DragDrop, { /** * Initializes the slider. Executed in the constructor * @method initSlider * @param {string} sType the type of slider (horiz, vert, region) */ initSlider: function(sType) { /** * The type of the slider (horiz, vert, region) * @property type * @type string */ this.type = sType; //this.removeInvalidHandleType("A"); /** * Event the fires when the value of the control changes. If * the control is animated the event will fire every point * along the way. * @event change * @param {int} newOffset|x the new offset for normal sliders, or the new * x offset for region sliders * @param {int} y the number of pixels the thumb has moved on the y axis * (region sliders only) */ this.createEvent("change", this); /** * Event that fires at the beginning of a slider thumb move. * @event slideStart */ this.createEvent("slideStart", this); /** * Event that fires at the end of a slider thumb move * @event slideEnd */ this.createEvent("slideEnd", this); /** * Overrides the isTarget property in YAHOO.util.DragDrop * @property isTarget * @private */ this.isTarget = false; /** * Flag that determines if the thumb will animate when moved * @property animate * @type boolean */ this.animate = YAHOO.widget.Slider.ANIM_AVAIL; /** * Set to false to disable a background click thumb move * @property backgroundEnabled * @type boolean */ this.backgroundEnabled = true; /** * Adjustment factor for tick animation, the more ticks, the * faster the animation (by default) * @property tickPause * @type int */ this.tickPause = 40; /** * Enables the arrow, home and end keys, defaults to true. * @property enableKeys * @type boolean */ this.enableKeys = true; /** * Specifies the number of pixels the arrow keys will move the slider. * Default is 25. * @property keyIncrement * @type int */ this.keyIncrement = 20; /** * moveComplete is set to true when the slider has moved to its final * destination. For animated slider, this value can be checked in * the onChange handler to make it possible to execute logic only * when the move is complete rather than at all points along the way. * * @property moveComplete * @type Boolean */ this.moveComplete = true; /** * If animation is configured, specifies the length of the animation * in seconds. * @property animationDuration * @type int * @default 0.2 */ this.animationDuration = 0.2; }, /** * Initializes the slider's thumb. Executed in the constructor. * @method initThumb * @param {YAHOO.widget.SliderThumb} t the slider thumb */ initThumb: function(t) { var self = this; /** * A YAHOO.widget.SliderThumb instance that we will use to * reposition the thumb when the background is clicked * @property thumb * @type YAHOO.widget.SliderThumb */ this.thumb = t; t.cacheBetweenDrags = true; // add handler for the handle onchange event t.onChange = function() { self.handleThumbChange(); }; if (t._isHoriz && t.xTicks && t.xTicks.length) { this.tickPause = Math.round(360 / t.xTicks.length); } else if (t.yTicks && t.yTicks.length) { this.tickPause = Math.round(360 / t.yTicks.length); } // delegate thumb methods t.onMouseDown = function () { return self.focus(); }; t.onMouseUp = function() { self.thumbMouseUp(); }; t.onDrag = function() { self.fireEvents(true); }; t.onAvailable = function() { return self.setStartSliderState(); }; }, /** * Executed when the slider element is available * @method onAvailable */ onAvailable: function() { var Event = YAHOO.util.Event; Event.on(this.id, "keydown", this.handleKeyDown, this, true); Event.on(this.id, "keypress", this.handleKeyPress, this, true); }, /** * Executed when a keypress event happens with the control focused. * Prevents the default behavior for navigation keys. The actual * logic for moving the slider thumb in response to a key event * happens in handleKeyDown. * @param {Event} e the keypress event */ handleKeyPress: function(e) { if (this.enableKeys) { var Event = YAHOO.util.Event; var kc = Event.getCharCode(e); switch (kc) { case 0x25: // left case 0x26: // up case 0x27: // right case 0x28: // down case 0x24: // home case 0x23: // end Event.preventDefault(e); break; default: } } }, /** * Executed when a keydown event happens with the control focused. * Updates the slider value and display when the keypress is an * arrow key, home, or end as long as enableKeys is set to true. * @param {Event} e the keydown event */ handleKeyDown: function(e) { if (this.enableKeys) { var Event = YAHOO.util.Event; var kc = Event.getCharCode(e), t=this.thumb; var h=this.getXValue(),v=this.getYValue(); var horiz = false; var changeValue = true; switch (kc) { // left case 0x25: h -= this.keyIncrement; break; // up case 0x26: v -= this.keyIncrement; break; // right case 0x27: h += this.keyIncrement; break; // down case 0x28: v += this.keyIncrement; break; // home case 0x24: h = t.leftConstraint; v = t.topConstraint; break; // end case 0x23: h = t.rightConstraint; v = t.bottomConstraint; break; default: changeValue = false; } if (changeValue) { if (t._isRegion) { this.setRegionValue(h, v, true); } else { var newVal = (t._isHoriz) ? h : v; this.setValue(newVal, true); } Event.stopEvent(e); } } }, /** * Initialization that sets up the value offsets once the elements are ready * @method setStartSliderState */ setStartSliderState: function() { this.setThumbCenterPoint(); /** * The basline position of the background element, used * to determine if the background has moved since the last * operation. * @property baselinePos * @type [int, int] */ 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); this.deferredSetRegionValue = null; } else { this.setRegionValue(0, 0, true, true); } } else { if (this.deferredSetValue) { this.setValue.apply(this, this.deferredSetValue, true); this.deferredSetValue = null; } else { this.setValue(0, true, true); } } }, /** * When the thumb is available, we cache the centerpoint of the element so * we can position the element correctly when the background is clicked * @method setThumbCenterPoint */ setThumbCenterPoint: function() { var el = this.thumb.getEl(); if (el) { /** * The center of the slider element is stored so we can * place it in the correct position when the background is clicked. * @property thumbCenterPoint * @type {"x": int, "y": int} */ this.thumbCenterPoint = {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -