dom-autoscroller
Advanced tools
Comparing version 1.2.4 to 1.3.0
167
index.js
@@ -1,2 +0,2 @@ | ||
var pointer = require('pointer-point'); | ||
var createPointCB = require('create-point-cb'); | ||
@@ -8,6 +8,4 @@ /* | ||
function AutoScrollerFactory(element, options){ | ||
return new AutoScroller(element, options); | ||
} | ||
function AutoScroller(elements, options){ | ||
@@ -21,4 +19,7 @@ var self = this, pixels = 2; | ||
this.point = pointer(elements); | ||
var point = {}, pointCB = createPointCB(point), down = false; | ||
window.addEventListener('mousemove', pointCB, false); | ||
window.addEventListener('touchmove', pointCB, false); | ||
if(!isNaN(options.pixels)){ | ||
@@ -37,8 +38,26 @@ pixels = options.pixels; | ||
this.destroy = function() { | ||
this.point.destroy(); | ||
window.removeEventListener('mousemove', pointCB, false); | ||
window.removeEventListener('touchmove', pointCB, false); | ||
window.removeEventListener('mousedown', onDown, false); | ||
window.removeEventListener('touchstart', onDown, false); | ||
window.removeEventListener('mouseup', onUp, false); | ||
window.removeEventListener('touchend', onUp, false); | ||
}; | ||
var hasWindow = null, temp = []; | ||
for(var i=0; i<elements.length; i++){ | ||
if(elements[i] === window){ | ||
hasWindow = window; | ||
break; | ||
}else{ | ||
temp.push(elements[i]) | ||
} | ||
} | ||
elements = temp; | ||
temp = null; | ||
Object.defineProperties(this, { | ||
down: { | ||
get: function(){ return self.point.down; } | ||
get: function(){ return down; } | ||
}, | ||
@@ -54,8 +73,85 @@ interval: { | ||
this.point.on('move', function(el, rect){ | ||
window.addEventListener('mousedown', onDown, false); | ||
window.addEventListener('touchstart', onDown, false); | ||
window.addEventListener('mouseup', onUp, false); | ||
window.addEventListener('touchend', onUp, false); | ||
function onDown(){ | ||
down = true; | ||
} | ||
function onUp(){ | ||
down = false; | ||
} | ||
var n = 0, current; | ||
window.addEventListener('mousemove', onMove, false); | ||
window.addEventListener('touchmove', onMove, false); | ||
function onMove(event){ | ||
if(!self.autoScroll()) return; | ||
if(!event.target) return; | ||
var target = event.target, last; | ||
if(!current || !inside(point, current)){ | ||
if(!current && target){ | ||
current = null; | ||
while(target = target.parentNode){ | ||
for(var i=0; i<elements.length; i++){ | ||
if(elements[i] === target && inside(point, elements[i])){ | ||
current = elements[i]; | ||
break; | ||
} | ||
} | ||
} | ||
}else{ | ||
last = current; | ||
current = null; | ||
for(var i=0; i<elements.length; i++){ | ||
if(elements[i] !== last && inside(point, elements[i])){ | ||
current = elements[i]; | ||
} | ||
} | ||
} | ||
} | ||
if(hasWindow){ | ||
autoScroll(hasWindow); | ||
} | ||
if(!current) return; | ||
autoScroll(current); | ||
} | ||
function autoScroll(el){ | ||
var rect = getRect(el); | ||
if(point.y < rect.top + self.margin){ | ||
autoScrollV(el, -1, rect); | ||
}else if(point.y > rect.bottom - self.margin){ | ||
autoScrollV(el, 1, rect); | ||
} | ||
if(point.x < rect.left + self.margin){ | ||
autoScrollH(el, -1, rect); | ||
}else if(point.x > rect.right - self.margin){ | ||
autoScrollH(el, 1, rect); | ||
} | ||
} | ||
/*this.point.on('move', function(el, rect){ | ||
console.log('moveing ', ++n, el) | ||
if(!el) return; | ||
if(!self.autoScroll()) return; | ||
if(!self.scrollWhenOutside && this.outside(el)) return; | ||
//if(!self.scrollWhenOutside && self.point.outside(el)) return; | ||
if(self.point.outside(el)) return; | ||
rect = pointer.getRect(el); | ||
if(self.point.y < rect.top + self.margin){ | ||
@@ -72,11 +168,13 @@ autoScrollV(el, -1, rect); | ||
} | ||
}); | ||
});*/ | ||
function autoScrollV(el, amount, rect){ | ||
//if(!self.down) return; | ||
if(!self.autoScroll()) return; | ||
if(!self.scrollWhenOutside && self.point.outside(el)) return; | ||
if(!self.scrollWhenOutside && !inside(point, el, rect)) return; | ||
if(el === window){ | ||
window.scrollTo(el.pageXOffset, el.pageYOffset + amount); | ||
}else{ | ||
el.scrollTop = el.scrollTop + amount; | ||
@@ -86,5 +184,5 @@ } | ||
setTimeout(function(){ | ||
if(self.point.y < rect.top + self.margin){ | ||
if(point.y < rect.top + self.margin){ | ||
autoScrollV(el, amount, rect); | ||
}else if(self.point.y > rect.bottom - self.margin){ | ||
}else if(point.y > rect.bottom - self.margin){ | ||
autoScrollV(el, amount, rect); | ||
@@ -96,5 +194,6 @@ } | ||
function autoScrollH(el, amount, rect){ | ||
//if(!self.down) return; | ||
if(!self.autoScroll()) return; | ||
if(!self.scrollWhenOutside && self.point.outside(el)) return; | ||
if(!self.scrollWhenOutside && !inside(point, el, rect)) return; | ||
if(el === window){ | ||
@@ -107,5 +206,5 @@ window.scrollTo(el.pageXOffset + amount, el.pageYOffset); | ||
setTimeout(function(){ | ||
if(self.point.x < rect.left + self.margin){ | ||
if(point.x < rect.left + self.margin){ | ||
autoScrollH(el, amount, rect); | ||
}else if(self.point.x > rect.right - self.margin){ | ||
}else if(point.x > rect.right - self.margin){ | ||
autoScrollH(el, amount, rect); | ||
@@ -118,2 +217,32 @@ } | ||
module.exports = AutoScrollerFactory; | ||
module.exports = function AutoScrollerFactory(element, options){ | ||
return new AutoScroller(element, options); | ||
}; | ||
function getRect(el){ | ||
if(el === window){ | ||
return { | ||
top: 0, | ||
left: 0, | ||
right: window.innerWidth, | ||
bottom: window.innerHeight, | ||
width: window.innerWidth, | ||
height: window.innerHeight | ||
}; | ||
}else{ | ||
try{ | ||
return el.getBoundingClientRect(); | ||
}catch(e){ | ||
console.log(el) | ||
throw new TypeError("Can't call getBoundingClientRect on "+el); | ||
} | ||
} | ||
} | ||
function inside(point, el, rect){ | ||
rect = rect || getRect(el); | ||
return (point.y > rect.top && point.y < rect.bottom && | ||
point.x > rect.left && point.x < rect.right); | ||
} |
{ | ||
"name": "dom-autoscroller", | ||
"version": "1.2.4", | ||
"version": "1.3.0", | ||
"description": "Auto scroll dom elements", | ||
@@ -10,4 +10,14 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "browserify index.js --standalone autoScroller > dist/dom-autoscroller.js", | ||
"minify": "browserify index.js -d -p [minifyify --no-map] --standalone autoScroller > dist/dom-autoscroller.min.js", | ||
"gadd": "git add dist", | ||
"lint": "jshint .", | ||
"validate": "npm ls" | ||
}, | ||
"pre-commit": [ | ||
"build", | ||
"minify", | ||
"gadd" | ||
], | ||
"keywords": [ | ||
@@ -20,6 +30,9 @@ "scrolling", | ||
"dependencies": { | ||
"pointer-point": "^2.1.12" | ||
"create-point-cb": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"dragula": "^3.5.4" | ||
"browserify": "^13.0.1", | ||
"dragula": "^3.5.4", | ||
"minifyify": "^7.3.3", | ||
"precommit-hook": "^3.0.0" | ||
}, | ||
@@ -29,3 +42,8 @@ "repository": { | ||
"url": "https://github.com/hollowdoor/dom_autoscroller.git" | ||
} | ||
}, | ||
"files": [ | ||
"dist", | ||
"LICENSE", | ||
"index.js" | ||
] | ||
} |
@@ -9,2 +9,9 @@ dom-autoscroller | ||
Then use browserify (maybe webpack) to build your script. | ||
Or Download one of these files from the Github repo: | ||
- *dist/dom-autoscroller.js* | ||
- *dist/dom-autoscroller.min.js* | ||
Usage | ||
@@ -11,0 +18,0 @@ ----- |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28552
6
460
187
4
3
4
1
+ Addedcreate-point-cb@^1.0.0
+ Addedcreate-point-cb@1.2.0(transitive)
+ Addedtype-func@1.0.3(transitive)
- Removedpointer-point@^2.1.12
- Removedmore-events@1.3.2(transitive)
- Removedpointer-point@2.4.0(transitive)