11 Creates a slider with two elements: a knob and a container. Returns the values.
14 The Slider requires an XHTML doctype.
17 element - the knob container
19 options - see Options below
22 onChange - a function to fire when the value changes.
23 onComplete - a function to fire when you're done dragging.
24 onTick - optionally, you can alter the onTick behavior, for example displaying an effect of the knob moving to the desired position.
25 Passes as parameter the new position.
26 steps - the number of steps for your slider.
27 mode - either 'horizontal' or 'vertical'. defaults to horizontal.
28 offset - relative offset for knob position. default to 0.
31 var Slider = new Class({
34 onChange: Class.empty,
35 onComplete: Class.empty,
36 onTick: function(pos){
37 this.knob.setStyle(this.p, pos);
44 initialize: function(el, knob, options){
47 this.setOptions(options);
48 this.previousChange = -1;
49 this.previousEnd = -1;
51 this.element.addEvent('mousedown', this.clickedElement.bindWithEvent(this));
53 switch(this.options.mode){
57 mod = {'x': 'left', 'y': false};
58 offset = 'offsetWidth';
63 mod = {'x': false, 'y': 'top'};
64 offset = 'offsetHeight';
66 this.max = this.element[offset] - this.knob[offset] + (this.options.offset * 2);
67 this.half = this.knob[offset]/2;
68 this.getPos = this.element['get' + this.p.capitalize()].bind(this.element);
69 this.knob.setStyle('position', 'relative').setStyle(this.p, - this.options.offset);
71 lim[this.z] = [- this.options.offset, this.max - this.options.offset];
72 this.drag = new Drag.Base(this.knob, {
82 onComplete: function(){
87 if (this.options.initialize) this.options.initialize.call(this);
92 The slider will get the step you pass.
99 this.step = step.limit(0, this.options.steps);
102 this.fireEvent('onTick', this.toPosition(this.step));
106 clickedElement: function(event){
107 var position = event.page[this.z] - this.getPos() - this.half;
108 position = position.limit(-this.options.offset, this.max -this.options.offset);
109 this.step = this.toStep(position);
112 this.fireEvent('onTick', position);
115 draggedKnob: function(){
116 this.step = this.toStep(this.drag.value.now[this.z]);
120 checkStep: function(){
121 if (this.previousChange != this.step){
122 this.previousChange = this.step;
123 this.fireEvent('onChange', this.step);
128 if (this.previousEnd !== this.step){
129 this.previousEnd = this.step;
130 this.fireEvent('onComplete', this.step + '');
134 toStep: function(position){
135 return Math.round((position + this.options.offset) / this.max * this.options.steps);
138 toPosition: function(step){
139 return this.max * step / this.options.steps;
144 Slider.implement(new Events);
145 Slider.implement(new Options);