home.page.js 15.1 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
/**
 *  首页
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/10/12
 */
//加载css
require("layout/_loading.css")
require("layout/_utils.css")

require("common/_filter.css")
require("common/_good.css")
require("common/_loading.css")
require("common/_suspend-home.css")

require("channel/_banner-bottom.css")
require("channel/_banner-center.css")
require("channel/_banner-top.css")
require("channel/_brand.css")
require("channel/_cate.css")
require("channel/_creative-life.css")
require("channel/_divide-image.css")
require("channel/_fine-brands.css")
require("channel/_floor-header.css")
require("channel/_footer-tab.css")
require("channel/_fresh-only.css")
require("channel/_goods-category.css")
require("channel/_home-header.css")
require("channel/_home.css")
require("channel/_hot-brands.css")
require("channel/_hot-category.css")
require("channel/_hot-single.css")
require("channel/_icons-enter.css")
require("channel/_index.css")
require("channel/_left-right.css")
require("channel/_maybe-like.css")
require("channel/_new-first.css")
require("channel/_new-user-floor.css")
require("channel/_notice.css")
require("channel/_plus-star.css")
require("channel/_sale-floor.css")
require("channel/_search-input.css")
require("channel/_search.css")
require("channel/_seckill.css")
require("channel/_shop-recommand.css")
require("channel/_side-nav.css")
require("channel/_six-lines-floor.css")
require("channel/_three-picture.css")
require("channel/_thumb-row.css")
require("channel/_trend-topics.css")
require("channel/_trend-tops.css")
require("channel/_trendsetter-collocation.css")
require("channel/_two-column-goods.css")
require("channel/_vip-only.css")


var $ = require('yoho-jquery'),
    Swiper = require('yoho-swiper'),
    alloytouch = require('alloytouch'),
    lazyLoad = require('yoho-jquery-lazyload'),
    fastclick = require('yoho-fastclick'),
    noticeScroll = require('../plugin/notice-scroll'),
    tip = require('../plugin/tip');

var $mobileWrap = $('.mobile-wrap'),
    $overlay = $('.overlay'),
    $sideNav = $('.side-nav'),
    $subNav = $('.sub-nav'),
    $logotrans = $('.home-header .logo'),
    $resourceContent = $('.resource-content'),
    start = 0,
    swiperClass,
    isen = true;

var channelContentHbs = require('channel/content.hbs');

var C_ID;

require('../common');
fastclick.attach(document.body);

lazyLoad($('img.lazy'));
noticeScroll('.notice', $('.notice').data('time') * 1000);

$('.nav-btn').on('touchstart', function() {
    $sideNav.css('pointer-events', 'none');
    $mobileWrap.addClass('menu-open');
    $overlay.show().css('opacity', 0.3);
    $sideNav.addClass('on');

    setTimeout(function() {
        $sideNav.css('pointer-events', 'auto');
    }, 400);

    return false;
});

function hideSideBar() {
    if ($mobileWrap.hasClass('menu-open')) {
        $mobileWrap.removeClass('menu-open');
        $overlay.hide();
        $('.sub-nav').removeClass('show');
        $sideNav.removeClass('on');
    }
}

$overlay.on('touchstart', function() {
    hideSideBar();
    return false;
});

$sideNav.on('touchmove scroll', function() {
    return false;
});

// 点击一级导航,弹出二级导航
$sideNav.on('touchend', 'li', function(e) {
    if ($(this).find('.sub-nav').size() > 0) {
        $('.sub-nav').removeClass('show');
        $(this).find('.sub-nav').addClass('show');
        $(this).find('.highlight').removeClass('highlight');
    }

    if (e.target.pathname === location.pathname) {
        hideSideBar();
        return false;
    }
});

// 返回一级导航,收起二级导航
$subNav.each(function() {
    $(this).find('li').eq(0).on('click', function() {
        $('.sub-nav').removeClass('show');
        return false;
    });
}).on('touchstart', function(e) {
    if (e.currentTarget !== e.target) {
        $subNav.find('li').removeClass('current');
    }
});

// 侧边栏点击背景色变化
function highlight($elem) {
    $elem.find('li').on('touchstart', function() {
        $elem.find('.highlight').removeClass('highlight');
        $(this).addClass('highlight');
    }).on('touchend touchcancel', function() {
        $(this).removeClass('highlight');
    });
}
highlight($sideNav);
highlight($subNav);


// 头部banner轮播
if ($('.banner-alloy').find('img').size() > 1) {
    new alloytouch({
        touch:".banner-alloy",//反馈触摸的dom
        vertical: false,//不必需,默认是true代表监听竖直方向touch
        target: $('.alloy-wrapper')[0], //运动的对象
        property: "translateY",  //被运动的属性
        change:function(){  }, //不必需,属性改变的回调。alloytouch.css版本不支持该事件
        touchStart:function(value){  },
        touchMove:function(value){  },
        touchEnd:function(evt, v, index){
            var step_v = index * this.step * -1;
            var dx = v - step_v;

            console.log(v)
            if (Math.abs(dx) < 30) {
                this.go(step_v);

            }
            else if (dx > 0) {
                this.go(step_v + this.step);

            } else {

                this.go(step_v - this.step);
            }

            return false;
        },
        animationEnd:function(v, page){  
            var i = 0,
                len = items.length;
            for (; i < len; i++) {
                if (i === page) {
                    items[i].classList.add("active");
                } else {
                    items[i].classList.remove("active");
                }
            }
        } //运动结束

    })
    // new Swiper('.banner-swiper', {
    //     lazyLoading: true,
    //     lazyLoadingInPrevNext: true,
    //     loop: true,
    //     autoplay: 3000,
    //     autoplayDisableOnInteraction: false,
    //     paginationClickable: true,
    //     slideElement: 'li',
    //     pagination: '.banner-top .pagination-inner'
    // });
}

// 店铺推荐人数和收藏初始查询
var checkShop = function() {
    var shopIds = [];

    $('.recom-shop').each(function() {
        shopIds.push($(this).attr('shopId'));
    });
    shopIds.length &&
    $.ajax({
        type: 'POST',
        url: '/channel/shopRecom',
        data: {
            shopIds: shopIds.join(','),
        },
    }).then(function(result) {
        if (result.code === 200) {
            for (var i = 0, elem;
                (elem = result.data[i]) != null; i++) {
                var a = $('.recom-shop[shopId = ' + elem.id + ']');

                a.find('.faved-num').text(elem.collectionNum + '人已收藏');
                if (elem.favorite) {
                    a.find('.fav-no').hide();
                    a.find('.fav-yes').show();
                }
            }
        }
    });
};

// sale倒计时
var saleTime = function(elem, offsetTime) {
    var hour = parseInt(offsetTime / (60 * 60), 10),
        minute = parseInt(offsetTime % (60 * 60) / 60, 10),
        second = offsetTime % 60;

    if (offsetTime <= -1) { // 结束倒计时刷新状态
        $(elem).find('.limit').hide();
    }

    if (offsetTime >= -1) {
        $(elem).find('.hour').text(hour < 0 ? '00' : (hour < 10 ? ('0' + hour) : hour));
        $(elem).find('.minute').text(minute < 0 ? '00' : (minute < 10 ? ('0' + minute) : minute));
        $(elem).find('.second').text(second < 0 ? '00' : (second < 10 ? ('0' + second) : second));

        if (offsetTime <= -1) { // 结束倒计时刷新状态
            $(elem).find('.limit').hide();
        } else {
            setTimeout(function() {
                saleTime(elem, --offsetTime);
            }, 1000);
        }
    }
};

