index.js 1.02 KB
Component({
    properties: {
        defaultIndex: { // 默认选中位置
            type: Number,
            value: 0
        },
        navs: { // [{name: '', id: ''}]
            type: Array,
            value: []
        }
    },
    data: {
        scrollLeft: 0
    },
    ready: function() {
        setTimeout(() => {
            if (this.data.navs.length > 3) {
                let systemInfo = wx.getSystemInfoSync();

                wx.createSelectorQuery().in(this).select('.my-scroll-nav .nav-item.active').boundingClientRect().exec(res => { // eslint-disable-line
                    let offset = res[0] || {};

                    this.setData({
                        scrollLeft: offset.left - systemInfo.windowWidth / 2 + offset.width / 2
                    });
                });
            }
        }, 200);
    },
    methods: {
        tapNav: function(e) { // 滚动底部 加载更多 回调
            let dataset = e.currentTarget.dataset;

            this.triggerEvent('tapNav', {dataset});
        }
    }
});