mixin.js 2.0 KB
Newer Older
M
maguohua 已提交
1 2
import { getStyle } from '../../config/mUtils'
import { imgBaseUrl, localapi, proapi } from '../../config/env'
M
updata  
maguohua 已提交
3 4

export const loadMore = {
M
maguohua 已提交
5 6
	directives: {
		'load-more': {
M
updata  
maguohua 已提交
7 8 9 10 11 12
			bind: (el, binding) => {
				let windowHeight = window.screen.height;
				let height;
				let setTop;
				let paddingBottom;
				let marginBottom;
M
maguohua 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25
				let requestFram;
				let oldScrollTop;
				let scrollEl;
				let heightEl;
				let scrollType = el.attributes.type && el.attributes.type.value;
				let scrollReduce = 2;
				if (scrollType == 2) {
					scrollEl = el;
					heightEl = el.children[0];
				} else {
					scrollEl = document.body;
					heightEl = el;
				}
M
updata  
maguohua 已提交
26

M
maguohua 已提交
27 28 29 30 31 32 33 34 35
				el.addEventListener('touchstart', () => {
					height = heightEl.clientHeight;
					if (scrollType == 2) {
						height = height
					}
					setTop = el.offsetTop;
					paddingBottom = getStyle(el, 'paddingBottom');
					marginBottom = getStyle(el, 'marginBottom');
				}, false)
M
updata  
maguohua 已提交
36

M
maguohua 已提交
37 38 39
				el.addEventListener('touchmove', () => {
					loadMore();
				}, false)
M
updata  
maguohua 已提交
40

M
maguohua 已提交
41 42 43 44
				el.addEventListener('touchend', () => {
					oldScrollTop = scrollEl.scrollTop;
					moveEnd();
				}, false)
M
updata  
maguohua 已提交
45

M
maguohua 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
				const moveEnd = () => {
					requestFram = requestAnimationFrame(() => {
						if (scrollEl.scrollTop != oldScrollTop) {
							oldScrollTop = scrollEl.scrollTop;
							moveEnd()
						} else {
							cancelAnimationFrame(requestFram);
							height = heightEl.clientHeight;
							loadMore();
						}
					})
				}

				const loadMore = () => {
					if (scrollEl.scrollTop + windowHeight >= height + setTop + paddingBottom + marginBottom - scrollReduce) {
						binding.value();
					}
				}
M
updata  
maguohua 已提交
64 65 66
			}
		}
	}
M
maguohua 已提交
67 68 69 70 71
};

export const getImgPath = {
	methods: {
		//传递过来的图片地址需要处理后才能正常使用
M
maguohua 已提交
72
		getImgPath(path) {
M
maguohua 已提交
73
			let suffix;
M
maguohua 已提交
74
			if (!path) {
C
cangdu 已提交
75
				return '//elm.cangdu.org/img/default.jpg'
M
maguohua 已提交
76
			}
M
maguohua 已提交
77 78
			if (path.indexOf('jpeg') !== -1) {
				suffix = '.jpeg'
M
maguohua 已提交
79
			} else {
M
maguohua 已提交
80 81
				suffix = '.png'
			}
M
maguohua 已提交
82
			let url = '/' + path.substr(0, 1) + '/' + path.substr(1, 2) + '/' + path.substr(3) + suffix;
M
maguohua 已提交
83
			return 'https://fuss10.elemecdn.com' + url
M
maguohua 已提交
84 85 86
		},
	}

M
updata  
maguohua 已提交
87
}