zengh
2022-05-16 63ad2c3598400370dd7da5534659fd7e768a0a4a
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
/*
 */
(function($) {
    var methods = {
        init: function(opts) {
            var settings = {};
            return this.each(function() {
                var $this = $(this);
                var data = $this.data('TimeSliderPlugin');
                var options = opts || {};
                options = $.extend({}, settings, options);
                if (!data) {
                    data = new $.TimeSliderPlugin(this, options);
                    $(this).data('TimeSliderPlugin', data);
                    data.init();
                }
            });
        },
        destroy: function() {
            return this.each(function() {
                var $this = $(this);
                $this.removeData('TimeSliderPlugin');
            });
        },
        setCurrent: function(index) {
            return this.each(function() {
                getData(this).setCurrent(index);
            });
        },
        getCurrent: function(index) {
            return getData(this).getCurrent(index);
        },
    };
 
    function getData(context) {
        var $this = $(context);
        var data = $this.data('TimeSliderPlugin');
        if (!data) {
            alert("timeslider控件未初始化!");
        }
        return data;
    };
    //初始化函数
    $.TimeSliderPlugin = function(target, opts) {
        var options = opts;
        var $body = $(target);
        var currentIndex = 0;
        var $this = this;
        this.setCurrent = function(index) {
            if (opts.reverse){
                index= opts.max-index-1;
            }
            currentIndex = index;
            this.updateButtonStyle(currentIndex);
            this.changeLayers(currentIndex);
        };
        this.getCurrent = function() {
            if (opts.reverse){
                return opts.max-currentIndex-1;
            }
            return currentIndex;
        };
        this.init = function() {
            if (opts.reverse){
                opts.timeset.reverse();
            }
            var timeset = opts.timeset;
            
            //初始化控件布局<div class="time_outer"></div>
            $body.append('<div class="time_txt"></div><div class="time_container"><div class="to_left_un"></div><div class="showMultDate_img">'+opts.groupName+'</div><div class="time_MultContainer"><div class="time_image_zz"></div></div><div class="to_right"></div><div class="date"></div></div>');
 
            var max = opts.timeset.length;
            var min = opts.min;
            //生成时间条刻度
            for (var i = 0; i < max; i++) {
                var left = 200 / (max - 1) * i;
                if (i == max - 1) left = left - 2 - 5 - 5;
                $(".time_MultContainer").append("<div class='multshowdate' style='left:" + (left + 5) + "px'></div>");
                $body.find(".date").append("<div style='position:absolute;top:0px;left:" + (left + 10) + "px'>" + timeset[i].substring(0, 4) + "</div>");
            }
 
            //生成时间lbl
            var timetxt = timeset[currentIndex];
            $body.find(".time_txt").html(timetxt);
 
            //to_left
            $body.find(".to_left_un").click(function() {
                //修改左右按钮样式
                if (currentIndex > 0) {
                    currentIndex--;
                    $this.updateButtonStyle(currentIndex);
                    $this.changeLayers(currentIndex);
                    opts.timeChangeHandler(opts.reverse?opts.max-currentIndex-1:currentIndex);
                }
            });
            //to_right
            $body.find(".to_right").click(function() {
 
                //修改左右按钮样式
                if (currentIndex < max - 1) {
                    currentIndex++;
                    $this.updateButtonStyle(currentIndex);
                    $this.changeLayers(currentIndex);
                    opts.timeChangeHandler(opts.reverse?opts.max-currentIndex-1:currentIndex);
                }
 
            });
 
        };
        this.updateButtonStyle = function(current) {
 
            if (current > 0) {
                $body.find(".to_left_un").removeClass().addClass("to_left");
            }
            if (current == 0) {
                $body.find(".to_left").removeClass().addClass("to_left_un");
            }
            if (current == options.max - 1) {
                $body.find(".to_right").removeClass().addClass("to_right_un");
            }
            if (current < options.max - 1) {
                $body.find(".to_right_un").removeClass().addClass("to_right");
            }
 
        }
 
        this.changeLayers = function(current) {
            var left = 200 / (options.max - 1) * current;
            if (current == options.max - 1) {
                left = left - 10;
            }
            //改变刻度指针位置
            $body.find(".time_image_zz").css({
                "left": left
            });
            $body.find(".time_txt").html(options.timeset[current]);
            $body.find(".time_txt").css({
                "margin-left": left + 20
            });
        }
 
 
    };
    $.fn.timeslider = function(method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on timeslider');
        }
        return this;
    };
})(jQuery);