window.ebg = window.ebg || function () {}; window.console = window.console || { log: function () {}, warn: function () {}, error: function () {}, info: function () {} } // 有懒加载,大屏小屏的替换图片 ebg.prototype.eReplaceImg = function () { function init() { if ($('.e-replace-img').length > 0) { var winw = $(window).width(); $('.e-replace-img').each(function () { var $this = $(this); var src = $this.attr("src"); var img1920 = $this.data("xl"); var img1600 = $this.data("lg"); var img1200 = $this.data("md"); var img992 = $this.data("sm"); var img750 = $this.data("xs"); if (winw > 1600) { $this.attr('data-original', img1920 || img1600 || img1200 || img992 || img750 || src); } else if (winw > 1200) { $this.attr('data-original', img1600 || img1920 || img1200 || img992 || img750 || src); } else if (winw > 992) { $this.attr('data-original', img1200 || img1600 || img1920 || img992 || img750 || src); } else if (winw > 750) { $this.attr('data-original', img992 || img750 || img1200 || img1600 || img1920 || src); } else { $this.attr('data-original', img750 || img992 || img1200 || img1600 || img1920 || src); } }); }; } //对外重新初始化 $("body").on("eReplaceImg", function () { init(); }); init(); } // 计算同行等高方法,优化版 // 如果层级只有两级,传2个参数,如果层级有三级,传3个参数, ebg.prototype.v3SetHeight = function (obj, obj2, obj3) { if (!this.resizeStackLock && this.resizeStack) { this.resizeStack.push({ fn: this.v3SetHeight, params: arguments }); } var This = $(obj); var args = arguments; This.each(function () { var _this = $(this), oList = _this.find(obj2), oList_len = oList.length, oBox_w = _this.width() + 10, oList_w = oList.width(), oColumn_len = Math.floor(oBox_w / oList_w), oRow_len = Math.ceil(oList_len / oColumn_len), oList_H = 0, oList_obj = null; if (args.length == 2) { oList.height('auto'); } else { oList.find(obj3).height('auto'); } for (var i = 0; i < oRow_len; i++) { for (var j = i * oColumn_len; j < oColumn_len * (i + 1); j++) { oList.eq(j).addClass('oList_obj'); if (args.length == 2) { var H = oList.eq(j).height(); } else { var H = oList.eq(j).find(obj3).height(); } H > oList_H ? oList_H = H : oList_H; oList_obj = $('.oList_obj'); } if (args.length == 2) { oList_obj.height(oList_H); } else { oList_obj.find(obj3).height(oList_H); } oList_obj.removeClass('oList_obj'); oList_H = 0; } }); } // 懒加载 ebg.prototype.lazy = function () { if ($('.e-lazy img').length > 0) { function lazyloadClearBg(obj, className) { var _this = $(obj); if (_this.parents(".e-bg").length && _this.parents(".e-bg").data("replace") != "not") { //请避免路径中存在空格 _this.parents(".e-bg").css("background-image", "url(" + _this.data("original") + ")"); _this.parents(className).hide(); } if (_this.parents(className)) { _this.attr("src", _this.data("original")); _this.parents(className).css({ 'height': 'auto', 'background': 'none' }).addClass('over'); } } // 初始化 var lazyLazy = new LazyLoad({ elements_selector: ".e-lazy img", data_src: "original", callback_loaded: function (obj) { lazyloadClearBg(obj, ".e-lazy"); }, callback_enter: function (obj) { lazyloadClearBg(obj, ".e-lazy"); } }); } } // 页面自适应拉动屏幕,强制刷新 // ebg.prototype.pageResize = function () { // $(window).on('resize.page20220927', function () { // $("html,body").scrollTop(0); // location.reload(); // }) // } // 刷新页面直接回到顶部 ebg.prototype.pageTop = function () { if ('scrollRestoration' in history) { history.scrollRestoration = 'manual'; } } // 检测浏览器的版本 ebg.prototype.browserVersion = function () { var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 if ((userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) { var isIos = !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //判断移动端浏览器 var isAndroid = userAgent.indexOf('Android') > -1 || userAgent.indexOf('Adr') > -1; //判断移动端浏览器 if (isIos) { return "ios"; } if (isAndroid) { if (userAgent.match(/HUAWEI/i) && userAgent.match(/MQQBrowser/i)) { return "huawei"; } else if (userAgent.match(/HONOR/i) && userAgent.match(/MQQBrowser/i)) { return "honor"; } else { return "android"; } } } else { var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器 var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器 var isIE11 = userAgent.indexOf("rv:11") > -1; var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器 var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器 var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器 var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Edge") == -1; //判断Chrome浏览器 if (isIE) { var reIE = new RegExp("MSIE (\\d+\\.\\d+);"); reIE.test(userAgent); var fIEVersion = parseFloat(RegExp["$1"]); switch (fIEVersion) { case 6: return "6"; case 7: return "7"; case 8: return "8"; case 9: return "9"; case 10: return "10"; case 11: return "11"; default: return "0"; //IE版本过低 } } if (isFF) { return "FF"; } if (isOpera) { return "Opera"; } if (isSafari) { return "Safari"; } if (isChrome) { return "Chrome"; } if (isEdge) { return "Edge"; } if (isIE11) { return '11' } } } var ebgLib = new ebg(); ebgLib.eReplaceImg(); //大屏小屏的图片替换 ebgLib.v3SetHeight(); //大屏小屏的图片替换 ebgLib.lazy(); //懒加载 // ebgLib.pageResize(); //页面自适应拉动屏幕,强制刷新 ebgLib.pageTop(); // 刷新页面直接回到顶部 ebgLib.browserVersion(); // 检测浏览器的版本 // 检测是哪个浏览器打开的,根据不同的浏览器,给页面html最外层的html元素添加class if (ebgLib.browserVersion()) { if (ebgLib.browserVersion() <= 11) { $('html').addClass('ie' + ebgLib.browserVersion()); } else { $('html').addClass(ebgLib.browserVersion()); } }