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.