Blame view

public/js/guang/star.page.js 9.9 KB
王水玲 authored
1 2 3 4 5 6 7 8
/**
 * 星潮教室首页
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/5/31
 */

var $ = require('yoho-jquery'),
    Swiper = require('yoho-swiper'),
ccbikai authored
9 10

    // inView = require('in-view'),
王水玲 authored
11 12
    loading = require('../plugin/loading'),
    PullRefresh = require('../plugin/pull-refresh');
王水玲 authored
13
ccbikai authored
14
var $window = $(window),
王水玲 authored
15 16 17
    stopLoading = false,
    $mainContent = $('.main-content'),
    $loadingTip = $('.loading-tip'),
王水玲 authored
18 19
    u = navigator.userAgent,
    isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
20
王水玲 authored
21
/*
王水玲 authored
22 23
    $swiperView = $('.swiper-view'),
    $viewImg = $('.view-img'),
24 25 26
    $body = $('body'),
    imgs = [],
    allImgs = {};
王水玲 authored
27
王水玲 authored
28 29 30
var mySwiper;
*/
梁志锋 authored
31 32
var avatarKey, bannerLen, res, avatarSwiper, getIndexHtml, bannerSwiper,
    $avatarWrap, starIScroll;
王水玲 authored
33
梁志锋 authored
34 35
var swiperNum = $('.avatar-wrap .swiper-slide').width() / 2,
    topBanner = $('.banner-top');
王水玲 authored
36
ccbikai authored
37 38 39 40 41 42
// var $gif = $('img[data-original*="gif"]');

if ($('.star-main').css('height') === '0px') {
    $('.star-main').css('height', $window.height() - $('.head-tab').height());
}
王水玲 authored
43 44 45
if ($window.width() === 375 && isiOS) {
    swiperNum = 32;
}
王水玲 authored
46
王水玲 authored
47
require('../common');
ccbikai authored
48
require('yoho-jquery-lazyload');
王水玲 authored
49
ccbikai authored
50 51
// require('yoho-jquery-inview');
王水玲 authored
52
$window.on('mousewheel', false);
ccbikai authored
53 54 55
$('#yoho-header, .head-tab').on('touchmove', function() {
    return false;
});
王水玲 authored
56
梁志锋 authored
57
ccbikai authored
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
// inView(document.querySelectorAll('img[data-original*="gif"]'))
//     .on('enter', function() {
//         console.log(1);
//     })
//     .on('exit', function() {
//         console.log(2);
//     });

// $('img[data-original*="gif"]').on('inview', function(event, isInView) {
//     if (isInView) {
//         $(this).css('display', 'block');
//     } else {
//         $(this).css('display', 'none');
//     }
// });
王水玲 authored
74
// 限制标题字数
ccbikai authored
75 76 77
// function txtLimit() {
//
// }
王水玲 authored
78
ccbikai authored
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
// // 设置默认头像
// function setAvatar($userAvatar) {
//     var myImage = new Image(),
//         avatar = '';
//
//     // 部分老用户没有头像,显示默认头像
//     avatar = $userAvatar.data('avatar');
//
//     // myImage.onload = function() {
//     //     $userAvatar.css('background-image', 'url(' + avatar + ')');
//     // };
//     //
//     // if (avatar) {
//     //     myImage.src = avatar;
//     // } else {
//     //     $userAvatar.addClass('default-avater');
//     // }
// }
王水玲 authored
97
王水玲 authored
98 99 100

