PageSourceSearch

https://www.cpic.com.cn/toolbar/toolbar.js?v=1.2

js cpic.com.cn collected 2026-10-02 07:35:01 UTC 15,861 bytes, 356 lines download raw bytes

1/**
2 * @description 官网顶部工具栏
3 * @time 2021-12-17 10:34:57
4 * 调用方式(不传参数,则用默认配置):
5 1、页面引入toolbar.js、toolbar.css
6 2、$('body').toolbar({
7    zoomDom: '.zoomElement', // 放大的容器元素
8    zoomMax: 2, // 缩放最大比例
9    zoomMin: 1, // 缩放最小比例
10    zoomStep: 0.1, // 点击缩放
11    switchBtn: true, // 工具栏开关
12    switchBtnPosTop: '50%', // 侧边栏开关定位top值
13    domain: '.cpic.com.cn', // 设置cookie
14    callback: function(barFnName, number){}, // 工具栏操作触发回调 number:缩放倍数, eventType:工具栏按钮对应事件名称
15    fixedDomArr: [], // dom arr 针对原来fixed定位元素处理,以兼容IE和IE兼容模式浏览器
16})
17 */
18(function ($) {
19// 私有方法
20    var privateFun = function () {
21    };
22    var getCookie = function (c_name) {
23        if (document.cookie.length > 0) {
24            c_start = document.cookie.indexOf(c_name + '=');
25            if (c_start !== -1) {
26                c_start = c_start + c_name.length + 1;
27                c_end = document.cookie.indexOf(';', c_start);
28                if (c_end === -1) {
29                    c_end = document.cookie.length;
30                }
31                return unescape(document.cookie.substring(c_start, c_end));
32            }
33        }
34        return null;
35    }
36    var setCookie = function (name, value, days, path, domain) {
37        var Days = days || 0;
38        var path = path || '';
39        var domain = domain || '';
40        var expires = '';
41        if (Days !== 0) {
42            var date = new Date();
43            date.setTime(date.getTime() + Days * 24 * 60 * 60 * 1000);
44            expires = '; expires=' + date.toGMTString();
45        }
46        if (path !== '') {
47            path = ';path=' + path;
48        }
49        if (domain !== '') {
50            domain = ';domain=' + domain;
51        }
52        document.cookie = name + '=' + escape(value) + expires + path + domain + '';
53    }
54    function debounce(fn, interval) {
55        let timeout = null;
56        return function () {
57            clearTimeout(timeout);
58             timeout = setTimeout(function() {
59                fn.apply(this, arguments);
60            }, interval || 300);
61        };
62    }
63    var urlKey = location.origin.indexOf('sit') !== -1 ? 'sit' : 'prod';
64    var urlObj = {
65        sit: {
66            websitUrl: 'https://www.cpic.com.cn/'
67        },
68        prod: {
69            websitUrl: 'https://www.cpic.com.cn/'
70        }
71    }
72// 合并对象等
73    var toolbar = (function () {
74        function toolbar(element, options) {
75            // 设置为true深拷贝,合并对象
76            var initDefault = $.extend(true, {}, $.fn.toolbar.options)
77            this.settings = $.extend(true, initDefault, options || {});
78            this.element = element || $(document.body);
79            this.zoomElement = $(this.settings.zoomDom);
80            this.fnName = {
81                hideToolbar: 'hideToolbar',
82                showToolbar: 'showToolbar',
83                initZoom: 'initZoom',
84                changeZoom: 'changeZoom'
85            }
86            this.show = false;
87          				
88            // 初始化插件
89            this.init()
90        }
91
92        // 在toolbar原型上定义方法,让实例可以调用这些方法
93        toolbar.prototype = {
94            // 功能方法
95            init: function () {
96                this.creatHtml()
97                this.initZoom()
98            },
99            creatHtml: function () {
100                // bar
101                var toolbarHtml = '';
102                toolbarHtml += '<div class="toolbar">'
103                toolbarHtml += '<ul class="toolbar-div">'
104                toolbarHtml += '<li class="bar-item">'
105                toolbarHtml += '<span class="bar-img toolbar_reset"><img  src="' + urlObj[urlKey].websitUrl + '/toolbar/images/reflash1.png" alt=""></span>'
106                toolbarHtml += '<span class="bar-text" title="重置">重置</span>'
107                toolbarHtml += '</li>'
108                toolbarHtml += '<li class="bar-item">'
109                toolbarHtml += '<span class="bar-img toolbar_amplification"><img  src="' + urlObj[urlKey].websitUrl + '/toolbar/images/enlarge1.png" alt=""></span>'
110                toolbarHtml += '<span class="bar-text" title="放大">放大</span>'
111                toolbarHtml += '</li>'
112                toolbarHtml += '<li class="bar-item">'
113                toolbarHtml += '<span class="bar-img toolbar_narrow"><img  src="' + urlObj[urlKey].websitUrl + '/toolbar/images/small1.png" alt=""></span>'
114                toolbarHtml += '<span class="bar-text" title="缩小">缩小</span>'
115                toolbarHtml += '</li>'
116                toolbarHtml += '<li class="bar-item">'
117                toolbarHtml += '<span class="bar-img toolbar_exit"><img  src="' + urlObj[urlKey].websitUrl + '/toolbar/images/exit1.png" alt=""></span>'
118                toolbarHtml += '<span class="bar-text" title="退出">退出</span>'
119                toolbarHtml += '</li>'
120                toolbarHtml += ' </ul>'
121                toolbarHtml += '</div>';
122
123                this.element.append(toolbarHtml)
124                if (this.settings.switchBtn) {
125                    this.loadSwithBtn()
126                }
127                this.events();
128            },
129            loadSwithBtn: function () {
130                // 开关
131                var btnHtml = '<div class="bar-switch-button" style="top: ' + this.settings.switchBtnPosTop + '">爱心模式</div>';
132                this.element.append(btnHtml)
133            },
134            // 缩放
135            changeZoom: function (type) {
136                var number = this.zoomElement.css("transform") ?
137                    Number(this.zoomElement
138                        .css("transform")
139                        .replace(/[^0-9\-\.,]/g, '')
140                        .split(',')[0]) : 1;
141                // 放大倍数
142                if (type === '+' && number < this.settings.zoomMax) {
143                    number += this.settings.zoomStep
144                }
145                // 缩小倍数
146                if (type === '-' && number > this.settings.zoomMin) {
147                    number -= this.settings.zoomStep
148                }
149                // 指定倍数
150                if (typeof type === 'number') {
151                    number = type;
152                }
153                // css3缩放
154                this.zoomElement.css({
155                    'transform': "scale(" + number + ")",
156                    'transform-origin': '0px 0px'
157                })
158                // 滚动条居中
159                window.scrollTo((document.body.scrollWidth - window.innerWidth) / 2, 0)
160                // 保存在cookie中,页面公用
161                setCookie('readZoom', number, '', '/', this.settings.domain)
162                this.callBack(this.fnName.changeZoom, number)
163            },
164            // 初始化缩放
165            initZoom: function () {
166                var number = Number(getCookie('readZoom'));
167                if (number) {
168                    // 初始化zoom系数
169                    this.changeZoom(number)
170                    setTimeout(function () {
171                        window.scrollTo((document.body.scrollWidth - window.innerWidth) / 2, 0)
172                    }, 300)
173                }
174                // bar显示与否
175             
176                if (Number(getCookie('toolbarVisible'))) {
177                    this.showToolbar();
178                }
179                if ((number === 1 || !number) && $('.toolbar').css('display') === 'none') {
180                    this.zoomElement.css({
181                        'transform': "none",
182                        'transform-origin': '0px 0px'
183                    })
184                }
185                this.fixedElementCompatibleIE(this.fnName.initZoom, number, this.settings.fixedDomArr)
186                this.callBack(this.fnName.initZoom, number)
187            },
188            // 显示bar
189            showToolbar: function () {
190                $('.zoomElement').addClass('zoomElement-zoomed');
191                var number = Number(getCookie('readZoom')) || 1
192                this.changeZoom(number)
193                $(".toolbar").slideDown(0);
194                this.show = true;
195                $("body").animate({paddingTop: '98px'}, 0);
196                this.fixedElementCompatibleIE(this.fnName.showToolbar, number, this.settings.fixedDomArr)
197                setCookie('toolbarVisible', 1, '', '/', this.settings.domain);
198                this.callBack(this.fnName.showToolbar, number)
199            },
200            // 隐藏bar
201            hideToolbar: function () {
202                var that = this;
203                $(".toolbar").slideUp(0);
204                var number = Number(getCookie('readZoom')) || 1
205                this.show = false;
206                this.changeZoom(1)
207                $("body").animate({paddingTop: '0px'}, 0);
208                setTimeout(function () {
209                    that.resetTransform()
210                }, 350)
211                this.fixedElementCompatibleIE(this.fnName.hideToolbar, number, this.settings.fixedDomArr)
212                setCookie('toolbarVisible', 0, '', '/', this.settings.domain);
213                this.callBack(this.fnName.hideToolbar, number)
214                setTimeout(function(){
215                  $('.zoomElement').removeClass('zoomElement-zoomed');
216                },350)
217            },
218            // 切换bar可见性
219            toggleToolbar: function () {
220                if ($('.toolbar').css('display') === 'block') {
221                    this.hideToolbar()
222                } else {
223                    this.showToolbar();
224                }
225            },
226            // 清除transform
227            resetTransform: function () {
228                var number = this.zoomElement.css("transform") ?
229                    Number(this.zoomElement
230                        .css("transform")
231                        .replace(/[^0-9\-\.,]/g, '')
232                        .split(',')[0]) : 1;
233                if ((number === 1 || !number) && $('.toolbar').css('display') === 'none') {
234                    this.zoomElement.css({
235                        'transform': "none",
236                        'transform-origin': '0px 0px'
237                    })
238                }
239            },
240            // 缩放后,ie获取ie兼容模式对固定定位元素处理
241            fixedElementCompatibleIE: function (fn, number, arr) {
242                var timer = {};
243                for (var j = 0; j < arr.length; j++) {
244                    if (fn === this.fnName.showToolbar ||
245                        (fn === this.fnName.initZoom && number !== 1)) { // 工具栏显示或者初始化
246                        if (navigator.userAgent.indexOf('Trident') !== -1) { // ie浏览器 或者IE兼容模式处理fixed定位元素
247                            if (arr[j].sync) {
248                                (function (i) {
249                                    timer[i + 'xxx'] = setInterval(function () {
250                                        if ($(arr[i].name).length > 0) {
251                                            clearInterval(timer[i + 'xxx'])
252                                            $(arr[i].name).css({
253                                                position: 'absolute'
254                                            })
255                                        }
256                                    }, 0)
257                                })(j);
258                            } else {
259                                $(arr[j].name).css({
260                                    position: 'absolute'
261                                })
262                            }
263                        }
264                    }
265                    if ((fn === this.fnName.hideToolbar || fn === this.fnName.initZoom)
266                        && number === 1 && !this.show) { // 工具栏初始化或者隐藏
267                        (function (i) {
268                            setTimeout(function timer() {
269                                if (navigator.userAgent.indexOf('Trident') !== -1) { // ie浏览器 或者IE兼容模式处理fixed定位元素
270                                    if (arr[i].sync) {
271                                        (function (k) {
272                                            timer[k + 'xxx'] = setInterval(function () {
273                                                if ($(arr[k].name).length > 0) {
274                                                    clearInterval(timer[k + 'xxx'])
275                                                    $(arr[k].name).css({
276                                                        position: 'fixed'
277                                                    })
278                                                }
279                                            }, 0)
280                                        })(i);
281                                    } else {
282                                        $(arr[i].name).css({
283                                            position: 'fixed'
284                                        })
285                                    }
286                                }
287                            }, 310);
288                        })(j);
289                    }
290                }
291            },
292            // 添加事件
293            events: function () {
294                var that = this
295                // 重置
296                this.element.find(".toolbar .toolbar_reset").click(function () {
297                    that.changeZoom(1);
298                });
299                // 放大
300                this.element.find(".toolbar .toolbar_amplification").click(function () {
301                    that.changeZoom('+');
302                });
303                // 缩小
304                this.element.find(".toolbar .toolbar_narrow").click(function () {
305                    that.changeZoom('-');
306                });
307                // 退出
308                this.element.find(".toolbar .toolbar_exit").click(function () {
309                    that.changeZoom(1);
310                    that.hideToolbar();
311                });
312                // 开关
313                this.element.find(".bar-switch-button").click(function () {
314                    that.toggleToolbar()
315                });
316            },
317            // 回调函数
318            callBack: function (fn, number) {
319                if (this.settings.callback && typeof this.settings.callback == "function") {
320                    this.settings.callback(fn, number)
321                }
322            }
323        };
324        return toolbar;
325    })();
326// 创建对象实例
327    $.fn.toolbar = function (options, param) {
328        // 实现链式调用
329        return this.each(function () {
330            var me = $(this),
331                instance = me.data("toolbar");
332            // 判断实例是否存在,不存在则创建对象,并将该对象及配置项传入
333            if (!instance) {
334                instance = new toolbar(me, options);
335                // 存放实例
336                me.data("toolbar", instance);
337            }
338            // 在外部可以通过$(selection).toolbar("init")
339            if ($.type(options) === "String" || $.type(options) === "string") {
340                return instance[options](param);
341            }
342        });
343    };
344// 配置参数
345    $.fn.toolbar.options = {
346        zoomDom: '.zoomElement', // 放大的容器元素
347        zoomMax: 2, // 缩放最大比例
348        zoomMin: 1, // 缩放最小比例
349        zoomStep: 0.1, // 点击缩放
350        switchBtn: true, // 工具栏开关
351        switchBtnPosTop: '50%', // 侧边栏开关定位top值
352        domain: '.cpic.com.cn', // 设置cookie
353        callback: '', // 工具栏操作触发回调
354        fixedDomArr: [], // 针对原来fixed定位元素处理,以兼容IE和IE兼容模式浏览器
355    };
356})(jQuery, window, document)

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.