(function(window) { var ys = {}; if (window.ys) { ys = window.ys }; // 滚动条隐藏与显示 var scrollbar = { $html: $("html"), $body: $("body"), originalBodyPad: null, scrollbarWidth: 0, hide: function() { this.checkScrollbar(); this.setScrollbar(); this.$html.css("overflow", "hidden"); // if (this.ismobilesSafari()) { // this.$html.css({ // width: "100%", // position: "fixed" // }); // } }, show: function() { this.$html.removeAttr("style"); this.resetScrollbar(); }, checkScrollbar: function() { var fullWindowWidth = window.innerWidth if (!fullWindowWidth) { // workaround for missing window.innerWidth in IE8 var documentElementRect = document.documentElement.getBoundingClientRect(); fullWindowWidth = documentElementRect.right - Math.abs(documentElementRect.left); } this.bodyIsOverflowing = document.body.clientWidth < fullWindowWidth; this.scrollbarWidth = this.measureScrollbar(); }, setScrollbar: function() { var bodyPad = parseInt((this.$body.css('padding-right') || 0), 10) this.originalBodyPad = document.body.style.paddingRight || '' if (this.bodyIsOverflowing) this.$body.css('padding-right', bodyPad + this .scrollbarWidth) }, measureScrollbar: function() { var scrollDiv = document.createElement('div'); scrollDiv.style.position = "absolute"; scrollDiv.style.top = "-9999px"; scrollDiv.style.width = "50px"; scrollDiv.style.height = "50px"; scrollDiv.style.overflow = "scroll"; this.$body.append(scrollDiv); var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth; this.$body[0].removeChild(scrollDiv); return scrollbarWidth; }, resetScrollbar: function() { this.$body.css('padding-right', this.originalBodyPad); }, ismobilesSafari: function() { var ua = navigator.userAgent.toLowerCase(); var isChrome = ua.indexOf("chrome") != -1; var isSafari = ua.indexOf("safari") != -1; var ismobiles = ua.indexOf("safari") != -1; if (!isChrome && isSafari && ismobiles) { return 1; } else { return 0; } } }; ys.scrollbar = scrollbar; /** * * @description 回到顶部 * @param {Number} duration - 时间(ms) * */ function goTop(duration) { var duration = duration || 500; $("html,body").stop().animate({ scrollTop: 0, }, duration) }; ys.goTop = goTop; /** * * @description 当滚动条离开最顶部给选择元素加一个类名isfixed * @param {String} selector - css选择器 * */ function isFixed(selector) { var $obj = $(selector); $(function() { judgeTop() }); $(window).on("scroll", judgeTop); $(window).on("load", judgeTop); $(window).on("resize", judgeTop); function judgeTop() { if ($(window).scrollTop() > 0) { $obj.addClass("isfixed"); } else { $obj.removeClass("isfixed"); } } } ys.isFixed = isFixed; /** * * @description banner轮播视频初始化 * @param {String} selector - css选择器 * */ function banVideo(selector) { var $obj = $(selector); if ($(window).width() > 1200 && $obj.find("video").length > 0) { init(); function init() { firstVideo(); videoEnd(); afterChange(); } // 检验第一屏是否有视频 function firstVideo() { var $firSlide = $obj.find(".slick-slide[data-slick-index=0]"); var $video = $firSlide.find("video"); // autoplay自动播放 if ($video.length > 0) { $obj.slick('slickPause'); // 如果轮播只有一个循环播放 if ($obj.get(0).slick.slideCount < 2) { $video.attr("loop", "loop"); } if ($video.get(0).paused) { setTimeout(function() { $video.get(0).play(); }, 100) } } else { $obj.slick('slickPlay'); } } // 切换到下一张是否有视频 function afterChange() { $obj.on('afterChange', function(event, slick, index) { // 视频暂停并回到第一帧 $obj.find("video").each(function() { var video = $(this).get(0); video.pause(); video.currentTime = 0; }); // 有视频则暂停轮播 var $nowSlide = $(slick.$slides[index]); var $video = $nowSlide.find("video"); if ($video.length > 0) { $obj.slick('slickPause'); $video.get(0).play(); }else{ $obj.slick('slickPlay'); } }); } // 视频播完切换到下一张 function videoEnd() { $obj.find("video").each(function() { var video = $(this).get(0); video.onended = function() { $obj.slick("next"); $obj.slick('slickPlay'); }; }); }; } else { $obj.slick('play'); } }; ys.banVideo = banVideo; /** * * @description 下拉选择 * @param {String} selector - css选择器 * */ function select(selector, callback) { var $obj = $(selector); $obj.find(".ys_select_li").each(function() { var index = $(this).index(); if ($(this).attr("data-id") == undefined) { $(this).attr("data-id", index); } $(this).attr("data-value", $(this).text()); }); $obj.find(".ys_select_hd").click(function(e) { e.stopPropagation(); // var target = $(this).siblings(".ys_select_bd").get(0); $(this).toggleClass("on"); if ($(this).parents(".ys_select").hasClass("zindex")) { //收回 $(this).siblings(".ys_select_bd").removeClass("showscroll"); $(this).siblings(".ys_select_bd").stop().slideUp(function() { $(this).parents(".ys_select").removeClass("zindex"); }); } else { //展开 $(this).parents(".ys_select").addClass("zindex"); $(this).siblings(".ys_select_bd").stop().slideDown(function() { $(this).mCustomScrollbar("update"); $(this).addClass("showscroll"); }); } $(".ys_select_hd").not(this).removeClass("on"); $(".ys_select_hd").not(this).siblings(".ys_select_bd").removeClass("showscroll").stop().slideUp(); $(".ys_select_hd").not(this).parents(".ys_select").removeClass("zindex"); }) $obj.find(".ys_select_bd").on("click", ".ys_select_li", function() { var selectShow = $(this).text(); var realVal = $(this).data("value"); var data = $(this).data(); $(this).parents(".ys_select_bd").siblings(".ys_select_hd").find(".ys_select_show").val(selectShow); $(this).parents(".ys_select_bd").siblings(".ys_select_hd").find(".ys_select_real").val(realVal).trigger("change"); if (callback) { callback(data); } }); // 点击页面关闭 $("body").click(function() { $(".ys_select_hd").removeClass("on"); $(".ys_select_bd").removeClass("showscroll").stop().slideUp(function() { $(this).parents(".ys_select").removeClass("zindex"); }); }); // 滚动条 $obj.find(".ys_select_bd").mCustomScrollbar({ axis: "y", scrollInertia: 500, theme: 'dark', scrollbarPosition: "outside" }); }; ys.select = select; /** * * @description 弹窗蒙层 * @param {String} btn 弹窗触发按钮(选择器) * @param {String} modal 对应弹窗(选择器) * @param {String} scrollEle 滚动条元素(选择器) * @param {Boolean} modalClose 点击蒙层非内容部分是否可关闭(默认为否) * */ function modal(btn, modal, scrollEle, modalClose) { var $modal = $(modal); // 显示 $("body").on("click", btn, function() { $modal.stop().fadeIn(); scrollbar.hide(); }); // 关闭 $("body").on("click", ".ys_modal_close", function() { $(this).parents(modal).stop().fadeOut(function() { scrollbar.show(); }); }); // 滚动条 var scrollEle = scrollEle ? scrollEle : ".ys_modal_content"; if (scrollEle != "noscroll") { $modal.find(scrollEle).mCustomScrollbar({ axis: "y", scrollInertia: 500, theme: 'dark', }); } else { $modal.find(".ys_modal_content").addClass("noscroll") } }; ys.modal = modal; /** * * @description 视频弹窗 * @param {selector} selector - css选择器 * @param {Boolean} autoplay - 是否弹窗自动播放video * */ function videomodal(selector, autoplay) { var $videomodal; // 插入视频模版 if ($(".ys_videom").length > 0) { $videomodal = $(".ys_videom"); } else { var videomodal = '
' + '
' + '' + '
' + '
' + '
' + '' + '
' + '
' + '
' + '
'; $videomodal = $(videomodal); $("body").append($videomodal); } var Vmodal = { $self: $videomodal, close: function() { $videomodal.find(".ys_videom_video").attr("src", "").attr("poster", "").stop().hide().get(0).pause(); $videomodal.find(".ys_videom_iframebox").stop().hide().find("iframe").remove(); $videomodal.stop().fadeOut(function() { scrollbar.show(); }); }, show: function() { scrollbar.hide(); $videomodal.stop().fadeIn(); } } $("body").on("click", selector, function() { // 视频类型是video还是iframe var videoType = $(this).attr("data-type"); //src var videoSrc = $(this).attr("data-src"); console.log($(this)); if (videoType == "video") { // 视频 //video视频封面图 var videoPoster = $(this).data("poster"); $videomodal.find(".ys_videom_video").stop().show().attr("src", videoSrc).attr("poster", videoPoster); if (autoplay) { $videomodal.find(".ys_videom_video").get(0).play(); } } else if (videoType == "iframe") { // iframe $videomodal.find(".ys_videom_iframebox").stop().show().html(videoSrc); } else { return; }; Vmodal.show(); }); $videomodal.on("click", ".ys_videom_close", function() { Vmodal.close(); }); } ys.videomodal = videomodal; /** * * @description 滚动条初始化 * @param {String} selector - css选择器 * @param {Boolean} inside - 滚动条占用内容区 * */ function mCustomScrollbarInit(selector, inside) { var $obj = $(selector); scrollbarPosition = inside ? "inside" : "outside"; $obj.mCustomScrollbar({ axis: "y", scrollInertia: 500, theme: 'dark', scrollbarPosition: scrollbarPosition, }); }; ys.mCustomScrollbarInit = mCustomScrollbarInit; /** * * @description 数字滚动 * @param {String} selector - css选择器 * @param {Boolean} addZero - 是否补0 * @param {Number} speed - 滚动速度(ms) * */ function numRoll(selector, addZero, speed) { var $ele = $(selector); var speed = speed || 1000; $ele.appear(function() { var realContent = $(this).text(); $(this).width($(this).width()); var content = $(this).data("to"); var to = parseFloat(content.toString().replace(/,/g, "")); var length = parseInt(to).toString().length; var hasDou = false; var contentArr = content.toString().split(","); if (contentArr[1]) { hasDou = true; }; if (addZero) { formatter = function(value, options) { var myValue = value.toFixed(options.decimals); var valLength = parseInt(myValue).toString().length; for (var i = 0; i < (length - valLength); i++) { myValue = "0" + myValue; } if (hasDou) { myValue = toThousands(myValue); } return myValue; } } else { formatter = function(value, options) { var myValue = value.toFixed(options.decimals); if (hasDou) { myValue = toThousands(myValue); } return myValue; } } function toThousands(num) { return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); } $(this).countTo({ speed: speed, refreshInterval: 60, formatter: formatter, onComplete: function() { $(this).removeAttr("style"); $(this).text(realContent); } }); }); } ys.numRoll = numRoll; /** * * @description 解决bshare乱码 * @param {String} title - 分享的标题文字 * @param {String} summary - 分享的内容文字 * */ function shareInit(title, summary) { var title = title ? encodeURI(title) : encodeURI($("h1").text()); var summary = summary ? encodeURI(summary) : encodeURI($("p").eq(0).text()); bShare.addEntry({ title: title, summary: summary }); // 微信弹窗 if ($(".bshare-weixin").length > 0) { $(".bshare-weixin").click(function() { scrollbar.hide(); }); } $("body").on("click", ".bsClose", function() { scrollbar.show(); }); if ($(".bshare-sinaminiblog").length > 0) { var urlf = (window.locations.href); //var link = 'https://service.weibo.com/share/share.php?url=' + urlf + '&title=' + title + ' - ' + summary; var link = 'https://service.weibo.com/share/share.php?url=' + urlf + '&title=' + title ; var atitle = $(".bshare-sinaminiblog").attr("title"); // $(".bshare-sinaminiblog").attr("href", link); $(".bshare-sinaminiblog").css("pointer-events", "none"); $(".bshare-sinaminiblog").parent().attr("title", atitle).css("cursor", "pointer").click(function() { window.open(link, "_blank"); }); } } ys.shareInit = shareInit; /** * * @description 移动端导航初始化 * @param {Number} style - * */ function phNavInit(style) { switch (style) { case 1: phNavInit1(); break; case 2: phNavInit2(); break; case 3: phNavInit3(); break; case 4: phNavInit4(); break; default: phNavInit1(); } function phNavInit1() { // menu按钮点击 $(".ys_phnav_menubtn").click(function() { // 显示隐藏滚动条 if ($(this).hasClass("on")) { ys.scrollbar.show(); } else { ys.scrollbar.hide(); } // 按钮样式 $(this).toggleClass("on"); // 下拉展开 $(".ys_phnav_menu").stop().slideToggle(); }); // nav点击展开 $(".ys_phnav_lisbox").click(function() { var $subNav = $(this).siblings("ul"); if ($subNav.length > 0) { $(this).find(".ys_phnav_icon").toggleClass("on"); $subNav.stop().slideToggle(); } $(this).parent().siblings().find("ul").stop().slideUp(); $(this).parent().siblings().find(".ys_phnav_icon").removeClass("on"); }); } function phNavInit2() { // 搜索下拉 $(".ys_phnavss_btn").click(function(e) { e.stopPropagation(); $(".ys_phnavss_xl").stop().slideToggle(); if ($(".ys_phnav_menubtn").hasClass("on")) { $(".ys_phnav_menubtn").trigger("click"); } $(this).toggleClass("on"); }); $(".ys_phnavss_xl").click(function(e) { e.stopPropagation(); }); $("body").click(function() { $(".ys_phnavss_xl").stop().slideUp(); }) // menu按钮点击 $(".ys_phnav_menubtn").click(function(e) { e.stopPropagation(); if ($(".ys_phnavss_btn").hasClass("on")) { $(".ys_phnavss_btn").trigger("click"); } // 显示隐藏滚动条 if ($(this).hasClass("on")) { ys.scrollbar.show(); } else { ys.scrollbar.hide(); $(".ys_phnav2_lang_modal").removeClass("on"); } // 按钮样式 $(this).toggleClass("on"); // 下拉展开 $(".ys_phnav_menu").toggleClass("on"); }); // nav点击展开 $(".ys_phnav_lisbox").click(function() { var $subNav = $(this).siblings("ul"); if ($subNav.length > 0) { $(this).find(".ys_phnav_icon").toggleClass("on"); $subNav.stop().slideToggle(); } $(this).parent().siblings().find("ul").stop().slideUp(); $(this).parent().siblings().find(".ys_phnav_icon").removeClass("on"); }); // 语言弹框 $(".ys_phnav2_lang_btn").click(function() { $(".ys_phnav2_lang_modal").addClass("on"); console.log(1); }); $(".ys_phnav2_lang_back").click(function() { $(".ys_phnav2_lang_modal").removeClass("on"); }); } function phNavInit3() { // 搜索弹窗 ys.modal(".ys_phnavss_btn", ".ys_phnavss_modal"); // menu按钮点击 $(".ys_phnav_menubtn").click(function() { // 隐藏滚动条 ys.scrollbar.hide(); // 按钮样式 $(this).addClass("on"); $(".ys_phnav_menu").addClass("on"); }); //menu关闭按钮点击 $(".ys_phmenu_close").click(function() { // 显示滚动条 ys.scrollbar.show(); // 按钮样式 $(".ys_phnav_menubtn").removeClass("on"); $(".ys_phnav_menu").removeClass("on"); }); // nav点击展开 $(".ys_phnav_lisbox").click(function() { var $subNav = $(this).siblings(".ys_phsubnav_modal"); if ($subNav.length > 0) { $subNav.addClass("on"); } }); // nav返回点击 $(".ys_phsubnav_close").click(function() { $(this).parents(".ys_phsubnav_modal").eq(0).removeClass("on"); }); // 语言下拉 $(".ys_phnav3_lang_hd").click(function() { $(this).toggleClass("on"); $(".ys_phnav3_lang_bd").stop().slideToggle(); }); } function phNavInit4() { // 搜索弹窗 ys.modal(".ys_phnavss_btn", ".ys_phnavss_modal") // menu按钮点击 $(".ys_phnav_menubtn").click(function() { // 显示隐藏滚动条 if ($(this).hasClass("on")) { ys.scrollbar.show(); } else { ys.scrollbar.hide(); } // 按钮样式 $(this).toggleClass("on"); // 下拉展开 $(".ys_phnav_menu").stop().slideToggle(); }); // nav点击展开 $(".ys_phnav_lisbox").click(function() { var $subNav = $(this).siblings("ul"); if ($subNav.length > 0) { $(this).find(".ys_phnav_icon").toggleClass("on"); $subNav.stop().slideToggle(); } $(this).parent().siblings().find("ul").stop().slideUp(); $(this).parent().siblings().find(".ys_phnav_icon").removeClass("on"); }); } } ys.phNavInit = phNavInit; /** * * @description 通过js调用animate.css的动画 * @param {String} ele - 动画的元素 * @param {String} animate - 动画的名字 * @param {Function} callback - 动画执行完的回调函数 * */ function animateCss(ele, animate, callback) { var $ele = $(ele); var duration = $ele.css("animation-duration"); duration == '0s' ? duration = 1000 : duration = parseFloat(duration) * 1000; $ele.addClass('animated ' + animate); if (callback == "show") { $ele.stop().show(); }; setTimeout(function() { if (callback == "hide") { $ele.stop().hide(); } else if (callback != "show" && callback) { callback(); } $ele.removeClass(animate); }, duration); }; ys.animateCss = animateCss; //检查页面是否缩放 function checkViewport() { //FF 谷歌 if (window.devicePixelRatio > 1 || window.devicePixelRatio < 1) { alert('当前页面缩放比例异常,请设置成100%') } //IE if (window.screen.deviceXDPI != window.screen.logicalXDPI) { alert('当前页面缩放比例异常,请设置成100%') } } ys.checkViewport = checkViewport; // 重新加载main.css和midea.css function reloadCss() { $("link").each(function() { var href = $(this).attr("href"); if (href.indexOf("main.css") > 0) { $(this).attr("href", 'css/main.css?' + new Date().getTime()); } if (href.indexOf("media.css") > 0) { $(this).attr("href", 'css/media.css?' + new Date().getTime()); } }); } ys.reloadCss = reloadCss; // 暂停所有slick轮播 function stopSlick() { $(".slick-slider").slick("pause"); } ys.stopSlick = stopSlick; /** * * @description 锚点定位 * @param {String} reg - 路由拼接 * @param {Number} navHeight - pc导航高度 */ function yxtop(reg, navHeight) { $(window).on('load', function() { var reg = reg || "?tp="; var navHeight = navHeight || $(".ys_hd_pc").height() || 0; if ($(window).width() < 1200) { navHeight = 64; } var strArr = (window.locations.href).split(reg); if (!isNaN(strArr[1])) { $("html,body").animate({ scrollTop: $('[data-page="' + strArr[1] + '"]').offset().top - navHeight }, 700); } }) } ys.yxtop = yxtop; window.ys = ys; }(window))