You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/public/react-mobile7/build/handleScroll.js

83 lines
2.4 KiB

6 years ago
// 阻止拖动看到“powered by...”
// 查看el是否存在selector对应的父dom
function closest(el, selector) {
var matchesFn;
// find vendor prefix
['matches','webkitMatchesSelector','mozMatchesSelector','msMatchesSelector','oMatchesSelector'].some(function(fn) {
if (typeof document.body[fn] == 'function') {
matchesFn = fn;
return true;
}
return false;
})
var parent;
// traverse parents
while (el) {
parent = el.parentElement;
if (parent && parent[matchesFn](selector)) {
return parent;
}
el = parent;
}
return null;
}
var _startX = 0,
_startY = 0,
_currentSelector;
function _touchStart(e) {
if (e.target.className === 'active-state' || closest(e.target, '#eruda')) {
return;
}
if (!closest(e.target, _currentSelector)) {
// console.log('非需要拖拽的区域')
e.preventDefault()
return;
}
try {
var touch = e.touches[0],
x = Number(touch.pageX),
y = Number(touch.pageY);
_startX = x;
_startY = y;
} catch (e) {
alert(e);
}
}
// document.addEventListener('touchstart', _touchStart);
function initScrollDom(selector) {
return;
_currentSelector = selector
var ele = document.querySelector(selector);
// ele.ontouchmove
var _touchMove = function (e) {
var point = e.touches[0],
eleTop = ele.scrollTop,
eleScrollHeight = ele.scrollHeight,
eleOffsetHeight = ele.offsetHeight,
eleTouchBottom = eleScrollHeight - eleOffsetHeight;
// console.log('----------------------------')
// console.log(eleTop)
// console.log(eleTouchBottom)
// console.log(point.clientY)
// console.log(_startY)
// console.log('----------------------------')
if (eleTop === 0) {
if (point.clientY > _startY) {
e.preventDefault();
}
} else if (eleTop === eleTouchBottom) {
if (point.clientY < _startY) {
e.preventDefault()
}
}
};
ele.removeEventListener('touchmove', _touchMove);
ele.addEventListener('touchmove', _touchMove);
}