saunter.js 2.86 KB
/**
 * '逛'js
 * @author: yue.liu@yoho.cn
 * @date;2015/3/31
 */

var $ = require('jquery'),
    ellipsis = require('mlellipsis');

require('lazyload');
require('./saunter/article-type-three');

/**
 * 初始化页面加载时的文字截取和图片懒加载功能
 */
exports.init = function() {
    $(function() {
        var $loginTip = $('#login-tip'),
    		winH,
    		winW,
    		tipH,
    		tipW;
            
        var isLogin = $('#is-login').val();
        isLogin = isLogin ? isLogin : 'N';
        
        //头部作者信息样式计算(在描述信息过长时换行显示, 去除intro的padding-top) 
        var $linkC = $('#link-container'),
            $avatar = $linkC.find('.avatar'),
            $name = $linkC.find('.name'),
            $intro = $linkC.find('.intro');
        if ($avatar.outerWidth(true) + $name.outerWidth(true) + $intro.outerWidth(true) > $(window).width()) {
            $intro.css('padding-top', 0);
        }
        
        //相关文章截取文字
        ellipsis.init();
        $('.post-list').find('span').each(function() {
            $(this).mlellipsis(2);
        });
        
        //图片懒加载
        $('img.lazy').lazyload();
        $('.thumb-container img.lazy').lazyload({
            event: 'load'
        });
        
        //提示信息位置
    	winH = $(window).height();
    	winW = $(window).width();
    	
    	tipH = $loginTip.height();
    	tipW = $loginTip.width();
    	
    	$loginTip.css({
    		top: (winH - tipH) / 2,
    		left: (winW - tipW) / 2
    	});
            
        //绑定商品信息的收藏和取消收藏事件(相关推荐和搭配模块)
        $('.good-list, .prod-list').delegate('.good-islike', 'touchstart', function(e) {
            var $cur,
                $good,
                id;
            if (isLogin === 'N') {
                $cur = $(e.currentTarget);
                $good = $cur.closest('.good-info');
                id = $good.data('id');
                
                $.ajax({
                    type: 'GET',
                    url: '/favorite/product',
                    data: {
                        st: 1,
                        product_skn: id
                    }
                }).then(function(data) {
                    if (data.code === 200) {
                        $cur.toggleClass('good-like');
                    } else if (data.code === 400) {
                        //提示登录信息
                        $('#login-tip').fadeIn(500, function() {
                            setTimeout(function() {
                               $('#login-tip').fadeOut(500);
                            }, 1000);
                        });
                    }
                });
            }
        }).delegate('.good-islike', 'click', function(e) {
            if (isLogin === 'Y') {
                e.preventDefault();
            }
        });
    });
};