Authored by 阿达

js fix bug

... ... @@ -48,106 +48,126 @@ var $clearHistory = $('#clear-history');
var chHammer;
loading.showLoadingMask();
function changeBackground() {
var $brandList = $('.brand-list').find('p');
lazyLoad($('img.lazy'));
$('.yoho-header').css({
'z-index': 2,
position: 'fixed',
top: 0
});
$brandList.on('touchstart', function() {
$brandList.css('background', '#fff');
$(this).css('background', '#eee');
}).on('touchend touchcancel', function() {
$(this).css('background', '#fff');
});
}
changeBackground();
$homebuttom.css('top', '51px');
$homebuttom.css('position', 'fixed');
/**
* 增加单条记录
*/
function addHistory(brandName) {
$.ajax({
type: 'GET',
url: '/brands/addBrandSearch',
data: 'brandName=' + brandName,
success: function(data) {
},
error: function() {
}
});
}
$navBtn.on('touchstart', function() {
if ($homebuttom.hasClass('hide')) {
$genderNav.css('top', '54px');
$newSearch.css('margin-top', '0px');
$searchPage.css('margin-top', '0px');
$('.banner-top').css('margin-top', '0px');
//比较大小
function sequence(a, b) {
if (a.time > b.time) {
return 1;
} else if (a.time < b.time) {
return -1;
} else {
$genderNav.css('top', '104px');
$newSearch.css('margin-top', '50px');
$searchPage.css('margin-top', '50px');
$('.banner-top').css('margin-top', '50px');
return 0;
}
});
}
function searchInput() {
if ($keyword.val().length) {
$icon.css('color', '#444');
// $(this).closest('.search-box').css('width', '11.25rem');
$searchAction.show().find('.clear-text').show();
} else {
$icon.css('color', '#b2b2b2');
// $(this).closest('.search-box').css('width', '12.5rem');
$searchAction.find('.clear-text').hide();
(function() {
if ($('.banner-top').length > 0) {
$('.hot-brands').css('padding-top', '0');
}
searchResult();
}
$('.hide-when-loading').show();
loading.hideLoadingMask();
//绑定提交前的存local操作
function bindWirteLocal($brandList, list) {
$brandList.on('click', 'p , li', function(e) {
var brandName, brandId, brandDomain, url;
var a = $(this).find('a');
var myDate = new Date();
var query = a.attr('brandName') + ranToken2 + myDate,
historys;
//banner滑动
swiper = new Swiper('.swiper-container', {
lazyLoading: true,
loop: true,
autoplay: 3000,
pagination: '.swiper-pagination .pagination-inner'
});
$keyword = $('#keyword');
//热门品牌滑动
hotBrandsSwiper = new Swiper('.brands-swiper', {
grabCursor: true,
slidesPerView: 'auto',
wrapperClass: 'brands-list',
slideElement: 'li'
});
})();
brandName = a.attr('brandName');
$fixTitleBar = $('<div class="title-bar fixed-title-bar"><h2></h2></div>');
$fixTitleBar.css({
position: 'fixed',
top: brandSwipe
}).hide();
minBrandListTop = brandSwipe + $('.hot-brands').outerHeight() + $('.banner-top').outerHeight();
$brandList.last().append($fixTitleBar);
brandId = a.attr('brandId');
if ($('.brand-index-page').length > 0) {
function scrollHandler() {
var scrTop = $(window).scrollTop();
brandDomain = a.attr('brandDomain');
if ($brandList.eq(0).offset().top < minBrandListTop) {
$fixTitleBar.hide();
}
url = a.attr('href');
$brandList.each(function() {
var offTop = $(this).offset().top - brandSwipe;
e.preventDefault();
if (scrTop >= offTop) {
$fixTitleBar.css({
display: 'block'
}).find('h2').html($(this).find('.title-bar').text());
if ($netHistory.length > 0) {
addHistory(brandName);
if (list) {
location.href = url;
return;
}
});
}
$keyword.val(brandName);
searchInput();
return;
}
//srcoll to load more
$(window).scroll(function() {
window.requestAnimationFrame(scrollHandler);
});
}
if (localStorage) {
historys = localStorage.getItem('historys-brand');
historys = historys ? historys : '';
if (historys.indexOf(ranToken + query + ranToken) > -1) {
function changeBackground() {
var $brandList = $('.brand-list').find('p');
$brandList.on('touchstart', function() {
$brandList.css('background', '#fff');
$(this).css('background', '#eee');
}).on('touchend touchcancel', function() {
$(this).css('background', '#fff');
$keyword.val(brandName);
searchInput();
return;
}
if (historys === '') {
query = ranToken + query;
}
historys += query + ranToken;
localStorage.setItem('historys-brand', historys);
}
if (list) {
location.href = url;
return;
}
$keyword.val(brandName);
searchInput();
});
}
changeBackground();
function searchResult() {
var keyword = ($keyword.val() + '').toLowerCase();
... ... @@ -210,6 +230,96 @@ function searchResult() {
bindWirteLocal($('.brand-list'), true);
}
loading.showLoadingMask();
lazyLoad($('img.lazy'));
$('.yoho-header').css({
'z-index': 2,
position: 'fixed',
top: 0
});
$homebuttom.css('top', '51px');
$homebuttom.css('position', 'fixed');
$navBtn.on('touchstart', function() {
if ($homebuttom.hasClass('hide')) {
$genderNav.css('top', '54px');
$newSearch.css('margin-top', '0px');
$searchPage.css('margin-top', '0px');
$('.banner-top').css('margin-top', '0px');
} else {
$genderNav.css('top', '104px');
$newSearch.css('margin-top', '50px');
$searchPage.css('margin-top', '50px');
$('.banner-top').css('margin-top', '50px');
}
});
(function() {
if ($('.banner-top').length > 0) {
$('.hot-brands').css('padding-top', '0');
}
$('.hide-when-loading').show();
loading.hideLoadingMask();
//banner滑动
swiper = new Swiper('.swiper-container', {
lazyLoading: true,
loop: true,
autoplay: 3000,
pagination: '.swiper-pagination .pagination-inner'
});
//热门品牌滑动
hotBrandsSwiper = new Swiper('.brands-swiper', {
grabCursor: true,
slidesPerView: 'auto',
wrapperClass: 'brands-list',
slideElement: 'li'
});
})();
$fixTitleBar = $('<div class="title-bar fixed-title-bar"><h2></h2></div>');
$fixTitleBar.css({
position: 'fixed',
top: brandSwipe
}).hide();
minBrandListTop = brandSwipe + $('.hot-brands').outerHeight() + $('.banner-top').outerHeight();
$brandList.last().append($fixTitleBar);
function scrollHandler() {
var scrTop = $(window).scrollTop();
if ($brandList.eq(0).offset().top < minBrandListTop) {
$fixTitleBar.hide();
}
$brandList.each(function() {
var offTop = $(this).offset().top - brandSwipe;
if (scrTop >= offTop) {
$fixTitleBar.css({
display: 'block'
}).find('h2').html($(this).find('.title-bar').text());
}
});
}
if ($('.brand-index-page').length > 0) {
/**
* srcoll to load more
*/
$(window).scroll(function() {
window.requestAnimationFrame(scrollHandler);
});
}
if ($('.history-search').length > 0) {
bindWirteLocal($('.history-search'), false);
}
... ... @@ -246,22 +356,6 @@ if ($('.brand-search-page').length > 0) {
}
function searchInput() {
if ($keyword.val().length) {
$icon.css('color', '#444');
// $(this).closest('.search-box').css('width', '11.25rem');
$searchAction.show().find('.clear-text').show();
} else {
$icon.css('color', '#b2b2b2');
// $(this).closest('.search-box').css('width', '12.5rem');
$searchAction.find('.clear-text').hide();
}
searchResult();
}
if ($genderItem.length > 0) {
$genderItem.on('touchstart', function() {
var index = $(this).data('id') + 1;
... ... @@ -323,32 +417,6 @@ function removeHistory() {
});
}
/**
* 增加单条记录
*/
function addHistory(brandName) {
$.ajax({
type: 'GET',
url: '/brands/addBrandSearch',
data: 'brandName=' + brandName,
success: function(data) {
},
error: function() {
}
});
}
//比较大小
function sequence(a, b) {
if (a.time > b.time) {
return 1;
} else if (a.time < b.time) {
return -1;
} else {
return 0;
}
}
//初始化历史搜索的内容
if ($('.brand-search-page').length > 0) {
... ... @@ -375,7 +443,7 @@ if ($('.brand-search-page').length > 0) {
history,
historys, i,
localHistory,
brand,data,brandName,
brand,data,
time,netArr,dict,dictc,localArr,
combinArr,key,date,temp;
... ... @@ -390,7 +458,7 @@ if ($('.brand-search-page').length > 0) {
dict = {
brand: brand,
time: time
};
},dictc,dict;
netArr.push(dict);
});
... ... @@ -414,7 +482,7 @@ if ($('.brand-search-page').length > 0) {
dict = {
brand: brand,
time: time
}
};
localArr.push(dict);
... ... @@ -432,18 +500,22 @@ if ($('.brand-search-page').length > 0) {
temp = {};
if (localArr.length > 0) {
for (dictc in localArr) {
temp = localArr[dictc];
if (!date[temp.brand] || date[temp.brand] < temp.time) {
date[temp.brand] = temp.time;
if (localArr.hasOwnProperty(dictc)) {
temp = localArr[dictc];
if (!date[temp.brand] || date[temp.brand] < temp.time) {
date[temp.brand] = temp.time;
}
}
}
}
if (netArr.length > 0 ) {
if (netArr.length > 0) {
for (dict in netArr) {
temp = netArr[dict];
if (!date[temp.brand] || date[temp.brand] < temp.time) {
date[temp.brand] = temp.time;
if (netArr.hasOwnProperty(dict)) {
temp = netArr[dict];
if (!date[temp.brand] || date[temp.brand] < temp.time) {
date[temp.brand] = temp.time;
}
}
}
}
... ... @@ -454,16 +526,15 @@ if ($('.brand-search-page').length > 0) {
data = {
brand: key,
time: date[key]
}
};
combinArr.push(data);
}
}
combinArr.sort(sequence);
if (combinArr.length > 0) {
for (i = combinArr.length - 1 ; i >= (combinArr.length > 10) ? (combinArr.length - 10) : 0; i--) {
for (i = combinArr.length - 1; i >= (combinArr.length > 10) ? (combinArr.length - 10) : 0; i--) {
brand = combinArr[i].brand;
time = combinArr[i].time;
... ... @@ -486,70 +557,3 @@ if ($('.brand-search-page').length > 0) {
}());
}
//绑定提交前的存local操作
function bindWirteLocal($brandList,list) {
$brandList.on('click', 'p , li' , function(e) {
var brandName, brandId, brandDomain, url;
var a = $(this).find('a');
var myDate = new Date();
var query = a.attr('brandName') + ranToken2 + myDate,
historys;
$keyword = $('#keyword');
brandName = a.attr('brandName');
brandId = a.attr('brandId');
brandDomain = a.attr('brandDomain');
url = a.attr('href');
e.preventDefault();
if ($netHistory.length > 0) {
addHistory(brandName);
if (list) {
location.href = url;
return;
}
$keyword.val(brandName);
searchInput();
return;
}
if (localStorage) {
historys = localStorage.getItem('historys-brand');
historys = historys ? historys : '';
if (historys.indexOf(ranToken + query + ranToken) > -1) {
$keyword.val(brandName);
searchInput();
return;
}
if (historys === '') {
query = ranToken + query;
}
historys += query + ranToken;
localStorage.setItem('historys-brand', historys);
}
if (list) {
location.href = url;
return;
}
$keyword.val(brandName);
searchInput();
});
}
... ...