// 明星头像点击居中显示或跳转
function bindAvatar(dom) {
王水玲 authored
101
    avatarKey = dom.index();
王水玲 authored
102 103

    if (!dom.hasClass('swiper-slide-active')) {
104
梁志锋 authored
105
        // if (avatarKey >= (2 * bannerLen + 1)) {
106 107
        //    avatarSwiperClone && avatarSwiperClone.slideTo(bannerLen + 1, 0);
        //    avatarSwiper.slideTo(bannerLen + 1, 0);
梁志锋 authored
108
        // } else if (avatarKey <= (bannerLen - 1)) {
109 110
        //    avatarSwiperClone && avatarSwiperClone.slideTo(2 * bannerLen - 1, 0);
        //    avatarSwiper.slideTo(2 * bannerLen - (bannerLen - avatarKey), 0);
梁志锋 authored
111 112
        // } else {
        //    avatarSwiperClone && avatarSwiperClone.slideTo(avatarKey, 200);
113
        // avatarSwiper.slideTo(avatarKey, 200);
梁志锋 authored
114
        // }
王水玲 authored
115 116 117 118 119

        return false;
    }
}
王水玲 authored
120
function setIndexAction() {
梁志锋 authored
121 122 123
//     $('.avatar').each(function(key, item) {
//         $(item).addClass('avatar-' + key);
//     });
124
王水玲 authored
125 126
    // 明星头像 swiper 初始化
    if (bannerLen > 1) {
ccbikai authored
127 128
        avatarSwiper = new Swiper($avatarWrap.find('.avatar-swiper')[0], {
            lazyLoading: true,
ccbikai authored
129 130 131
            lazyLoadingInPrevNextAmount: 3,

            // loop: true,
王水玲 authored
132
            initialSlide: 2,
王水玲 authored
133 134
            centeredSlides: true,
            slidesPerView: 'auto',
135
            loopedSlides: bannerLen,
王水玲 authored
136
            spaceBetween: 10,
王水玲 authored
137
            slidesOffsetBefore: -swiperNum,
王水玲 authored
138
            watchSlidesProgress: true,
王水玲 authored
139 140
            watchSlidesVisibility: true,
            onInit: function() {
ccbikai authored
141
                $avatarWrap.on('click', 'li', function() {
王水玲 authored
142 143 144 145
                    res = bindAvatar($(this));
                    return res;
                });
            }
146
        });
王水玲 authored
147
    }
王水玲 authored
148
王水玲 authored
149
    // 明星动态头像左右滑动
ccbikai authored
150 151 152 153 154 155 156 157 158
    // if ($('.article-avatar-swiper').find('li').length > 1) {
    //     new Swiper('.article-avatar-swiper', {
    //         initialSlide: 0,
    //         lazyLoading: true,
    //         lazyLoadingInPrevNext: true,
    //         loop: true,
    //         autoplay: 5000
    //     });
    // }
159
ccbikai authored
160 161
    // banner swiper 初始化
    if ($('.banner-swiper').find('li').length > 1) {
ccbikai authored
162
        bannerSwiper = new Swiper('.banner-swiper', {
ccbikai authored
163 164 165 166 167 168 169 170 171 172
            lazyLoading: true,
            lazyLoadingInPrevNext: true,
            loop: true,
            autoplay: 3000,
            autoplayDisableOnInteraction: false,
            paginationClickable: true,
            slideElement: 'li',
            pagination: '.banner-top .pagination-inner'
        });
    }
王水玲 authored
173
ccbikai authored
174 175 176 177 178 179 180
    // if ($('.star-info').find('li').length > 0) {
    //     $starArticle.each(function(key, item) {
    //         txtLimit($(item), '.article-title', 'p');
    //
    //         // articleImgAction($(item), key);
    //     });
    // }
王水玲 authored
181
ccbikai authored
182 183 184
    // $('.rank-avatar').each(function(key, item) {
    //     setAvatar($(item));
    // });
王水玲 authored
185 186 187 188 189 190
}

function initAction() {
    $avatarWrap = $('.avatar-wrap');
    bannerLen = $('.avatar-wrap').find('.swiper-slide').length;
梁志锋 authored
191 192 193
//    if ($avatarClone) {
//        $avatarClone.remove();
//    }
王水玲 authored
194 195

    $loadingTip.before($('.avatar-swiper').clone().addClass('avatar-clone').hide());
王水玲 authored
196
梁志锋 authored
197
    // $avatarClone = $('.avatar-clone');
王水玲 authored
198
ccbikai authored
199 200 201
    $('img.lazy').lazyload({
        effect: 'fadeIn'
    });
王水玲 authored
202
梁志锋 authored
203
    setIndexAction();
王水玲 authored
204
}
王水玲 authored
205
ccbikai authored
206 207
initAction();
王水玲 authored
208 209 210 211 212 213
// 首页数据请求
getIndexHtml = function() {
    loading.showLoadingMask();
    $.ajax({
        url: '/guang/star/getIndexHtml',
        dataType: 'html',
ccbikai authored
214
        timeout: 5000
陈轩 authored
215 216 217
    })
    .done(function(data) {
        if (data) {
梁志锋 authored
218 219 220 221
//            if (avatarSwiperClone) {
//                avatarSwiperClone.destroy(true, true);
//                avatarSwiperClone = null;
//            }
王水玲 authored
222
陈轩 authored
223 224 225
            $mainContent.html(data);

            initAction();
王水玲 authored
226
        }
陈轩 authored
227 228 229 230
    })
    .always(function() {
        stopLoading = false;
        loading.hideLoadingMask();
王水玲 authored
231 232 233
    });
};
梁志锋 authored
234 235 236 237 238 239 240
// $(window).ready(function() {
//    topBannerH = $('.banner-top').height();
//    console.log('init'+topBannerH);
// });


