brands.page.js
4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* 首页
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2015/11/23
*/
var $ = require('yoho-jquery'),
lazyLoad = require('yoho-jquery-lazyload');
var $tabs = $('.brands-tabs');
var $list = $('.brands-list');
var $gory = $('.brands-category');
var $news = $('.news-txt ul');
var $brand = $list.find('li>a');
var $category = $gory.find('a');
var $tab = $tabs.find('li>a');
var $arr = $tabs.find('.hoverarr');
var $thisTab;
var categoryHeight = $category.height();
var categoryTop = $category.offset() ? $category.offset().top : 0;
var newsHeight = $news.height();
var newsTop = $news.offset() ? $news.offset().top : 0;
var timeout, _id;
// 用于临时存储数据
var tempdata = {};
var templete = require('hbs/brands/brand-info.hbs');
require('../common');
require('yoho-jquery-dotdotdot');
$('.brand-desc').dotdotdot({
wrap: 'letter'
});
lazyLoad($('img.lazy'));
// $('.slide-container').slider({
// orient: true
// });
$.easing.easeOutQuint = function(x, t, b, c, d) {
return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
};
function getQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
var r = window.location.search.substr(1).match(reg);
if (r !== null) {
return window.unescape(r[2]);
}
return null;
}
// 格式化资讯NEWS标题数量
if ($news.length) {
$news.find('li').each(function() {
var $dom = $(this);
var domHeight = $dom.offset().top - newsTop + $dom.height();
if (domHeight > newsHeight) {
$dom.hide();
}
});
}
// 头部图片TAB切换展示
$tab.eq(0).parent('li').find('.brands-content').css('z-index', '1');
_id = getQueryString('id') ? getQueryString('id') : 0;
// 设置对应TAB选中
$thisTab = $tab.eq(_id);
$thisTab.find('.g-mask').addClass('g-mask-on');
if (_id && $thisTab.length) {
$arr.css({
left: parseFloat($thisTab.offset().left) - parseFloat($tabs.offset().left)
});
}
$tab.hover(function() {
var $this = $(this);
clearTimeout(timeout);
timeout = setTimeout(function() {
var targetLeft = parseFloat($this.offset().left) - parseFloat($tabs.offset().left);
$arr.animate({
left: targetLeft
}, 200, 'easeOutQuint');
}, 50);
$tabs.find('.brands-content').removeAttr('style');
$this.parent('li').find('.brands-content').css('z-index', '1');
}, function() {
clearTimeout(timeout);
});
// 品牌类别滚动事件
$(window).scroll(function() {
if ($(this).scrollTop() >= categoryTop) {
$gory.addClass('category-fix');
} else {
$gory.removeClass('category-fix');
}
});
// 点击字母,页面滚动到相关区域
$category.click(function() {
var name = $(this).attr('href').split('#')[1];
var targetTop = $list.find('[name="' + name + '"]').offset().top - categoryHeight;
if (!$gory.hasClass('category-fix')) {
targetTop -= categoryHeight;
}
$('html,body').animate({
scrollTop: targetTop
}, 200);
return false;
});
// 浮层代码
function bindTemplete($select, data, tmp) {
var $this = $select;
var offset = {
width: $this.width(),
left: $this.offset().left,
right: parseFloat($(window).width()) - parseFloat($this.offset().left) - parseFloat($this.width())
};
var $parent = $this.parent('li');
var myTemplate;
$list.find('.brands-dialog').remove();
myTemplate = tmp;
$parent.append(myTemplate(data));
$parent.find('.right').dotdotdot({
wrap: 'letter'
});
if (offset.right - 350 < 0) {
$parent.find('.brands-layer')
.addClass('brands-layer-right').css('left', -330 - offset.width);
}
}
// 鼠标悬浮品牌,请求数据,并且展示
function bindHoverEvent() {
if (String($list.data('ishover')) === 'true') {
return;
}
$brand.unbind('mouseenter').unbind('mouseleave').hover(function() {
var $this = $(this);
var key = $this.attr('data-key');
var options;
// 全球购品牌不展示品牌简介
if (+key < 0) {
return;
}
options = {
url: '/brands/brandinfo',
type: 'get',
data: {
brandId: key
},
success: function(_data) {
if (_data.code === 200 && _data.brand) {
if (!tempdata.hasOwnProperty(_data.brand.key)) {
tempdata[_data.brand.key] = _data.brand;
}
bindTemplete($this, tempdata[_data.brand.key], templete);
}
}
};
clearTimeout(timeout);
timeout = setTimeout(function() {
if (!tempdata.hasOwnProperty(key)) {
$.ajax(options);
} else {
bindTemplete($this, tempdata[key], templete);
}
}, 200);
}, function() {
clearTimeout(timeout);
$list.find('.brands-dialog').remove();
});
}
bindHoverEvent();