riku
2024-02-27 7578c3ff65329b2269f099475eb687e963efac1c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
;
(function($, window, document, undefined) {
    'use strict';
 
    var pluginName = "jackWeiSlider";
 
    var defaults = {
        width: '400px',
        handleSrc: '../Images/slider_handle.png',
        progress: 0.3,
        isCustomText: false
    };
 
    //构造函数
    function JackWeiSlider(element, options) {
        this.element = element;
        this.settings = $.extend({}, defaults, options);
        this._defaults = defaults;
        this._name = pluginName;
 
        this.isEnable = true;
        this.dcX = 0;
        this.barW = 0;
        this.currW = 0;
        this.haMarginL = -12;
        this.txMarginL = -40;
        this.isDrag = false;
        this.progress = 0;
        this.onStartDragCallback;
        this.onDragCallback;
        this.onStopDragCallback;
        this.maxW = parseInt(this.settings.width.split('px')[0]);
        this.isCustomText = this.settings.isCustomText;
 
        this.init();
    }
 
    JackWeiSlider.prototype = {
        init: function() {
            var that = this; //避免与内部对象的this重名
            var settings = that.settings;
            var $element = $(this.element);
 
            //添加slider元素
            $element.append('<div style="width:' + settings.width + ';">\n' +
                '        <div class="jws-outside-bar">\n' +
                '            <div class="jws-inside-bar" style="background-color: ' + settings.color + '"></div>\n' +
                '            <img class="jws-handle" src=' + settings.handleSrc + '>\n' +
                '            <div class="jws-text"></div>\n' +
                '        </div>\n' +
                '    </div>'
            );
 
            //设置默认进度
            this.setProgress(settings.progress);
 
            $(this.element).on('mousedown', '.jws-handle', function(e) {
                if (!that.isEnable) return;
 
                that.element.attr('tabindex', -1)
                that.element.css("outline", "none")
                that.element.focus();
                that.isDrag = true;
                that.dcX = e.clientX;
                if (typeof that.onStartDragCallback === 'function')
                    that.onStartDragCallback();
                e.preventDefault();
            });
 
            $(document).mousemove(function(e) {
                if (!that.isDrag) return;
 
                //阻止默认事件
                e.preventDefault();
 
                //计算偏移量并开始移动滑块
                that.move(e.clientX - that.dcX);
                // console.log(e.clientX);
 
                //拖动事件回掉
                if (typeof that.onDragCallback === 'function')
                    that.onDragCallback(that.progress);
            });
 
            $(this.element).keydown(function(e) {
                // if (document.activeElement != that.element) {
                //     return
                // }
                //left arrow
                if (e.keyCode == 37) {
                    that.setProgress(that.progress - 0.003)
                    //拖动事件回掉
                    if (typeof that.onDragCallback === 'function')
                        that.onDragCallback(that.progress);
                }
                //right arrow
                else if (e.keyCode == 39) {
                    that.setProgress(that.progress + 0.003)
                    //拖动事件回掉
                    if (typeof that.onDragCallback === 'function')
                        that.onDragCallback(that.progress);
                }
            });
 
            $(document).mouseup(function(e) {
                if (!that.isDrag) return;
 
                that.isDrag = false;
 
                //获取当前控件的位置数据
                that.updateData(that);
 
                //停止拖拽回调
                if (typeof that.onStopDragCallback === 'function')
                    that.onStopDragCallback();
            });
        },
        enable: function() {
            this.isEnable = true;
            return this;
        },
        disEnable: function() {
            this.isEnable = false;
            return this;
        },
        setText: function(text) {
            $(this.element).find('.jws-text').text(text);
            this.isCustomText = true;
            return this;
        },
        updateData: function() {
            var $element = $(this.element);
            this.currW = this.barW = parseInt($element.find('.jws-inside-bar').css('width').split("px")[0]);
            this.haMarginL = parseInt($element.find('.jws-handle').css('margin-left').split("px")[0]);
            this.txMarginL = parseInt($element.find('.jws-text').css('margin-left').split("px")[0]);
        },
        move: function(offset) {
            //计算控件现在的位置
            var w = Math.round(this.barW + offset);
            var hml = Math.round(this.haMarginL + offset);
            var tml = Math.round(this.txMarginL + offset);
            // console.log('w:' + w + ' hml:' + hml + ' tml:' + tml);
 
            //往左限制
            if (w < 0 || hml < -12 || tml < -40) {
                w = 0
                hml = -12
                tml = -40
            }
 
            //往右限制
            if (w > this.maxW || hml > -12 + this.maxW || tml > -40 + this.maxW) {
                w = this.maxW
                hml = -12 + this.maxW
                tml = -40 + this.maxW
            }
 
            //更新progress
            this.progress = w / this.maxW;
 
            //更新UI
            var $element = $(this.element);
            $element.find('.jws-inside-bar').css('width', w);
            $element.find('.jws-handle').css('margin-left', hml);
            $element.find('.jws-text').css('margin-left', tml);
            if (!this.isCustomText)
                $element.find('.jws-text').text(Math.round(this.progress * 100) + "%");
        },
        setProgress: function(progress) {
            if (progress > 1) {
                progress = 1
            } else if (progress < 0) {
                progress = 0
            }
            var offset = progress * this.maxW - this.currW; //减去当前位置回到原点
            this.move(offset, this);
            this.updateData(this);
            return this;
        },
        setOnStartDragCallback: function(callback) {
            this.onStartDragCallback = callback;
            return this;
        },
        setOnDragCallback: function(callback) {
            this.onDragCallback = callback;
            return this;
        },
        setOnStopDragCallback: function(callback) {
            this.onStopDragCallback = callback;
            return this;
        }
    }
 
    $.fn.jackWeiSlider = function(options) {
        return new JackWeiSlider(this, options);
    }
 
})(jQuery, window, document);