// 处于页面配置时,绑定方法 start if ($isDecorateMode) { document.addEventListener("diy_header/1", (event) => { const { field, value } = event.detail; console.log(field, value) if (field === 'hots') { let html = '' value.forEach(e => { html += `${e.name}` }) $('.' + field).html(html) } }); } // 处于页面配置时,绑定方法 end $(function () { $("#logout").click(function () { $request .post("/api/user/logout") .then((res) => { // 跳转页面 location.href = "/"; }); }) // 防止一开始进入页面在底部不显示 setHeaderSticky() $(window).scroll(setHeaderSticky) function setHeaderSticky(){ let offset = $("html,body").scrollTop() if (offset) { $('.ys-header').addClass('is-sticky') } else { $('.ys-header').removeClass('is-sticky') } } // 导航栏悬停 $('.ys-header__nav .nav-item').hover(function () { $(this).find('.ys-header__card').addClass('show') if(!$(this).hasClass('ignore')) $('.ys-header__cover').removeClass('display-none') }, function () { $(this).find('.ys-header__card').removeClass('show') $('.ys-header__cover').addClass('display-none') }) // 产品中心悬停 $('.ys-header__nav .product-item').hover(function () { $('.ys-header__nav .product-child').addClass('display-none') $('.ys-header__nav .product-item').removeClass('active') $(this).addClass('active') $(this).next().removeClass('display-none') }) // 搜索框悬停事件 $('.ys-header__search').focus(function () { $('.ys-header__nav').addClass('display-none') $('.ys-header__right').addClass('open') }, function() { if (!$('.ys-header__search-card').hasClass('show')) { $('.ys-header__nav').removeClass('display-none') $('.ys-header__right').removeClass('open') } }) // 搜索框聚焦事件 $('.ys-header__search input').focus(function () { openSearchCard() }) $('.ys-header__search-card .icon-close').click(function () { closeSearchCard() }) $(document).on('click', function (e) { if (!$('.ys-header__search-card')[0].contains(e.target) && !$('.ys-header__search input').is(':focus')) { closeSearchCard() } }) // 搜索 $('.ys-header__search input').keyup(function (e) { if (e.keyCode === 13) { search() } }) $('.ys-header__search .search-btn').click(function () { search() }) // 清空历史记录 $('.ys-header__search-card .search-history span').click(function () { localStorage.removeItem('searchHistory') setHistory() }) // 展开搜索卡片 function openSearchCard() { $('.ys-header__nav').addClass('display-none') $('.ys-header__search-card').addClass('show') $('.ys-header__cover').removeClass('display-none') $('.ys-header__right').addClass('open') } // 收起搜索卡片 function closeSearchCard() { $('.ys-header__nav').removeClass('display-none') $('.ys-header__search-card').removeClass('show') $('.ys-header__cover').addClass('display-none') $('.ys-header__right').removeClass('open') } // 搜索事件 function search() { let val = $('.ys-header__search input').val() if (!val) return $message.warning('请输入搜索关键词') let history = JSON.parse(localStorage.getItem('searchHistory')) || [] history.unshift(val) history = [...new Set(history)] localStorage.setItem('searchHistory', JSON.stringify(history.slice(0, 10))) window.open('/search_result.html?keyword=' + val) closeSearchCard() setHistory() } // 设置搜索历史 function setHistory() { let history = JSON.parse(localStorage.getItem('searchHistory')) || [] let html = '' history.forEach(e => { html += `${e}` }) $('.ys-header__search-card .search-history .search-words').html(html) } setHistory() }) // 客服 $('.ys-header-shortcut__server span').click(function(){ window.dispatchEvent( new CustomEvent('openService') ) }) /** 点击文章列表项 **/ // 如果需要点击item任意区域都能跳转 /* $(".info-list").on("click", ".info-item", function (e) { if (e.target !== e.currentTarget) return const articleId = $(this).data("id"); if (!articleId) return; window.open("/information/" + articleId, "_blank"); }); */ /** 搜索栏相关 **/ let searchLoading = false function search() { const keyword = $('#infoSearch').val() if (!keyword) { $message.warning('请输入搜索内容') return } let query = parsePath(location.href).query || {} if (query.cid) { location.href = location.origin + location.pathname + '?cid=' + query.cid + '&keyword=' + keyword } else { location.href = location.origin + location.pathname + '?keyword=' + keyword } } $('#infoSearchBtn').click(search) // input 回车 $('#infoSearch').keydown(function (e) { if (e.which !== 13) return search() }) /** 页码相关 **/ const defaultPageSize = 8 // 设置页码默认值,推荐在模板引擎控制option的selected function initPageSize(pageSize, className = '.c-page-selector') { if (!pageSize || isNaN(Number(pageSize))) { pageSize = defaultPageSize } $(`${className} option[value=${pageSize}]`).attr('selected', true) } initPageSize($('.c-page-selector').data('default')) // 页面切换每页个数 $('.c-page-selector').change(function () { let sizes = $(this).val() // 默认值 if (!sizes || isNaN(Number(sizes))) { sizes = defaultPageSize } console.log('sizes', sizes) let query = parsePath(location.href).query || {} if (query.cid) { location.href = location.origin + location.pathname + '?cid=' + query.cid + '&page=1&pageSize=' + sizes } else { location.href = location.origin + location.pathname + '?page=1&pageSize=' + sizes } }) function initPagination() { const total = +$('.c-pagination-nav').data('total') || 0 const page = +$('.c-pagination-nav').data('page') || 1 const pageSize = +$('.c-page-selector').data('default') || defaultPageSize const pageNum = Math.floor(total / pageSize) + 1 if (pageNum > 5) { let query = parsePath(location.href).query || {} if (page <= 3) { $('.c-page-item').each(function () { let p = +$(this).data('page') if (p && p > page + 1 && p !== pageNum) { $(this).css('display', 'none') } if (p === pageNum) { let more = `