mUtils.js 3.0 KB
Newer Older
M
updata  
maguohua 已提交
1 2 3 4 5 6
/**
 * 存储localStorage
 */
const setStore = (name, content) => {
	if (!name) return;
	if (typeof content !== 'string') {
M
updata  
maguohua 已提交
7
		content = JSON.stringify(content);
M
updata  
maguohua 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
	}
	window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
const getStore = name => {
	if (!name) return;
	return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
const removeStore = name => {
	if (!name) return;
	window.localStorage.removeItem(name);
}

M
updata  
maguohua 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/**
 * 获取style样式
 */
const getStyle =  (obj,attr) => { 
    if(obj.currentStyle){ 
        return obj.currentStyle[attr]; 
    } 
    else{ 
        return document.defaultView.getComputedStyle(obj,null)[attr]; 
    } 
} 

/**
 * 页面到达底部,加载更多
 */
const loadMore =  (el, callback) => {
	let windowHeight = window.screen.height;
	let height;
	let setTop;
	let paddingBottom;
	let marginBottom;
    let requestFram;
    let oldScrollTop;

	el.addEventListener('touchstart',() => {
        height = el.offsetHeight;
        setTop = el.offsetTop;
        paddingBottom = parseInt(getStyle(el,'paddingBottom'));
        marginBottom = parseInt(getStyle(el,'marginBottom'));
    },false)

    el.addEventListener('touchmove',() => {
       loadMore();
    },false)

    el.addEventListener('touchend',() => {
       	oldScrollTop = document.body.scrollTop;
       	moveEnd();
    },false)
    
    const moveEnd = () => {
        requestFram = requestAnimationFrame(() => {
            if (document.body.scrollTop != oldScrollTop) {
                oldScrollTop = document.body.scrollTop;
                moveEnd();
            }else{
            	cancelAnimationFrame(requestFram);
            	//为了防止鼠标抬起时已经渲染好数据从而导致重获取数据,应该重新获取dom高度
            	height = el.offsetHeight;
                loadMore();
            }
        })
    }

    const loadMore = () => {
        if (document.body.scrollTop + windowHeight >= height + setTop + paddingBottom + marginBottom) {
            callback();
        }
    }
}

/**
 * 显示返回顶部按钮
 */
const showBack =  callback => {
    let requestFram;
    let oldScrollTop;

    document.addEventListener('touchstart',() => {
       showBackFun();
    },false)

    document.addEventListener('touchmove',() => {
       showBackFun();
    },false)

    document.addEventListener('touchend',() => {
        oldScrollTop = document.body.scrollTop;
        moveEnd();
    },false)
    
    const moveEnd = () => {
        requestFram = requestAnimationFrame(() => {
            if (document.body.scrollTop != oldScrollTop) {
                oldScrollTop = document.body.scrollTop;
                showBackFun();
                moveEnd();
            }else{
                cancelAnimationFrame(requestFram);
            }
        })
    }

    const showBackFun = () => {
        if (document.body.scrollTop > 500) {
            callback(true);
        }else{
            callback(false);
        }
    }
}
M
updata  
maguohua 已提交
129 130


M
updata  
maguohua 已提交
131
export {setStore, getStore, removeStore, getStyle, loadMore, showBack}