saunter.js 12.8 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
/**
 * '逛'js
 * @author: yue.liu@yoho.cn
 * @date;2015/3/31
 */
var $ = require('yoho.jquery'),
    IScroll = require('iscroll/iscroll-probe'),
    lazyLoad = require('yoho.lazyload'),
    ellipsis = require('mlellipsis');

require('./wxshare')();

/**
 * 初始设置获取文章的点击数目(更新客户端缓存的数据)
 */
function updateClickCount() {
    var id = $('#article-id').val();

    $.ajax({
        type: 'GET',
        url: '/guang/info/getpraise',
        data: {
            id: id
        }
    }).then(function(data) {
        if (data.code === 200) {
            $('.page-view').html(data.data.views_num);
        }
    });
}
updateClickCount();

/**
 * 初始化Android页面
 */
exports.initAndroid = function() {
    $(function() {
        var $loginTip = $('#login-tip'),
            winH,
            winW,
            tipH,
            tipW;

        var isLogin = $('#is-login').val();

        //类型3文章相关变量
        var isInit = true,
            hadLazy = {},
            $atContainer,
            $thumbContainer,
            $prodList,
            thumbWidth,
            arrPos;

        //头部作者信息样式计算(在描述信息过长时换行显示,去除intro的padding-top)
        var $linkC = $('#link-container'),
            $avatar = $linkC.find('.avatar'),
            $name = $linkC.find('.name'),
            $intro = $linkC.find('.intro');

        isLogin = isLogin ? isLogin : 'N';

        if ($avatar.outerWidth(true) + $name.outerWidth(true) + $intro.outerWidth(true) > $(window).width()) {
            $intro.css('padding-top', 0);
        }

        //样式初始化
        $('#wrapper').removeClass('no-android');

        //相关文章截取文字
        ellipsis.init();
        $('.post-list span, .one-good .reco-name').each(function() {
            this.mlellipsis(2);
        });
        $('.brand-name').each(function() {
            this.mlellipsis(1);
        });

        //图片懒加载
        lazyLoad();

        //提示信息位置
        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();
            }
        });

        //类型三文章
        $atContainer = $('.article-type-three');
        if ($atContainer.length === 0) {
            return;
        }
        $thumbContainer = $atContainer.find('.thumb-container');
        $prodList = $atContainer.find('.prod-list');

        thumbWidth = $thumbContainer.find('li').width();
        arrPos = function($cur) {
            var left = $cur.offset().left,
                bgPos = -winW + left + (thumbWidth / 2) + 'px';

            $thumbContainer.css({
                backgroundPosition: bgPos + ' bottom'
            });
        };

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

            $curItem = $(this);

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

            arrPos($curItem);

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

            //图片懒加载(防止多次点击反复触发造成页面闪烁现象)
            if (!hadLazy[index]) {
                hadLazy[index] = true;
                lazyLoad($prodList.find('.prod:eq(' + index + ') img.lazy'));
            }

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

        //默认选中第一个
        $thumbContainer.find('.thumb:first-child').click();
    });
};

/**
 * 初始化非Android页面
 */
