📄 slider.js
字号:
/*Copyright (c) 2008, Yahoo! Inc. All rights reserved.Code licensed under the BSD License:http://developer.yahoo.net/yui/license.txtversion: 2.6.0*//** * 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) { YAHOO.widget.Slider.ANIM_AVAIL = (!YAHOO.lang.isUndefined(YAHOO.util.Anim)); 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 utility is detected. * @property YAHOO.widget.Slider.ANIM_AVAIL * @static * @type boolean */YAHOO.widget.Slider.ANIM_AVAIL = false;YAHOO.extend(YAHOO.widget.Slider, YAHOO.util.DragDrop, { /** * Override the default setting of dragOnly to true. * @property dragOnly * @type boolean * @default true */ dragOnly : true, /** * 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 20. * @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. * Deprecated because this flag is only useful when the background is * clicked and the slider is animated. If the user drags the thumb, * the flag is updated when the drag is over ... the final onDrag event * fires before the mouseup the ends the drag, so the implementer will * never see it. * * @property moveComplete * @type Boolean * @deprecated use the slideEnd event instead */ 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; /** * Constant for valueChangeSource, indicating that the user clicked or * dragged the slider to change the value. * @property SOURCE_UI_EVENT * @final * @default 1 */ this.SOURCE_UI_EVENT = 1; /** * Constant for valueChangeSource, indicating that the value was altered * by a programmatic call to setValue/setRegionValue. * @property SOURCE_SET_VALUE * @final * @default 2 */ this.SOURCE_SET_VALUE = 2; /** * When the slider value changes, this property is set to identify where * the update came from. This will be either 1, meaning the slider was * clicked or dragged, or 2, meaning that it was set via a setValue() call. * This can be used within event handlers to apply some of the logic only * when dealing with one source or another. * @property valueChangeSource * @type int * @since 2.3.0 */ this.valueChangeSource = 0; /** * Indicates whether or not events will be supressed for the current * slide operation * @property _silent * @type boolean * @private */ this._silent = false; /** * Saved offset used to protect against NaN problems when slider is * set to display:none * @property lastOffset * @type [int, int] */ this.lastOffset = [0,0]; }, /** * 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.onAvailable = function() { return self.setStartSliderState(); }; t.onMouseDown = function () { return self.focus(); }; t.startDrag = function() { self._slideStart(); }; t.onDrag = function() { self.fireEvents(true); }; t.onMouseUp = function() { self.thumbMouseUp(); }; }, /** * 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -