article-type-three.js 3.8 KB
/**
 * 类型3文章(Note: article-type-three在一个页面中只能出现一次(业务注意点),代码实现按照只有一个类型3实现)
 * @author: xuqi(qi.xu@yoho.cn)
 * @date;2015/3/30
 */
var $ = require('jquery');
require('lazyload'); 

/**
 * 初始化$事件并默认选中列表第一项
 */
exports.init = function() {
    $(function() {
        var container = $('.article-type-three'),
            thumbContainer = container.find('.thumb-container'),
            prodList = container.find('.prod-list'),
            wH = $(window).height(),
            isInit = true,
            tContainerH,
            containerH,
            containerTop;

        //点击分类,显示分类下推荐商品列表
        thumbContainer.delegate('.thumb', 'click', function() {
            var curItem,
                index;
            curItem = $(this);

            index = curItem.index();
            thumbContainer.find('.thumb').removeClass('focus')
            curItem.addClass('focus');

            prodList.find('.prod')
                .addClass('hide')
                .eq(index)
                .removeClass('hide');

            //图片懒加载
            $("img.lazy").lazyload({
                placeholder     : "data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw=="
            });

            //scroll to top
            if (!isInit) {
                $('body').animate({
                    scrollTop: container.offset().top
                }, 400);
            } else {
                isInit = false;
            }
        });

        //默认选中第一个
        thumbContainer.find('.thumb:first-child').click();
        
        tContainerH = thumbContainer.outerHeight(); //thumber-container高度
        containerH = container.height(); //article-type-three高度
        containerTop = container.offset().top; //article-type-three offset top

        //scroll时控制列表的位置
        $(document).scroll(function() {
            var sTop = $(this).scrollTop();

            //Tip: removeClass只用移除相邻状态即可
            if (sTop <= containerTop - wH + tContainerH) {
                thumbContainer
                    .addClass('fixed-bottom')
                    .removeClass('static');
                prodList.css({
                    'margin-top': tContainerH
                });
            } else if (sTop <= containerTop) {
                thumbContainer
                    .addClass('static')
                    .removeClass('fixed-bottom fixed-top')
                    .removeAttr('style');
                prodList.removeAttr('style')
            } else if (sTop <= containerTop + containerH - tContainerH) {
                thumbContainer
                    .addClass('fixed-top')
                    .removeClass('static absolute')
                    .removeAttr('style');
                prodList.css({
                    'margin-top': tContainerH
                });
            } else if (sTop <= containerTop + containerH) {
                thumbContainer
                    .addClass('absolute')
                    .removeClass('static fixed-top')
                    .css({
                        top: containerTop + containerH - tContainerH
                    });
                prodList.css({
                    'margin-top': tContainerH
                });
            } else if (sTop > containerTop + containerH) {
                thumbContainer
                    .addClass('static')
                    .removeClass('absolute')
                    .removeAttr('style');
                prodList.removeAttr('style');
            }
        });

        //初始执行scroll从而初始化分类信息的位置(非0时默认会执行scroll)
        if ($(document).scrollTop() === 0) {
            $(document).scroll();
        }
    });
};