function ajaxResource() {

    $('.banner-center-swiper').each(function() {
        if ($(this).find('li').length > 1) {
            new Swiper(this, {
                preloadImages: false,
                lazyLoading: true,
                lazyLoadingInPrevNext: true,
                lazyLoadingOnTransitionStart: true,
                loop: true,
                autoplay: 3000,
                autoplayDisableOnInteraction: false,
                paginationClickable: true,
                slideElement: 'li',
                pagination: '.banner-center .pagination-inner',
            });
        }
    });

    // 热门品牌滑动
    if ($('.brands-swiper').find('li').length > 1) {
        new Swiper('.brands-swiper', {
            grabCursor: true,
            slidesPerView: 'auto',
            wrapperClass: 'brands-list',
            slideElement: 'li'
        });
    }

    // 推荐搭配滑动
    if ($('.recommend-swiper').find('li').length > 1) {
        new Swiper('.recommend-swiper', {
            grabCursor: true,
            slidesPerView: 'auto',
            wrapperClass: 'recommend-list',
            slideElement: 'li'
        });
    }

    // 潮品话题轮播
    if ($('.trend-topic-swiper').find('li').size() > 1) {
        new Swiper('.trend-topic-swiper', {
            loop: true,
            autoplay: 3000,
            autoplayDisableOnInteraction: false,
            paginationClickable: true,
            slideElement: 'li',
            pagination: '.trend-topic-content .pagination-inner'
        });
    }

    // 新人专享轮播
    if ($('.fresh-list-swiper').find('li').size() > 1) {
        new Swiper('.fresh-list-swiper', {
            lazyLoading: true,
            lazyLoadingInPrevNext: true,
            grabCursor: true,
            slidesPerView: 'auto',
            slideElement: 'li',
            watchSlidesVisibility: true
        });
    }

    // 潮流上装/经典裤装等轮播
    $('.category-swiper').each(function(i) {
        swiperClass = 'category-swiper' + i;
        $(this).addClass(swiperClass);
        if ($('.' + swiperClass).find('.swiper-slide').size() > 1) {
            new Swiper('.' + swiperClass, {
                preloadImages: false,
                lazyLoading: true,
                lazyLoadingInPrevNext: true,
                lazyLoadingOnTransitionStart: true,
                loop: true,
                autoplay: 3000,
                autoplayDisableOnInteraction: false,
                paginationClickable: true,
                slideElement: 'li',
                pagination: '.' + swiperClass + ' .pagination-inner'
            });
        }
    });

    // 店铺推荐滑动效果
    if ($('.shop-recom-swiper-container').find('li').length > 1) {
        new Swiper('.shop-recom-swiper-container', {
            preloadImages: false,
            lazyLoading: true,
            lazyLoadingInPrevNext: true,
            lazyLoadingOnTransitionStart: true,
            centeredSlides: true,
            slidesPerView: 'auto',
            paginationClickable: true,
            slideActiveClass: 'recom-active',
        });
    }

    // 店铺收藏 || 取消收藏
    var $collect = $('.fav-container');

    $collect.on('touchstart', function() {
        var opt = $(this).hasClass('fav-yes') ? 'cancel' : 'ok';
        var self = $(this);
        var num = parseInt(self.parent().find('.faved-num').html());

        $.ajax({
            method: 'get',
            url: location.protocol + '//m.yohobuy.com/product/opt/favoriteBrand',
            data: {
                id: self.parent().attr('shopId'),
                opt: opt,
                type: 'shop',
            },
            xhrFields: {
                withCredentials: true
            },
            success: function(data) {
                var url = '';

                if (data.code === 200) {
                    if (self.hasClass('fav-yes')) {
                        self.hide();
                        self.prev().show();
                        num--;
                        self.parent().find('.faved-num').html(num + '人已收藏');
                        tip.show('取消收藏成功!');
                    } else {
                        self.hide();
                        self.next().show();
                        num++;
                        self.parent().find('.faved-num').html(num + '人已收藏');
                        tip.show('收藏成功!');
                    }
                }

                if (data.code === 400) {
                    url = data.data;
                    location.href = url;
                }

            },
            error: function() {
                tip.show('网络断开连接了~');
            }
        });
    });

    // vip专属等级查询
    $.ajax({
        method: 'POST',
        url: location.protocol + '//m.yohobuy.com/channel/userVip',
        data: {
            channel: 1,
        },
        success: function(data) {
            var vip = $('.vip-only');

            if (data.code === 200 && Number(data.current_vip_level) !== 0) {
                vip.show();
                vip.find('.vip' + data.current_vip_level).show();
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });

    checkShop();

    var limitFloor = $('.sale-floor-time');

    limitFloor.each(function() {
        var endTime = $(this).attr('data-time'),

        // ~~两次取反位运算就是取整
            limit = ~~((endTime * 1000 - Date.now()) / 1000);

        saleTime(this, limit);
    });
}

// 异步获取首屏其余楼层
var getResourceContent = function() {
    $.ajax({
        type: 'GET',
        url: '/channel/getResourceContent',
        data: {
            gender: $mobileWrap.data('channel')
        },
    }).then(function(result) {
        var resourceTemplate = channelContentHbs({content: result});

        $resourceContent.append(resourceTemplate);
        lazyLoad($resourceContent.find('img.lazy:not([src])'));

        ajaxResource();
    });
};

setTimeout(function() {
    getResourceContent();
}, 50);

// logo动画
function tsAnimate() {
    start = start + 10;
    $logotrans.css({
        transform: 'rotateX(' + start + 'deg)',
        '-webkit-transform': 'rotateX(' + start + 'deg)',
        '-moz-transform': 'rotateX(' + start + 'deg)'
    });
    if (start / 90 % 2 === 1) {
        if (isen) {
            $logotrans.addClass('animate');
            isen = false;
        } else {
            $logotrans.removeClass('animate');
            isen = true;
        }
    }
    if (start / 90 % 2 === 0 && start % 360 !== 0) {
        window.setTimeout(tsAnimate, 3000);
    } else {
        if (start % 360 === 0) {
            window.setTimeout(tsAnimate, 60 * 1000);
        } else {
            window.requestAnimationFrame(tsAnimate);
        }
    }
}

setTimeout(tsAnimate, 3000);

$('.home-header .iconfont').on('touchstart', function() {
    $(this).addClass('highlight');
}).on('touchend touchcancel', function() {
    $(this).removeClass('highlight');
});

// 底部留出tab 的高度
window.reMarginFooter('.footer-tab');

// 原有项目 _Channel 写错位置,这里清理掉
window.setCookie('_Channel', $mobileWrap.data('channel'), {
    expires: -1,
    domain: '.m.yohobuy.com'
});

// 记录当前 _Channel
window.setCookie('_Channel', $mobileWrap.data('channel'), {
    expires: 30,
    domain: '.yohobuy.com'
});

require('./maybe-like')();

C_ID = window._ChannelVary[window.cookie('_Channel')];

$('.search-btn').click(function() {
    if (window._yas && window._yas.sendCustomInfo) {
        window._yas.sendCustomInfo({
            op: 'YB_HOME_SEARCH_C',
            param: JSON.stringify({
                C_ID: C_ID,
            })
        }, true);
    }
});