// mixin
export const uniSystemInfoMixin = {
data() {
return {
// page-meta上设置的根标签字体大小
mixinRootFontSize: 50,
};
},
mounted() {
// 设置根字体大小
this.onSetFontSize();
},
onPageScroll({ scrollTop }) {
const mpHeaderHeight = this.$store.state.wxHeader.mpHeaderHeight || 44;
const pageScrollTop = this.$store.getters.['wxHeader/pageScrollTop'] || 44;
const parsedScrollTop = scrollTop > mpHeaderHeight ? mpHeaderHeight : scrollTop;
// 如果滑动值大于 mpHeaderHeight,就不再更新 data
if (parsedScrollTop === mpHeaderHeight && pageScrollTop === mpHeaderHeight) {
return;
}
this.$store.commit('wxHeader/setPageScrollTop', parsedScrollTop);
},
beforeDestroy() {
if (this.mpType === 'page') {
this.$store.commit('wxHeader/setPageScrollTop', 0);
}
},
methods: {
getMpHeaderBg() {
const pageScrollTop = this.getMpPageScrollTop();
const mpHeaderHeight = this.$store.state.wxHeader.mpHeaderHeight || 44;
return `rgba(255, 255, 255, ${Math.min(1, pageScrollTop / mpHeaderHeight)})`;
},
getMpPageScrollTop() {
const curPageName = this.getCurPageName();
const pageScrollTopMap = this.$store.state.wxHeader.pageScrollTopMap || {};
return pageScrollTopMap[curPageName] || 0;
},
getCurPageName() {
const pages = getCurrentPages();
return pages[pages.length - 1].route;
},
onSetFontSize() {
// 宽度 375 时(iphone6),rootFontSize为50,则一份为 375/50=7.5
const screenNumber = 7.5;
const that = this ;
if (that.mpType === 'page') {
// 窗体改变大小触发事件
uni.onWindowResize((res) => {
if (res.size.windowWidth) {
that.mixinRootFontSize = parseFloat(res.size.windowWidth) / screenNumber;
}
});
// 打开获取屏幕大小
uni.getSystemInfo({
success(res) {
const fontsize = res.screenWidth / screenNumber;
that.mixinRootFontSize = fontsize;
const mpHeaderHeight = res.statusBarHeight + 44;
that.$store.commit('wxHeader/setMpHeaderHeight', mpHeaderHeight);
},
});
}
},
},
};
|