Blame view

public/js/guang/index/view.js 2.01 KB
郭成尧 authored
1 2 3 4
/*
 * @Author: Targaryen
 * @Date: 2017-04-26 17:15:50
 * @Last Modified by: Targaryen
郭成尧 authored
5
 * @Last Modified time: 2017-05-02 14:43:23
郭成尧 authored
6 7
 */
'use strict';
陈峰 authored
8
import {View} from 'js/yoho-mvc';
郭成尧 authored
9
郭成尧 authored
10
class ListView extends View {
郭成尧 authored
11
    constructor() {
郭成尧 authored
12
        super('#guangList');
郭成尧 authored
13
郭成尧 authored
14
        this.list = $('#info-list');
郭成尧 authored
15 16
        this.nav = this.$base.find('#guang-nav');
        this.navItem = this.nav.find('.guang-nav-item');
郭成尧 authored
17 18 19

        this.beforeScroll = document.body.scrollTop; // 滚动前位置记录
郭成尧 authored
20
        this.on('touchend touchcancel', 'li', this.tabClick.bind(this));
郭成尧 authored
21 22 23 24 25

        $(window).scroll(() => {
            window.requestAnimationFrame(this.scrollToLoadMore.bind(this));
        });
郭成尧 authored
26 27
    }
郭成尧 authored
28 29 30 31
    /**
     * Tab 切换
     * @param {*} e
     */
郭成尧 authored
32
    tabClick(e) {
郭成尧 authored
33 34
        this.beforeScroll = document.body.scrollTop; // 滚动前位置记录
郭成尧 authored
35 36
        let $this = $(e.currentTarget);
郭成尧 authored
37 38 39
        this.navItem.removeClass('focus');
        $this.addClass('focus');
郭成尧 authored
40
        this.emit('tabchange', $this.data('type'));
郭成尧 authored
41 42
    }
郭成尧 authored
43 44 45 46
    /**
     * 列表改变
     * @param {*} html
     */
郭成尧 authored
47 48
    listChange(html) {
        this.list.html(html);
郭成尧 authored
49
    }
郭成尧 authored
50 51 52 53 54 55 56 57 58 59 60 61 62

    /**
     * 列表追加
     * @param {*} html
     */
    listAppend(html) {
        this.list.append(html);
    }

    /**
     * 获取默认 Type
     */
    getType() {
郭成尧 authored
63
        return this.navItem.find('li.focus').data('type');
郭成尧 authored
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
    }

    /**
     * 滚动加载更多
     */
    scrollHandler() {
        if ($(window).scrollTop() > this.list.height() * 0.5) {
            let type = this.getType();

            this.emit('loadmore', type);
        }
    }

    /**
     * 滚动控制
     */
    scrollToLoadMore() {
        setTimeout(() => {
            let afterScroll = document.body.scrollTop;

            if (afterScroll - this.beforeScroll > 0) {
                window.requestAnimationFrame(this.scrollHandler.bind(this));
                this.beforeScroll = afterScroll;
            } else {
                return false;
            }
        }, 5);
    }
郭成尧 authored
92 93 94
}

module.exports = {
郭成尧 authored
95
    ListView
郭成尧 authored
96
};