Created
February 5, 2013 12:05
-
-
Save dexteryy/4714086 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* TUI::ui::slider | |
* @created: Dexter.Yy | |
* @modified: $Author$ | |
* @version: $Rev$ | |
*/ | |
/** | |
* 滑块组件 | |
* @class TUI.widget.slider | |
*/ | |
TUI.newModule("TUI.widget.slider", function(sandbox, $, TUI){ | |
var Slider = { | |
event: {}, | |
setByValue: function(v, anime){ | |
this.value = v; | |
this.rank = parseInt( ( v - this.start ) / this.step ); | |
this.change(anime); | |
}, | |
setByRank: function(r, anime){ | |
this.rank = r; | |
this.value = this.start + this.rank * this.step; | |
this.change(anime); | |
}, | |
change: function(anime){ | |
var dist = this.stepWidth * this.rank; | |
if (anime || this.anime) | |
$(this.block).animate({ left: dist }, 100, "easeOutQuad"); | |
else | |
this.block.style.left = dist + "px"; | |
/** | |
* 滑块的值发生改变 | |
* @event change | |
*/ | |
this.event.fire("change", [this.value, this.rank]); | |
} | |
}; | |
return function(op){ | |
var o = TUI.clone(Slider); | |
$.extend(o, op); | |
o.event = new TUI.eventClass(); | |
var t = o.track; | |
var s = o.block = t.firstChild; | |
var blockWidth = s.offsetWidth || parseInt($(s).css("width")); | |
var rangeWidth = ( t.offsetWidth || parseInt($(t).css("width")) ) - blockWidth; | |
o.stepWidth = Math.abs(o.step) / ( o.max - o.min ) * rangeWidth; | |
o.start = o.step > 0 ? o.min : o.max; | |
o.setByValue(o.initial); | |
var start; | |
var mousemove = function(e){ | |
if (!start) { | |
$(document).unbind("mousemove", mousemove); | |
return false; | |
} | |
var m = o; | |
var end = e.pageX; | |
var n = parseInt( ( end - start ) / m.stepWidth ); | |
if ( Math.abs(n) > 1 ) { | |
var rank = m.rank + n; | |
var v = m.start + rank * m.step; | |
if (v <= m.max && v >= m.min) { | |
m.setByValue(v); | |
start = end; | |
} | |
} | |
}; | |
var mouseup = function(){ | |
start = null; | |
$(document).unbind("mousemove", mousemove).unbind("mouseup", mouseup); | |
/** | |
* 鼠标释放滑块 | |
* @event release | |
*/ | |
o.event.fire("release", [o.value, o.rank]); | |
}; | |
$(s).mousedown(function(e){ | |
e.preventDefault(); | |
start = e.pageX; | |
$(document).mousemove(mousemove).mouseup(mouseup); | |
}).click(function(){ | |
return false; | |
}); | |
$(t).click(function(e){ | |
var d = e.pageX - TUI.pos.elementLeft(t) - blockWidth / 2; | |
if ( d > rangeWidth ) | |
d = rangeWidth; | |
else if ( d < 0 ) | |
d = 0; | |
var rank = parseInt(d / o.stepWidth); | |
o.setByRank(rank, true); | |
o.event.fire("release", [o.value, o.rank]); | |
}); | |
return o; | |
}; | |
}, [jQuery, TUI]); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment