Authored by hongweigao

+brands.page.js

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