王水玲 authored
241 242 243 244 245 246 247 248 249 250 251
// 下拉刷新,上拉加载
starIScroll = new PullRefresh('.star-main', {
    pullDown: function() {
        if (!stopLoading) {
            stopLoading = true;
            getIndexHtml();
        }
    }
});

starIScroll.iScroll.on('scrollStart', function() {
梁志锋 authored
252 253 254 255 256 257
        // 下拉
        // if (this.directionY === -1) {
        //     $loadingTip.slideDown();
        // }
        // $gif.css('display', 'none');
    bannerSwiper && bannerSwiper.stopAutoplay();
王水玲 authored
258 259
});
zzzzzzz authored
260 261 262
starIScroll.iScroll.on('scroll', function() {
    $window.trigger('scroll');
ccbikai authored
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
    // var avatarOffsetT;
    //
    // if (this.directionY !== -1) {
    //     setTimeout(function() {
    //         starIScroll.iScroll.refresh();
    //     }, 0);
    // }
    //
    // avatarOffsetT = $avatarWrap[0].offsetTop;
    //
    // if (-this.y >= avatarOffsetT) {
    //     $avatarClone.show();
    //
    //     if (!avatarSwiperClone) {
    //         avatarSwiperClone = new Swiper($avatarClone[0], {
    //             lazyLoading: true,
    //             lazyLoadingInPrevNext: true,
    //
    //             // loop: true,
    //             initialSlide: $avatarWrap.find('.swiper-slide-active').index() % bannerLen,
    //             centeredSlides: true,
    //             slidesPerView: 'auto',
    //             loopedSlides: bannerLen,
    //             spaceBetween: 10,
    //             slidesOffsetBefore: -swiperNum,
    //             watchSlidesProgress: true,
    //             watchSlidesVisibility: true,
    //             onInit: function() {
    //                 $avatarClone.find('li').on('click', function() {
    //                     res = bindAvatar($(this));
    //                     return res;
    //                 });
    //             }
    //         });
    //
    //         avatarSwiperClone.params.control = avatarSwiper;
    //         avatarSwiper.params.control = avatarSwiperClone;
    //     }
    // } else {
    //     $avatarClone.hide();
    // }
zzzzzzz authored
304
});
ccbikai authored
305
王水玲 authored
306
starIScroll.iScroll.on('scrollEnd', function() {
梁志锋 authored
307 308 309 310 311
        // $loadingTip.slideUp();
        // $gif.css('display', 'block');
    if (-this.y <= topBanner.height()) {
        bannerSwiper && bannerSwiper.startAutoplay();
    }
王水玲 authored
312 313
});
梁志锋 authored
314 315

王水玲 authored
316
window.setCookie('guangStarUid', window.queryString.uid);
王水玲 authored
317
ccbikai authored
318 319 320 321
// $('img').on('load', function() {
//     starIScroll && starIScroll.iScroll.refresh();
// });
王水玲 authored
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
// 明星动态文章图片相关操作
/*
function articleImgAction(dom, key) {
    var $articeImgs = dom.find('.artice-imgs'),
        $li = $articeImgs.find('li'),
        imgLen = $articeImgs.find('img').length,
        addFlagDom = $li.eq(2);

    var i;

    imgs = [];

    $li.each(function() {
        imgs.push($(this).find('img').attr('src'));
    });

    allImgs[key] = imgs;

    $li.on('click', function() {
        for (i = 0; i < imgLen; i++) {
            $swiperView.find('ul').append('<li class="swiper-slide">' +
            '<img class="swiper-lazy" data-src="' + allImgs[key][i] + '">' +
            '<div class="swiper-lazy-preloader"></div></li>');
        }
        setTimeout(function() {
            mySwiper = new Swiper('.swiper-view', {
                lazyLoading: true,
                lazyLoadingInPrevNext: true,
                slideElement: 'li'
            });

            $('.swiper-view li').on('click', function(e) {
                if (e.target.nodeName === 'IMG') {
                    return;
                }
                $viewImg.hide();
                $body.css({
                    overflow: 'auto'
                });
                $viewImg.find('li').remove();
                mySwiper.destroy(false);
            });
        }, 100);

        $viewImg.show();

        $body.css({
            overflow: 'hidden'
        });
    });

    if (imgLen > 3 && addFlagDom.find('.img-size').length < 1) {
        addFlagDom.append('<div class="img-size"><i class="pic-icon"></i>' + imgLen + '</div>');
    }
}
*/
梁志锋 authored
378 379 380