exports.initOther = function() {
    var hasAt = true, //是否包含article-type-three
        $atContainer,
        $thumbContainer,
        $fixedThumbContainer = $(''), //初始化为jq对象
        $prodList,
        winH,
        myScroll;

    $(function() {
        var $loginTip = $('#login-tip'),
            winW,
            tipH,
            tipW;

        var isLogin = $('#is-login').val();

        //article-type-three相关变量
        var isInit = true,
            scrollToEl = document.querySelector('#wrapper .article-type-three'),
            $thumbs,
            arrPos;

        //头部作者信息样式计算(在描述信息过长时换行显示,去除intro的padding-top)
        var $linkC = $('#link-container'),
            $avatar = $linkC.find('.avatar'),
            $name = $linkC.find('.name'),
            $intro = $linkC.find('.intro');

        //thumb的宽度,防止fixed-thumb未加载的问题
        var thumbWidth;

        isLogin = isLogin ? isLogin : 'N';
        if ($avatar.outerWidth(true) + $name.outerWidth(true) + $intro.outerWidth(true) > $(window).width()) {
            $intro.css('padding-top', 0);
        }

        //图片懒加载
        lazyLoad();

        //相关文章截取文字
        ellipsis.init();
        $('.post-list .title, .one-good .reco-name').each(function() {
            this.mlellipsis(2);
        });
        $('.brand-name').each(function() {
            this.mlellipsis(1);
        });

        //提示信息位置
        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();
            }
        });

        //article-three效果
        $atContainer = $('.article-type-three');
        if ($atContainer.length > 0) {
            hasAt = true;
        } else {
            hasAt = false;
        }

        //初始化
        $thumbContainer = $atContainer.find('.thumb-container');
        $prodList = $atContainer.find('.prod-list');

        $fixedThumbContainer = $('#wrapper')
            .after($thumbContainer.clone().addClass('fixed-thumb-container fixed-bottom hide'))
            .next('.thumb-container');
        $thumbs = $('.thumb');

        //手动替换出fixedThumbContainer中的图片Lazyload
        $fixedThumbContainer.find('li').each(function() {
            var $img = $(this).children('img.lazy');

            $img.attr('src', $img.attr('data-original'));
        });

        /**
         * 计算小箭头位置函数
         * @param $cur 当前点击thumb
         */
        thumbWidth = $thumbContainer.find('li').width();
        arrPos = function($cur) {
            var left = $cur.offset().left,
                bgPos = -winW + left + (thumbWidth / 2) + 'px';

            $thumbContainer.css({
                backgroundPosition: bgPos + ' bottom'
            });
            $fixedThumbContainer.css({
                backgroundPosition: bgPos + ' bottom'
            });
        };

        /**
         * 分类的点击事件句柄
         */
        function thumbClickEvt(e) {
            var $that = $(e.currentTarget),
                index = $that.index(),
                $other;

            if ($that.closest('.fixed-thumb-container').length > 0) {
                $other = $thumbContainer.find('.thumb:eq(' + index + ')');
            } else {
                $other = $fixedThumbContainer.find('.thumb:eq(' + index + ')');
            }

            //set status of that & other synchronous
            $thumbs.removeClass('focus');

            $that.addClass('focus');
            $other.addClass('focus');

            arrPos($that);

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

            if (!isInit) {
                if (myScroll) {
                    myScroll.scrollToElement(scrollToEl, 400);
                    myScroll.refresh();
                }
            } else {
                isInit = false;
            }
        }
        $thumbs.click(thumbClickEvt);

        //默认选中第一个
        $thumbContainer.find('.thumb:first-child').click();
    });

    /**
     * init iscroll
     */
    window.onload = function() {
        var tContainerH, //thumber-container高度
            containerH, //article-type-three高度
            containerTop; //article-type-three offset top

        myScroll = new IScroll('#wrapper', {
            probeType: 3,
            mouseWheel: true,
            click: true
        });

        document.addEventListener('touchmove', function (e) {
            e.preventDefault();
        }, false);

        if (!hasAt) {
            myScroll.on('scroll', function() {
                $('#scroller').trigger('scroll');
            });
            return;
        }

        tContainerH = $thumbContainer.outerHeight();
        containerH = $atContainer.height();
        containerTop = $atContainer.offset().top;

        /**
         * scroll前重置container状态
         */
        function resetStatus() {
            $fixedThumbContainer.removeClass('fixed-top fixed-bottom absolute hide').css('top', '');
        }

        myScroll.on('scroll', function() {
            var sTop = -this.y;

            resetStatus();

            if (sTop <= containerTop - winH + tContainerH) {
                $fixedThumbContainer
                    .addClass('fixed-bottom');
            } else if (sTop <= containerTop) {
                $fixedThumbContainer
                    .addClass('hide');
            } else if (sTop <= containerTop + containerH - tContainerH) {
                $fixedThumbContainer
                    .addClass('fixed-top');
            } else if (sTop <= containerTop + containerH) {
                $fixedThumbContainer
                    .addClass('absolute')
                    .css({
                        top: containerTop + containerH - tContainerH - sTop
                    });
            } else if (sTop > containerTop + containerH) {
                $fixedThumbContainer
                    .addClass('hide');
            }

            $('#scroller').trigger('scroll');
        });

        //手动触发scroll, 初始化fixedThumb的位置和显示
        myScroll.scrollTo(0, 0, 0.1);
    };
};