PageSourceSearch

https://www.szhdgc.com/static/home/default/common.js

js szhdgc.com collected 2026-10-09 04:04:20 UTC 11,503 bytes, 333 lines download raw bytes

1/**
2 * common.js
3 * 依赖jquery
4 * Copyright (c) 2003-2025 互诺科技
5 * https://www.hunuo.com
6 * 400-660-5510
7 * @author linjianwei
8 */
9window.gb = {
10    /**
11     * 语言点击,跳转对应语言的当前页面
12     * @param {String} targetDir 目标目录: ['home', 'cn', 'en', 'jp', 'de', ...]
13     * @param {MouseEvent} e MouseEvent
14     * @example <a href="" onclick="gb.setLang('{$v.website_dir}', event)"></a> 
15     */
16    setLang: function(targetDir, e) {
17        e && e.preventDefault && e.preventDefault();
18        var currentDir = window.lang_dir || 'home'
19        // 已经是当前语言,无需跳转
20        if (targetDir == currentDir) {
21            return; 
22        }
23        // url路径转成数组,path[1] - 当前语言目录
24        var path = location.pathname.split('/');
25        // 如果 home 默认省略了, 补回 home。  ['', 'about'] => ['', 'home', 'about']
26        if (currentDir == 'home' && path[1] != 'home') {
27            path.splice(1, 0, 'home');
28        }
29        // 改成目标语言目录
30        path[1] = targetDir;
31        // 默认的home可以不显示
32        if (path[1] == 'home') {
33            path.splice(1, 1);
34        }
35        var newPath = path.join('/').replace(/\/\//g, '/');
36        location.href = newPath;
37    },
38    /**
39     * 设置站点语言
40     * @param {String} lang_template 目录: ['home', 'cn', 'en', 'jp', 'de', ...]
41     */
42    // setLangByAjax: function (lang_template) {
43    //     hn.load();
44    //     $.ajax({
45    //         url: "/Public/changeLang",
46    //         type: "POST",
47    //         data: {
48    //             lang: lang_template,
49    //             now_uri: window.location.href
50    //         },
51    //         dataType: "json",
52    //         success: function (res) {
53    //             hn.hideLoad();
54    //             hn.msg('Operation successful', 1, function () {
55    //                 window.location.href = res.data.url;
56    //             });
57    //         }
58    //     })
59    // },
60    /**
61     * 添加文本到剪切板
62     * @param {String} text 
63     */
64    copyTextToClipboard: function (text) {
65        if (navigator.clipboard) {
66            this.copyTextToClipboard = function (text) {
67                try {
68                    navigator.clipboard.writeText(text);
69                    console.log('Text copied to clipboard');
70                } catch (err) {
71                    console.error('Failed to copy: ', err);
72                }
73            }
74        } else {
75            this.copyTextToClipboard = function (text) {
76                var textArea = document.createElement("textarea");
77                textArea.value = text;
78                document.body.prepend(textArea);
79                textArea.focus();
80                textArea.select();
81                try {
82                    var successful = document.execCommand('copy');
83                    var msg = successful ? 'successful' : 'unsuccessful';
84                    console.log('Copying text command was ' + msg);
85                } catch (err) {
86                    console.error('Unable to copy text', err);
87                }
88                document.body.removeChild(textArea);
89            }
90        }
91        this.copyTextToClipboard(text)
92    },
93    /**
94     * 格式化筛选参数
95     * @param {HTMLFormElement} formEl 表单html
96     * @returns {string} 选中的多个值,以_连接
97     */
98    formatFilter: function (formEl) {
99        var arr = $(formEl).serializeArray()
100        var res = []
101        arr.forEach(function (item) {
102            res.push(item.value)
103        })
104        return res.join('_')
105    },
106    // 防抖
107    debounce: function(func, wait) {
108        var timeout;
109        return function () {
110            var context = this;
111            var args = arguments;
112
113            if (timeout) clearTimeout(timeout);
114
115            timeout = setTimeout(function () {
116                func.apply(context, args)
117            }, wait);
118        }
119    },
120    /**
121     * 初始化swiper-slide,一列num个
122     * @param {String} selector 元素选择器
123     * @param {Number} num 一个swiper-slide里要保护的元素个数
124     * @param {String} pClass 父元素,支持多级父元素,空格表示多层级,每层类名中间加.表示多个类名,例如
125     * @example swiper-slide.ul ul  ==> 
126     * <div class="swiper-slide ul">
127     *      <div class="ul"></div>
128     * </div>
129     * @returns null
130     */
131    f_initSlide: function(selector, num, pClass){
132        try{
133            num = Number(num);
134        } catch(err){
135            return;
136        }
137        if (!selector) {
138            return;
139        }
140        var classNum = 0;
141        var count = 0;
142        var $container = $(selector).parent()
143        $(selector).each(function(i, e){
144            count++;
145            if(count > num){
146                classNum++;
147                count = 1;
148            }
149            $(e).addClass('col-' + classNum);
150        })
151        var classList = []
152        if (pClass) {
153            classList = pClass.split(' ').map(function(el) {
154                return el.split('.').join(' ')
155            })
156        } else {
157            classList = ['swiper-slide']
158        }
159        for (var i = 0; i <= classNum; i++) {
160            var $list = $container.find('.col-' + i)
161            for (var j = 0; j < classList.length; j++) {
162                var name = classList[j]
163                var _html = '<div class="' + name + '"></div>'
164                $list = $list.wrapAll(_html)
165            }
166        }
167    },
168    /**
169     * 滚动到某个id元素
170     * @param {string} hashString 哈希字符串,可以传location.hash 或 '#{id}' 如:'#section-1'
171     * @param {Number} dur 时长,ms
172     * @param {Number} offset 顶部距离, 如头部导航的高度
173     * @example 
174     * gb.scrollToHash(location.hash); // 页面打开后滚动到指定板块
175     */
176    scrollToHash: function(hashString, dur, offset) {
177        var id = hashString.split('#')[1]
178        if (id) {
179            var $dom = $('#' + id);
180            if ($dom.length > 0) {
181                if (isNaN(Number(offset))) {
182                    offset = 0
183                }
184                var _top = $dom.offset().top
185                if (isNaN(Number(dur))) {
186                    dur = _top
187                    dur = Math.min(dur, 1000)
188                }
189                $("html,body").stop(true).animate({
190                    scrollTop: _top - offset
191                }, dur)
192            }
193        }
194    },
195    /**
196     * 滚动到某个元素
197     * @param {jQuery} $dom 元素jquery对象
198     * @param {Number} dur 时长,ms
199     * @param {Number} offset 顶部距离, 如头部导航的高度
200     * @example 
201     * gb.scrollToElement($('#section-1')); // 滚动到id为section-1的元素
202     */
203    scrollToElement: function($dom, dur, offset) {
204        if ($dom.length > 0) {
205            if (isNaN(Number(offset))) {
206                offset = 0
207            }
208            var _top = $dom.offset().top
209            if (isNaN(Number(dur))) {
210                dur = _top
211                dur = Math.min(dur, 1000)
212            }
213            $("html,body").stop(true).animate({
214                scrollTop: _top - offset
215            }, dur)
216        }
217    },
218    /**
219     * 证书放大预览
220     * @param {Object} options 选项
221     * @param {jQuery} options.img 图片jquery对象
222     * @param {jQuery} options.clickItem 点击元素jquery对象
223     * @example gb.viewImgList({
224            img: $('.list img'),
225            clickItem: $(".list .swiper-slide")
226        })
227     */
228    viewImgList: function(options){
229        if (options === null || typeof options !== 'object' || !options.img || !
229options.clickItem) {
230            return;
231        }
232        var $imgs = options.img
233        var $clickItem = options.clickItem
234        var imgDataList = [];
235        var gallery;
236        // 调用相册插件
237        function view(currentIndex) {
238            var pswpElement = document.querySelectorAll(".pswp")[0];
239            var options = {
240                index: currentIndex,
241                shareEl: false, // 分享
242                fullscreenEl: true, // 全屏
243            };
244            gallery = new PhotoSwipe(
245                pswpElement,
246                PhotoSwipeUI_Default,
247                imgDataList,
248                options
249            );
250            gallery.init();
251            // gallery.listen('preventDragEvent', function(e, isDown, preventObj) {
252            //     // e - original event
253            //     // isDown - true = drag start, false = drag release
254            
255            //     // Line below will force e.preventDefault() on:
256            //     // touchstart/mousedown/pointerdown events
257            //     // as well as on:
258            //     // touchend/mouseup/pointerup events
259            //     preventObj.prevent = false;
260            // }); 
261        }
262        // 加载单张图片
263        function loadOne(i, callback) {
264            if (typeof callback !== 'function') {
265                callback = function() {}
266            }
267            var item = imgDataList[i];
268            if (item.loaded) {
269                callback();
270                return;
271            }
272            var img = new Image();
273            img.src = item.src;
274
275            img.onload = function () {
276                item.loaded = true;
277                item.w = img.width;
278                item.h = img.height;
279                callback();
280            }
281            img.onerror = function () {
282                callback();
283            }
284        }
285        // 加载当前序号附近的图片
286        function loadImg(currentIndex) {
287            // 加载当前序号之后的图片
288            for (var i = currentIndex + 1; i < imgDataList.length; i++) {
289                loadOne(i);
290            }
291            // 加载当前序号之前的图片
292            for (var i = currentIndex - 1; i >= 0; i--) {
293                loadOne(i);
294            }
295        }
296        // 去重
297        $clickItem.each(function(idx, item) {
298            var $img = $imgs.eq(idx);
299            var $slide_1 = $(item).closest(".swiper-slide");
300            var $slide_2 = $img.closest(".swiper-slide");
301            // 剔除swiper复制出来的slide
302            if ($slide_1[0] === $slide_2[0] && !$slide_1.hasClass("swiper-slide-duplicate")) {
303                imgDataList.push({
304                    src: $img.attr("src") || $img.attr("data-src") || $img.attr("data-lazy") || $img.attr("data-original"),
305                    w: $img.data("w") || $(window).width(), // 需要有默认值,不然插件会报错
306                    h: $img.data("h") || $(window).height(), // 需要有默认值,不然插件会报错
307                    loaded: false
308                })
309            }
310        })
311        // 绑定点击事件
312        $clickItem.on("click", function (e) {
313            var realIndex = $(this).index();
314            // 兼容swiper设置了loop
315            if ($(this).data("swiper-slide-index") !== undefined) {
316                realIndex = $(this).data("swiper-slide-index")
317            }
318            console.log(realIndex);
319            hn.load();
320            // 加载当前图片
321            loadOne(realIndex, function() {
322                // 加载其他图片
323                loadImg(realIndex);
324                // 显示相册
325                view(realIndex);
326                // 取消loading
327                setTimeout(function() {
328                    hn.hideLoad();
329                }, 100);
330            });
331        })
332    }
333}

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.