/** * 商品搜索结果页 */ $(function() { var _object = function () { this.init(); }; var pageNo = 1; var flagAJAX = 1; /* 详情页 链接 */ var detailpageId = ''; var textStr = ''; _object.prototype = { /* 初始化方法 */ init: function () { var self = this; // 绑定搜索框点击事件 $('body').on('click', '.shopSearch svg', function () { pageNo = 1; // 清空元页面 并插入搜索结果页的 容器 $('.wqdCenterView').html('
'); // 获取 当前 siteid var userId = $(this).parents('.search-box').data('siteid'); detailpageId = $(this).parents('.search-box').data('pageid') // 获取搜索框内容的值 并将所有数据拼接成字符串 var json = pageNo +'?text=' + textStr + '&siteId='+ userId; // 插入数据 self.strHtml(json); // 为页面你绑定滚动事件,当页面滚动到底部时 继续加载 $(document).scroll(function (e) { var H = $(document).height(); var oH = $(document).scrollTop(); var Hr = document.documentElement.clientHeight; var footH = $('.wqdFooterView') && $('.wqdFooterView').height(); if(H - footH <= oH + Hr ) { json = pageNo +'?text=' + textStr + '&siteId=' + userId; flagAJAX && self.strHtml(json); } }) }) // 搜索框获取光标后按下回车键搜索 $('body').off('focus.search').on('focus.search', '.search-box .search-input', function (e) { this.onkeyup = function(event) { var event = event || window.event; event.keyCode == '13' && $('body .shopSearch svg').click(); } }); $('body').off('input.search propertychange.search').on('input.search propertychange.search', '.search-input', function (e) { textStr = $(this).val(); }); }, strHtml: function (json) { var self = this; flagAJAX = 0; $.ajax({ type: "GET", url: SAAS_SHOP+'/shop/api/goods/search/'+json, // 格式 pageNo?text=""&siteId="" timeout: 20000, //超时时间设置,单位毫秒 data: '', async: true, contentType: 'application/json, charset=utf-8 ', dataType: "jsonp", success: function (data, status) { if(status == 'success') { var str = ''; if(data.data.length) { $.each(data.data, function (i, val) { str += self.getHtml(val); }) pageNo ++ ; flagAJAX = 1; $('.wqdCenterView .search-list-zone').append(str); // history.pushState('abc', '', '#'+new Date().getTime()); // window.location.reload(); $(document).scrollTop(0); } else { $('.wqdCenterView .search-list-zone img.no-list').length && $('.wqdCenterView .search-list-zone img.no-list').remove(); $('.wqdCenterView .search-list-zone').append('
')
$(document).scrollTop(0);
}
if(!data.hasNext && pageNo >1) {
flagAJAX = 0;
$(document).off('scroll');
$('.wqdCenterView .search-list-zone .no-more').length && $('.wqdCenterView .search-list-zone .no-more').remove();
$('.wqdCenterView .search-list-zone').append('没有更多了
'); } } } }) }, /** * */ getHtml: function (data) { return [ '
' ].join(''); }, }; new _object(); });