|
1 /** |
|
2 * jquery.spin-button |
|
3 * (c) 2008 Semooh (http://semooh.jp/) |
|
4 * |
|
5 * Dual licensed under the MIT (MIT-LICENSE.txt) |
|
6 * and GPL (GPL-LICENSE.txt) licenses. |
|
7 * |
|
8 **/ |
|
9 (function($){ |
|
10 $.fn.extend({ |
|
11 spin: function(opt){ |
|
12 return this.each(function(){ |
|
13 opt = $.extend({ |
|
14 imageBasePath: '/img/spin/', |
|
15 spinBtnImage: 'spin-button.png?v='+Math.random(), //IE fix |
|
16 spinUpImage: 'spin-up.png?v='+Math.random(), //IE fix |
|
17 spinDownImage: 'spin-down.png?v='+Math.random(), //IE fix |
|
18 interval: 1, |
|
19 max: null, |
|
20 min: null, |
|
21 timeInterval: 300, |
|
22 timeBlink: 100 |
|
23 }, opt || {}); |
|
24 |
|
25 var txt = $(this); |
|
26 |
|
27 var spinBtnImage = opt.imageBasePath+opt.spinBtnImage; |
|
28 var btnSpin = new Image(); |
|
29 $(btnSpin).load(function(){ btn.css({width: btnSpin.width+'px'}); }); //IE fix |
|
30 btnSpin.src = spinBtnImage; |
|
31 |
|
32 var spinUpImage = opt.imageBasePath+opt.spinUpImage; |
|
33 var btnSpinUp = new Image(); |
|
34 btnSpinUp.src = spinUpImage; |
|
35 |
|
36 var spinDownImage = opt.imageBasePath+opt.spinDownImage; |
|
37 var btnSpinDown = new Image(); |
|
38 btnSpinDown.src = spinDownImage; |
|
39 |
|
40 var btn = $(document.createElement('img')); |
|
41 btn.attr('src', spinBtnImage); |
|
42 btn.css({cursor: 'pointer', verticalAlign: 'bottom', padding: 0, margin: 0 }); |
|
43 txt.after(btn); |
|
44 txt.css({marginRight:0, paddingRight:0}); |
|
45 |
|
46 |
|
47 function spin(vector){ |
|
48 var val = txt.val(); |
|
49 if(!isNaN(val)){ |
|
50 val = parseFloat(val) + (vector*opt.interval); |
|
51 if(opt.min!=null && val<opt.min) val=opt.min; |
|
52 if(opt.min!=null && val>opt.max) val=opt.max; |
|
53 if(val != txt.val()){ |
|
54 txt.val(val); |
|
55 txt.change(); |
|
56 src = (vector > 0 ? spinUpImage : spinDownImage); |
|
57 btn.attr('src', src); |
|
58 if(opt.timeBlink<opt.timeInterval) |
|
59 setTimeout(function(){btn.attr('src', spinBtnImage);}, opt.timeBlink); |
|
60 } |
|
61 } |
|
62 } |
|
63 |
|
64 btn.mousedown(function(e){ |
|
65 var pos = e.pageY - btn.offset().top + $(document.body).offset().top; |
|
66 var vector = (btn.height()/2 > pos ? 1 : -1); |
|
67 (function(){ |
|
68 spin(vector); |
|
69 var tk = setTimeout(arguments.callee, opt.timeInterval); |
|
70 $(document).one('mouseup', function(){ |
|
71 clearTimeout(tk); btn.attr('src', spinBtnImage); |
|
72 }); |
|
73 })(); |
|
74 return false; |
|
75 }); |
|
76 }); |
|
77 } |
|
78 }); |
|
79 })(jQuery); |