Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dom-autoscroller

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dom-autoscroller - npm Package Compare versions

Comparing version 2.1.6 to 2.2.0

131

dist/bundle.es.js

@@ -1,21 +0,6 @@

import createPointCB from 'create-point-cb';
import { boolean } from 'type-func';
import { requestAnimationFrame, cancelAnimationFrame } from 'animation-frame-polyfill';
import { hasElement, removeElements, addElements } from 'dom-set';
import { getClientRect, createPointCB, pointInside } from 'dom-plane';
var requestFrame = function () {
if (requestAnimationFrame) {
return requestAnimationFrame;
} else {
return function (fn) {
return setTimeout(fn);
};
}
}();
var cancelFrame = function () {
if (cancelAnimationFrame) {
return cancelAnimationFrame;
} else {
return clearTimeout;
}
}();
function AutoScroller(elements) {

@@ -43,15 +28,3 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

if (typeof options.autoScroll === 'boolean') {
this.autoScroll = options.autoScroll ? function () {
return true;
} : function () {
return false;
};
} else if (typeof options.autoScroll === 'undefined') {
this.autoScroll = function () {
return false;
};
} else if (typeof options.autoScroll === 'function') {
this.autoScroll = options.autoScroll;
}
this.autoScroll = boolean(options.autoScroll);

@@ -70,30 +43,17 @@ this.destroy = function () {

function getElement(element) {
if (typeof element === 'string') {
return document.querySelector(element);
this.add = function () {
for (var _len = arguments.length, element = Array(_len), _key = 0; _key < _len; _key++) {
element[_key] = arguments[_key];
}
return element;
}
this.add = function (element) {
element = getElement(element);
for (var i = 0; i < elements.length; i++) {
if (elements[i] === element) return this;
}
elements.push(element);
addElements.apply(undefined, [elements].concat(element));
return this;
};
this.remove = function (element) {
element = getElement(element);
this.remove = function () {
for (var _len2 = arguments.length, element = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
element[_key2] = arguments[_key2];
}
for (var i = 0; i < elements.length; i++) {
if (element === elements[i]) {
elements.splice(i, 1);
return this;
}
}
return this;
return removeElements.apply(undefined, [elements].concat(element));
};

@@ -180,4 +140,4 @@

down = false;
cancelFrame(animationFrame);
cancelFrame(windowAnimationFrame);
cancelAnimationFrame(animationFrame);
cancelAnimationFrame(windowAnimationFrame);
}

@@ -198,13 +158,9 @@

for (var i = 0; i < elements.length; i++) {
if (elements[i] === target) {
return target;
}
if (hasElement(elements, target)) {
return target;
}
while (target = target.parentNode) {
for (var i = 0; i < elements.length; i++) {
if (elements[i] === target) {
return target;
}
if (hasElement(elements, target)) {
return target;
}

@@ -256,4 +212,4 @@ }

if (hasWindow) {
cancelFrame(windowAnimationFrame);
windowAnimationFrame = requestFrame(scrollWindow);
cancelAnimationFrame(windowAnimationFrame);
windowAnimationFrame = requestAnimationFrame(scrollWindow);
}

@@ -265,4 +221,4 @@

cancelFrame(animationFrame);
animationFrame = requestFrame(scrollTick);
cancelAnimationFrame(animationFrame);
animationFrame = requestAnimationFrame(scrollTick);
}

@@ -273,4 +229,4 @@

cancelFrame(windowAnimationFrame);
windowAnimationFrame = requestFrame(scrollWindow);
cancelAnimationFrame(windowAnimationFrame);
windowAnimationFrame = requestAnimationFrame(scrollWindow);
}

@@ -286,8 +242,8 @@

cancelFrame(animationFrame);
animationFrame = requestFrame(scrollTick);
cancelAnimationFrame(animationFrame);
animationFrame = requestAnimationFrame(scrollTick);
}
function autoScroll(el) {
var rect = getRect(el),
var rect = getClientRect(el),
scrollx = void 0,

@@ -344,5 +300,11 @@ scrolly = void 0;

}
function getRect(el) {
if (el === window) {
function inside(point, el, rect) {
if (!rect) {
return pointInside(point, el);
} else {
return point.y > rect.top && point.y < rect.bottom && point.x > rect.left && point.x < rect.right;
}
}
/*function getRect(el){
if(el === window){
return {

@@ -356,15 +318,18 @@ top: 0,

};
} else {
try {
}else{
try{
return el.getBoundingClientRect();
} catch (e) {
throw new TypeError("Can't call getBoundingClientRect on " + el);
}catch(e){
throw new TypeError("Can't call getBoundingClientRect on "+el);
}
}
}
function inside(point, el, rect) {
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;
}
return (point.y > rect.top && point.y < rect.bottom &&
point.x > rect.left && point.x < rect.right);
}*/

@@ -371,0 +336,0 @@ /*

'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var typeFunc = require('type-func');
var animationFramePolyfill = require('animation-frame-polyfill');
var domSet = require('dom-set');
var domPlane = require('dom-plane');
var createPointCB = _interopDefault(require('create-point-cb'));
var requestFrame = function () {
if (requestAnimationFrame) {
return requestAnimationFrame;
} else {
return function (fn) {
return setTimeout(fn);
};
}
}();
var cancelFrame = function () {
if (cancelAnimationFrame) {
return cancelAnimationFrame;
} else {
return clearTimeout;
}
}();
function AutoScroller(elements) {

@@ -37,3 +20,3 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var point = {},
pointCB = createPointCB(point),
pointCB = domPlane.createPointCB(point),
down = false;

@@ -48,15 +31,3 @@

if (typeof options.autoScroll === 'boolean') {
this.autoScroll = options.autoScroll ? function () {
return true;
} : function () {
return false;
};
} else if (typeof options.autoScroll === 'undefined') {
this.autoScroll = function () {
return false;
};
} else if (typeof options.autoScroll === 'function') {
this.autoScroll = options.autoScroll;
}
this.autoScroll = typeFunc.boolean(options.autoScroll);

@@ -75,30 +46,17 @@ this.destroy = function () {

function getElement(element) {
if (typeof element === 'string') {
return document.querySelector(element);
this.add = function () {
for (var _len = arguments.length, element = Array(_len), _key = 0; _key < _len; _key++) {
element[_key] = arguments[_key];
}
return element;
}
this.add = function (element) {
element = getElement(element);
for (var i = 0; i < elements.length; i++) {
if (elements[i] === element) return this;
}
elements.push(element);
domSet.addElements.apply(undefined, [elements].concat(element));
return this;
};
this.remove = function (element) {
element = getElement(element);
this.remove = function () {
for (var _len2 = arguments.length, element = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
element[_key2] = arguments[_key2];
}
for (var i = 0; i < elements.length; i++) {
if (element === elements[i]) {
elements.splice(i, 1);
return this;
}
}
return this;
return domSet.removeElements.apply(undefined, [elements].concat(element));
};

@@ -173,3 +131,3 @@

if (scrolling) {
requestAnimationFrame(function () {
animationFramePolyfill.requestAnimationFrame(function () {
return scrolling = false;

@@ -186,4 +144,4 @@ });

down = false;
cancelFrame(animationFrame);
cancelFrame(windowAnimationFrame);
animationFramePolyfill.cancelAnimationFrame(animationFrame);
animationFramePolyfill.cancelAnimationFrame(windowAnimationFrame);
}

@@ -204,13 +162,9 @@

for (var i = 0; i < elements.length; i++) {
if (elements[i] === target) {
return target;
}
if (domSet.hasElement(elements, target)) {
return target;
}
while (target = target.parentNode) {
for (var i = 0; i < elements.length; i++) {
if (elements[i] === target) {
return target;
}
if (domSet.hasElement(elements, target)) {
return target;
}

@@ -262,4 +216,4 @@ }

if (hasWindow) {
cancelFrame(windowAnimationFrame);
windowAnimationFrame = requestFrame(scrollWindow);
animationFramePolyfill.cancelAnimationFrame(windowAnimationFrame);
windowAnimationFrame = animationFramePolyfill.requestAnimationFrame(scrollWindow);
}

@@ -271,4 +225,4 @@

cancelFrame(animationFrame);
animationFrame = requestFrame(scrollTick);
animationFramePolyfill.cancelAnimationFrame(animationFrame);
animationFrame = animationFramePolyfill.requestAnimationFrame(scrollTick);
}

@@ -279,4 +233,4 @@

cancelFrame(windowAnimationFrame);
windowAnimationFrame = requestFrame(scrollWindow);
animationFramePolyfill.cancelAnimationFrame(windowAnimationFrame);
windowAnimationFrame = animationFramePolyfill.requestAnimationFrame(scrollWindow);
}

@@ -292,8 +246,8 @@

cancelFrame(animationFrame);
animationFrame = requestFrame(scrollTick);
animationFramePolyfill.cancelAnimationFrame(animationFrame);
animationFrame = animationFramePolyfill.requestAnimationFrame(scrollTick);
}
function autoScroll(el) {
var rect = getRect(el),
var rect = domPlane.getClientRect(el),
scrollx = void 0,

@@ -350,5 +304,11 @@ scrolly = void 0;

}
function getRect(el) {
if (el === window) {
function inside(point, el, rect) {
if (!rect) {
return domPlane.pointInside(point, el);
} else {
return point.y > rect.top && point.y < rect.bottom && point.x > rect.left && point.x < rect.right;
}
}
/*function getRect(el){
if(el === window){
return {

@@ -362,15 +322,18 @@ top: 0,

};
} else {
try {
}else{
try{
return el.getBoundingClientRect();
} catch (e) {
throw new TypeError("Can't call getBoundingClientRect on " + el);
}catch(e){
throw new TypeError("Can't call getBoundingClientRect on "+el);
}
}
}
function inside(point, el, rect) {
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;
}
return (point.y > rect.top && point.y < rect.bottom &&
point.x > rect.left && point.x < rect.right);
}*/

@@ -377,0 +340,0 @@ /*

var autoScroll = (function () {
'use strict';
'use strict';
var index = function createPointCB(object) {
function getDef(f, d) {
if (typeof f === 'undefined') {
return typeof d === 'undefined' ? f : d;
}
// A persistent object (as opposed to returned object) is used to save memory
// This is good to prevent layout thrashing, or for games, and such
return f;
}
function boolean(func, def) {
// NOTE
// This uses IE fixes which should be OK to remove some day. :)
// Some speed will be gained by removal of these.
func = getDef(func, def);
// pointCB should be saved in a variable on return
// This allows the usage of element.removeEventListener
if (typeof func === 'function') {
return function f() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return function pointCB(event) {
return !!func.apply(this, args);
};
}
event = event || window.event; // IE-ism
object.target = event.target || event.srcElement || event.originalTarget;
object.element = this;
object.type = event.type;
return !!func ? function () {
return true;
} : function () {
return false;
};
}
// Support touch
// http://www.creativebloq.com/javascript/make-your-site-work-touch-devices-51411644
var prefix = ['webkit', 'moz', 'ms', 'o'];
if (event.targetTouches) {
object.x = event.targetTouches[0].clientX;
object.y = event.targetTouches[0].clientY;
object.pageX = event.pageX;
object.pageY = event.pageY;
} else {
var requestAnimationFrame = function () {
// If pageX/Y aren't available and clientX/Y are,
// calculate pageX/Y - logic taken from jQuery.
// (This is to support old IE)
// NOTE Hopefully this can be removed soon.
for (var i = 0, limit = prefix.length; i < limit && !window.requestAnimationFrame; ++i) {
window.requestAnimationFrame = window[prefix[i] + 'RequestAnimationFrame'];
}
if (event.pageX === null && event.clientX !== null) {
var eventDoc = event.target && event.target.ownerDocument || document;
var doc = eventDoc.documentElement;
var body = eventDoc.body;
if (!window.requestAnimationFrame) {
(function () {
var lastTime = 0;
object.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
object.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
} else {
object.pageX = event.pageX;
object.pageY = event.pageY;
}
window.requestAnimationFrame = function (callback) {
var now = new Date().getTime();
var ttc = Math.max(0, 16 - now - lastTime);
var timer = window.setTimeout(function () {
return callback(now + ttc);
}, ttc);
// pageX, and pageY change with page scroll
// so we're not going to use those for x, and y.
// NOTE Most browsers also alias clientX/Y with x/y
// so that's something to consider down the road.
lastTime = now + ttc;
object.x = event.clientX;
object.y = event.clientY;
}
return timer;
};
})();
}
//NOTE Remember accessibility, Aria roles, and labels.
};
return window.requestAnimationFrame.bind(window);
}();
var requestFrame = function () {
if (requestAnimationFrame) {
return requestAnimationFrame;
} else {
return function (fn) {
return setTimeout(fn);
};
}
}();
var cancelAnimationFrame = function () {
var cancelFrame = function () {
if (cancelAnimationFrame) {
return cancelAnimationFrame;
} else {
return clearTimeout;
}
}();
for (var i = 0, limit = prefix.length; i < limit && !window.cancelAnimationFrame; ++i) {
window.cancelAnimationFrame = window[prefix[i] + 'CancelAnimationFrame'] || window[prefix[i] + 'CancelRequestAnimationFrame'];
}
function AutoScroller(elements) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function (timer) {
window.clearTimeout(timer);
};
}
var self = this;
var maxSpeed = 4,
scrolling = false;
return window.cancelAnimationFrame.bind(window);
}();
this.margin = options.margin || -1;
//this.scrolling = false;
this.scrollWhenOutside = options.scrollWhenOutside || false;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
};
var point = {},
pointCB = index(point),
down = false;
/**
* Returns `true` if provided input is Element.
* @name isElement
* @param {*} [input]
* @returns {boolean}
*/
function isElement (input) {
return input != null && (typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object' && input.nodeType === 1 && _typeof(input.style) === 'object' && _typeof(input.ownerDocument) === 'object';
}
window.addEventListener('mousemove', pointCB, false);
window.addEventListener('touchmove', pointCB, false);
function indexOfElement(elements, element) {
element = resolveElement(element, true);
if (!isElement(element)) return -1;
for (var i = 0; i < elements.length; i++) {
if (elements[i] === element) {
return i;
}
}
return -1;
}
if (!isNaN(options.maxSpeed)) {
maxSpeed = options.maxSpeed;
}
function hasElement(elements, element) {
return -1 !== indexOfElement(elements, element);
}
if (typeof options.autoScroll === 'boolean') {
this.autoScroll = options.autoScroll ? function () {
return true;
} : function () {
return false;
};
} else if (typeof options.autoScroll === 'undefined') {
this.autoScroll = function () {
return false;
};
} else if (typeof options.autoScroll === 'function') {
this.autoScroll = options.autoScroll;
}
function pushElements(elements, toAdd) {
this.destroy = function () {
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);
for (var i = 0; i < toAdd.length; i++) {
if (!hasElement(elements, toAdd[i])) elements.push(toAdd[i]);
}
window.removeEventListener('scroll', setScroll, true);
elements = [];
};
return toAdd;
}
function getElement(element) {
if (typeof element === 'string') {
return document.querySelector(element);
}
return element;
}
function addElements(elements) {
for (var _len2 = arguments.length, toAdd = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
toAdd[_key2 - 1] = arguments[_key2];
}
this.add = function (element) {
element = getElement(element);
toAdd = toAdd.map(resolveElement);
return pushElements(elements, toAdd);
}
for (var i = 0; i < elements.length; i++) {
if (elements[i] === element) return this;
}
function removeElements(elements) {
for (var _len3 = arguments.length, toRemove = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
toRemove[_key3 - 1] = arguments[_key3];
}
elements.push(element);
return this;
};
return toRemove.map(resolveElement).reduce(function (last, e) {
this.remove = function (element) {
element = getElement(element);
var index = indexOfElement(elements, e);
for (var i = 0; i < elements.length; i++) {
if (element === elements[i]) {
elements.splice(i, 1);
return this;
}
}
return this;
};
if (index !== -1) return last.concat(elements.splice(index, 1));
return last;
}, []);
}
var hasWindow = null,
windowAnimationFrame = void 0;
function resolveElement(element, noThrow) {
if (typeof element === 'string') {
try {
return document.querySelector(element);
} catch (e) {
throw e;
}
}
if (Object.prototype.toString.call(elements) !== '[object Array]') {
elements = [elements];
}
if (!isElement(element) && !noThrow) {
throw new TypeError(element + ' is not a DOM element.');
}
return element;
}
(function (temp) {
elements = [];
temp.forEach(function (element) {
if (element === window) {
hasWindow = window;
} else {
self.add(element);
}
});
})(elements);
var index$2 = function createPointCB(object) {
Object.defineProperties(this, {
down: {
get: function get() {
return down;
}
},
maxSpeed: {
get: function get() {
return maxSpeed;
}
},
point: {
get: function get() {
return point;
}
},
scrolling: {
get: function get() {
return scrolling;
}
}
});
// A persistent object (as opposed to returned object) is used to save memory
// This is good to prevent layout thrashing, or for games, and such
var n = 0,
current = null,
animationFrame = void 0;
// NOTE
// This uses IE fixes which should be OK to remove some day. :)
// Some speed will be gained by removal of these.
window.addEventListener('mousedown', onDown, false);
window.addEventListener('touchstart', onDown, false);
window.addEventListener('mouseup', onUp, false);
window.addEventListener('touchend', onUp, false);
// pointCB should be saved in a variable on return
// This allows the usage of element.removeEventListener
window.addEventListener('mousemove', onMove, false);
window.addEventListener('touchmove', onMove, false);
return function pointCB(event) {
window.addEventListener('mouseleave', onMouseOut, false);
event = event || window.event; // IE-ism
object.target = event.target || event.srcElement || event.originalTarget;
object.element = this;
object.type = event.type;
window.addEventListener('scroll', setScroll, true);
// Support touch
// http://www.creativebloq.com/javascript/make-your-site-work-touch-devices-51411644
function setScroll(e) {
if (event.targetTouches) {
object.x = event.targetTouches[0].clientX;
object.y = event.targetTouches[0].clientY;
object.pageX = event.pageX;
object.pageY = event.pageY;
} else {
for (var i = 0; i < elements.length; i++) {
if (elements[i] === e.target) {
scrolling = true;
break;
}
}
// If pageX/Y aren't available and clientX/Y are,
// calculate pageX/Y - logic taken from jQuery.
// (This is to support old IE)
// NOTE Hopefully this can be removed soon.
if (scrolling) {
requestAnimationFrame(function () {
return scrolling = false;
});
}
}
if (event.pageX === null && event.clientX !== null) {
var eventDoc = event.target && event.target.ownerDocument || document;
var doc = eventDoc.documentElement;
var body = eventDoc.body;
function onDown() {
down = true;
}
object.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
object.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
} else {
object.pageX = event.pageX;
object.pageY = event.pageY;
}
function onUp() {
down = false;
cancelFrame(animationFrame);
cancelFrame(windowAnimationFrame);
}
// pageX, and pageY change with page scroll
// so we're not going to use those for x, and y.
// NOTE Most browsers also alias clientX/Y with x/y
// so that's something to consider down the road.
function onMouseOut() {
down = false;
}
object.x = event.clientX;
object.y = event.clientY;
}
};
function getTarget(target) {
if (!target) {
return null;
}
//NOTE Remember accessibility, Aria roles, and labels.
};
if (current === target) {
return target;
}
function createWindowRect() {
var props = {
top: { value: 0, enumerable: true },
left: { value: 0, enumerable: true },
right: { value: window.innerWidth, enumerable: true },
bottom: { value: window.innerHeight, enumerable: true },
width: { value: window.innerWidth, enumerable: true },
height: { value: window.innerHeight, enumerable: true },
x: { value: 0, enumerable: true },
y: { value: 0, enumerable: true }
};
for (var i = 0; i < elements.length; i++) {
if (elements[i] === target) {
return target;
}
}
if (Object.create) {
return Object.create({}, props);
} else {
var rect = {};
Object.defineProperties(rect, props);
return rect;
}
}
while (target = target.parentNode) {
for (var i = 0; i < elements.length; i++) {
if (elements[i] === target) {
return target;
}
}
}
function getClientRect(el) {
if (el === window) {
return createWindowRect();
} else {
try {
var rect = el.getBoundingClientRect();
if (rect.x === undefined) {
rext.x = rect.left;
rect.y = rect.top;
}
return rect;
} catch (e) {
throw new TypeError("Can't call getBoundingClientRect on " + el);
}
}
}
return null;
}
function pointInside(point, el) {
var rect = getClientRect(el);
return point.y > rect.top && point.y < rect.bottom && point.x > rect.left && point.x < rect.right;
}
function getElementUnderPoint() {
var underPoint = null;
function AutoScroller(elements) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
for (var i = 0; i < elements.length; i++) {
if (inside(point, elements[i])) {
underPoint = elements[i];
}
}
var self = this;
var maxSpeed = 4,
scrolling = false;
return underPoint;
}
this.margin = options.margin || -1;
//this.scrolling = false;
this.scrollWhenOutside = options.scrollWhenOutside || false;
function onMove(event) {
var point = {},
pointCB = index$2(point),
down = false;
if (!self.autoScroll()) return;
var target = event.target,
body = document.body;
window.addEventListener('mousemove', pointCB, false);
window.addEventListener('touchmove', pointCB, false);
if (current && !inside(point, current)) {
if (!self.scrollWhenOutside) {
current = null;
}
}
if (!isNaN(options.maxSpeed)) {
maxSpeed = options.maxSpeed;
}
if (target && target.parentNode === body) {
//The special condition to improve speed.
target = getElementUnderPoint();
} else {
target = getTarget(target);
this.autoScroll = boolean(options.autoScroll);
if (!target) {
target = getElementUnderPoint();
}
}
this.destroy = function () {
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);
if (target && target !== current) {
current = target;
}
window.removeEventListener('scroll', setScroll, true);
elements = [];
};
if (hasWindow) {
cancelFrame(windowAnimationFrame);
windowAnimationFrame = requestFrame(scrollWindow);
}
this.add = function () {
for (var _len = arguments.length, element = Array(_len), _key = 0; _key < _len; _key++) {
element[_key] = arguments[_key];
}
if (!current) {
return;
}
addElements.apply(undefined, [elements].concat(element));
return this;
};
cancelFrame(animationFrame);
animationFrame = requestFrame(scrollTick);
}
this.remove = function () {
for (var _len2 = arguments.length, element = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
element[_key2] = arguments[_key2];
}
function scrollWindow() {
autoScroll(hasWindow);
return removeElements.apply(undefined, [elements].concat(element));
};
cancelFrame(windowAnimationFrame);
windowAnimationFrame = requestFrame(scrollWindow);
}
var hasWindow = null,
windowAnimationFrame = void 0;
function scrollTick() {
if (Object.prototype.toString.call(elements) !== '[object Array]') {
elements = [elements];
}
if (!current) {
return;
}
(function (temp) {
elements = [];
temp.forEach(function (element) {
if (element === window) {
hasWindow = window;
} else {
self.add(element);
}
});
})(elements);
autoScroll(current);
Object.defineProperties(this, {
down: {
get: function get() {
return down;
}
},
maxSpeed: {
get: function get() {
return maxSpeed;
}
},
point: {
get: function get() {
return point;
}
},
scrolling: {
get: function get() {
return scrolling;
}
}
});
cancelFrame(animationFrame);
animationFrame = requestFrame(scrollTick);
}
var n = 0,
current = null,
animationFrame = void 0;
function autoScroll(el) {
var rect = getRect(el),
scrollx = void 0,
scrolly = void 0;
window.addEventListener('mousedown', onDown, false);
window.addEventListener('touchstart', onDown, false);
window.addEventListener('mouseup', onUp, false);
window.addEventListener('touchend', onUp, false);
if (point.x < rect.left + self.margin) {
scrollx = Math.floor(Math.max(-1, (point.x - rect.left) / self.margin - 1) * self.maxSpeed);
} else if (point.x > rect.right - self.margin) {
scrollx = Math.ceil(Math.min(1, (point.x - rect.right) / self.margin + 1) * self.maxSpeed);
} else {
scrollx = 0;
}
window.addEventListener('mousemove', onMove, false);
window.addEventListener('touchmove', onMove, false);
if (point.y < rect.top + self.margin) {
scrolly = Math.floor(Math.max(-1, (point.y - rect.top) / self.margin - 1) * self.maxSpeed);
} else if (point.y > rect.bottom - self.margin) {
scrolly = Math.ceil(Math.min(1, (point.y - rect.bottom) / self.margin + 1) * self.maxSpeed);
} else {
scrolly = 0;
}
window.addEventListener('mouseleave', onMouseOut, false);
setTimeout(function () {
window.addEventListener('scroll', setScroll, true);
if (scrolly) {
scrollY(el, scrolly);
}
function setScroll(e) {
if (scrollx) {
scrollX(el, scrollx);
}
});
}
for (var i = 0; i < elements.length; i++) {
if (elements[i] === e.target) {
scrolling = true;
break;
}
}
function scrollY(el, amount) {
if (el === window) {
window.scrollTo(el.pageXOffset, el.pageYOffset + amount);
} else {
el.scrollTop += amount;
}
}
if (scrolling) {
requestAnimationFrame(function () {
return scrolling = false;
});
}
}
function scrollX(el, amount) {
if (el === window) {
window.scrollTo(el.pageXOffset + amount, el.pageYOffset);
} else {
el.scrollLeft += amount;
}
}
}
function onDown() {
down = true;
}
function AutoScrollerFactory(element, options) {
return new AutoScroller(element, options);
}
function onUp() {
down = false;
cancelAnimationFrame(animationFrame);
cancelAnimationFrame(windowAnimationFrame);
}
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) {
throw new TypeError("Can't call getBoundingClientRect on " + el);
}
}
}
function onMouseOut() {
down = false;
}
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;
}
function getTarget(target) {
if (!target) {
return null;
}
/*
git remote add origin https://github.com/hollowdoor/dom_autoscroller.git
git push -u origin master
*/
if (current === target) {
return target;
}
return AutoScrollerFactory;
if (hasElement(elements, target)) {
return target;
}
while (target = target.parentNode) {
if (hasElement(elements, target)) {
return target;
}
}
return null;
}
function getElementUnderPoint() {
var underPoint = null;
for (var i = 0; i < elements.length; i++) {
if (inside(point, elements[i])) {
underPoint = elements[i];
}
}
return underPoint;
}
function onMove(event) {
if (!self.autoScroll()) return;
var target = event.target,
body = document.body;
if (current && !inside(point, current)) {
if (!self.scrollWhenOutside) {
current = null;
}
}
if (target && target.parentNode === body) {
//The special condition to improve speed.
target = getElementUnderPoint();
} else {
target = getTarget(target);
if (!target) {
target = getElementUnderPoint();
}
}
if (target && target !== current) {
current = target;
}
if (hasWindow) {
cancelAnimationFrame(windowAnimationFrame);
windowAnimationFrame = requestAnimationFrame(scrollWindow);
}
if (!current) {
return;
}
cancelAnimationFrame(animationFrame);
animationFrame = requestAnimationFrame(scrollTick);
}
function scrollWindow() {
autoScroll(hasWindow);
cancelAnimationFrame(windowAnimationFrame);
windowAnimationFrame = requestAnimationFrame(scrollWindow);
}
function scrollTick() {
if (!current) {
return;
}
autoScroll(current);
cancelAnimationFrame(animationFrame);
animationFrame = requestAnimationFrame(scrollTick);
}
function autoScroll(el) {
var rect = getClientRect(el),
scrollx = void 0,
scrolly = void 0;
if (point.x < rect.left + self.margin) {
scrollx = Math.floor(Math.max(-1, (point.x - rect.left) / self.margin - 1) * self.maxSpeed);
} else if (point.x > rect.right - self.margin) {
scrollx = Math.ceil(Math.min(1, (point.x - rect.right) / self.margin + 1) * self.maxSpeed);
} else {
scrollx = 0;
}
if (point.y < rect.top + self.margin) {
scrolly = Math.floor(Math.max(-1, (point.y - rect.top) / self.margin - 1) * self.maxSpeed);
} else if (point.y > rect.bottom - self.margin) {
scrolly = Math.ceil(Math.min(1, (point.y - rect.bottom) / self.margin + 1) * self.maxSpeed);
} else {
scrolly = 0;
}
setTimeout(function () {
if (scrolly) {
scrollY(el, scrolly);
}
if (scrollx) {
scrollX(el, scrollx);
}
});
}
function scrollY(el, amount) {
if (el === window) {
window.scrollTo(el.pageXOffset, el.pageYOffset + amount);
} else {
el.scrollTop += amount;
}
}
function scrollX(el, amount) {
if (el === window) {
window.scrollTo(el.pageXOffset + amount, el.pageYOffset);
} else {
el.scrollLeft += amount;
}
}
}
function AutoScrollerFactory(element, options) {
return new AutoScroller(element, options);
}
function inside(point, el, rect) {
if (!rect) {
return pointInside(point, el);
} else {
return point.y > rect.top && point.y < rect.bottom && point.x > rect.left && point.x < rect.right;
}
}
/*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){
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);
}*/
/*
git remote add origin https://github.com/hollowdoor/dom_autoscroller.git
git push -u origin master
*/
return AutoScrollerFactory;
}());
//# sourceMappingURL=dom-autoscroller.js.map

@@ -1,1 +0,1 @@

var autoScroll=function(){"use strict";function e(e){function t(e){return"string"==typeof e?document.querySelector(e):e}function a(t){for(var n=0;n<e.length;n++)if(e[n]===t.target){S=!0;break}S&&requestAnimationFrame(function(){return S=!1})}function c(){X=!0}function l(){X=!1,u(M),u(Y)}function d(){X=!1}function f(t){if(!t)return null;if(O===t)return t;for(var n=0;n<e.length;n++)if(e[n]===t)return t;for(;t=t.parentNode;)for(var n=0;n<e.length;n++)if(e[n]===t)return t;return null}function s(){for(var t=null,n=0;n<e.length;n++)o(x,e[n])&&(t=e[n]);return t}function m(e){if(L.autoScroll()){var t=e.target,n=document.body;O&&!o(x,O)&&(L.scrollWhenOutside||(O=null)),t&&t.parentNode===n?t=s():(t=f(t),t||(t=s())),t&&t!==O&&(O=t),b&&(u(Y),Y=i(w)),O&&(u(M),M=i(g))}}function w(){h(b),u(Y),Y=i(w)}function g(){O&&(h(O),u(M),M=i(g))}function h(e){var t=n(e),o=void 0,r=void 0;o=x.x<t.left+L.margin?Math.floor(Math.max(-1,(x.x-t.left)/L.margin-1)*L.maxSpeed):x.x>t.right-L.margin?Math.ceil(Math.min(1,(x.x-t.right)/L.margin+1)*L.maxSpeed):0,r=x.y<t.top+L.margin?Math.floor(Math.max(-1,(x.y-t.top)/L.margin-1)*L.maxSpeed):x.y>t.bottom-L.margin?Math.ceil(Math.min(1,(x.y-t.bottom)/L.margin+1)*L.maxSpeed):0,setTimeout(function(){r&&v(e,r),o&&p(e,o)})}function v(e,t){e===window?window.scrollTo(e.pageXOffset,e.pageYOffset+t):e.scrollTop+=t}function p(e,t){e===window?window.scrollTo(e.pageXOffset+t,e.pageYOffset):e.scrollLeft+=t}var y=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},L=this,E=4,S=!1;this.margin=y.margin||-1,this.scrollWhenOutside=y.scrollWhenOutside||!1;var x={},T=r(x),X=!1;window.addEventListener("mousemove",T,!1),window.addEventListener("touchmove",T,!1),isNaN(y.maxSpeed)||(E=y.maxSpeed),"boolean"==typeof y.autoScroll?this.autoScroll=y.autoScroll?function(){return!0}:function(){return!1}:"undefined"==typeof y.autoScroll?this.autoScroll=function(){return!1}:"function"==typeof y.autoScroll&&(this.autoScroll=y.autoScroll),this.destroy=function(){window.removeEventListener("mousemove",T,!1),window.removeEventListener("touchmove",T,!1),window.removeEventListener("mousedown",c,!1),window.removeEventListener("touchstart",c,!1),window.removeEventListener("mouseup",l,!1),window.removeEventListener("touchend",l,!1),window.removeEventListener("scroll",a,!0),e=[]},this.add=function(n){n=t(n);for(var o=0;o<e.length;o++)if(e[o]===n)return this;return e.push(n),this},this.remove=function(n){n=t(n);for(var o=0;o<e.length;o++)if(n===e[o])return e.splice(o,1),this;return this};var b=null,Y=void 0;"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),function(t){e=[],t.forEach(function(e){e===window?b=window:L.add(e)})}(e),Object.defineProperties(this,{down:{get:function(){return X}},maxSpeed:{get:function(){return E}},point:{get:function(){return x}},scrolling:{get:function(){return S}}});var O=null,M=void 0;window.addEventListener("mousedown",c,!1),window.addEventListener("touchstart",c,!1),window.addEventListener("mouseup",l,!1),window.addEventListener("touchend",l,!1),window.addEventListener("mousemove",m,!1),window.addEventListener("touchmove",m,!1),window.addEventListener("mouseleave",d,!1),window.addEventListener("scroll",a,!0)}function t(t,n){return new e(t,n)}function n(e){if(e===window)return{top:0,left:0,right:window.innerWidth,bottom:window.innerHeight,width:window.innerWidth,height:window.innerHeight};try{return e.getBoundingClientRect()}catch(t){throw new TypeError("Can't call getBoundingClientRect on "+e)}}function o(e,t,o){return o=o||n(t),e.y>o.top&&e.y<o.bottom&&e.x>o.left&&e.x<o.right}var r=function(e){return function(t){if(t=t||window.event,e.target=t.target||t.srcElement||t.originalTarget,e.element=this,e.type=t.type,t.targetTouches)e.x=t.targetTouches[0].clientX,e.y=t.targetTouches[0].clientY,e.pageX=t.pageX,e.pageY=t.pageY;else{if(null===t.pageX&&null!==t.clientX){var n=t.target&&t.target.ownerDocument||document,o=n.documentElement,r=n.body;e.pageX=t.clientX+(o&&o.scrollLeft||r&&r.scrollLeft||0)-(o&&o.clientLeft||r&&r.clientLeft||0),e.pageY=t.clientY+(o&&o.scrollTop||r&&r.scrollTop||0)-(o&&o.clientTop||r&&r.clientTop||0)}else e.pageX=t.pageX,e.pageY=t.pageY;e.x=t.clientX,e.y=t.clientY}}},i=function(){return requestAnimationFrame?requestAnimationFrame:function(e){return setTimeout(e)}}(),u=function(){return cancelAnimationFrame?cancelAnimationFrame:clearTimeout}();return t}();
var autoScroll=function(){"use strict";function e(e,n){return"undefined"==typeof e?"undefined"==typeof n?e:n:e}function n(n,t){return n=e(n,t),"function"==typeof n?function(){for(var e=arguments.length,t=Array(e),o=0;o<e;o++)t[o]=arguments[o];return!!n.apply(this,t)}:n?function(){return!0}:function(){return!1}}function t(e){return null!=e&&"object"===("undefined"==typeof e?"undefined":h(e))&&1===e.nodeType&&"object"===h(e.style)&&"object"===h(e.ownerDocument)}function o(e,n){if(n=c(n,!0),!t(n))return-1;for(var o=0;o<e.length;o++)if(e[o]===n)return o;return-1}function r(e,n){return-1!==o(e,n)}function i(e,n){for(var t=0;t<n.length;t++)r(e,n[t])||e.push(n[t]);return n}function a(e){for(var n=arguments.length,t=Array(n>1?n-1:0),o=1;o<n;o++)t[o-1]=arguments[o];return t=t.map(c),i(e,t)}function u(e){for(var n=arguments.length,t=Array(n>1?n-1:0),r=1;r<n;r++)t[r-1]=arguments[r];return t.map(c).reduce(function(n,t){var r=o(e,t);return r!==-1?n.concat(e.splice(r,1)):n},[])}function c(e,n){if("string"==typeof e)try{return document.querySelector(e)}catch(e){throw e}if(!t(e)&&!n)throw new TypeError(e+" is not a DOM element.");return e}function l(){var e={top:{value:0,enumerable:!0},left:{value:0,enumerable:!0},right:{value:window.innerWidth,enumerable:!0},bottom:{value:window.innerHeight,enumerable:!0},width:{value:window.innerWidth,enumerable:!0},height:{value:window.innerHeight,enumerable:!0},x:{value:0,enumerable:!0},y:{value:0,enumerable:!0}};if(Object.create)return Object.create({},e);var n={};return Object.defineProperties(n,e),n}function d(e){if(e===window)return l();try{var n=e.getBoundingClientRect();return void 0===n.x&&(rext.x=n.left,n.y=n.top),n}catch(n){throw new TypeError("Can't call getBoundingClientRect on "+e)}}function f(e,n){var t=d(n);return e.y>t.top&&e.y<t.bottom&&e.x>t.left&&e.x<t.right}function m(e){function t(n){for(var t=0;t<e.length;t++)if(e[t]===n.target){T=!0;break}T&&g(function(){return T=!1})}function o(){O=!0}function i(){O=!1,p(j),p(M)}function c(){O=!1}function l(n){if(!n)return null;if(Y===n)return n;if(r(e,n))return n;for(;n=n.parentNode;)if(r(e,n))return n;return null}function f(){for(var n=null,t=0;t<e.length;t++)s(S,e[t])&&(n=e[t]);return n}function m(e){if(L.autoScroll()){var n=e.target,t=document.body;Y&&!s(S,Y)&&(L.scrollWhenOutside||(Y=null)),n&&n.parentNode===t?n=f():(n=l(n),n||(n=f())),n&&n!==Y&&(Y=n),X&&(p(M),M=g(w)),Y&&(p(j),j=g(v))}}function w(){h(X),p(M),M=g(w)}function v(){Y&&(h(Y),p(j),j=g(v))}function h(e){var n=d(e),t=void 0,o=void 0;t=S.x<n.left+L.margin?Math.floor(Math.max(-1,(S.x-n.left)/L.margin-1)*L.maxSpeed):S.x>n.right-L.margin?Math.ceil(Math.min(1,(S.x-n.right)/L.margin+1)*L.maxSpeed):0,o=S.y<n.top+L.margin?Math.floor(Math.max(-1,(S.y-n.top)/L.margin-1)*L.maxSpeed):S.y>n.bottom-L.margin?Math.ceil(Math.min(1,(S.y-n.bottom)/L.margin+1)*L.maxSpeed):0,setTimeout(function(){o&&b(e,o),t&&x(e,t)})}function b(e,n){e===window?window.scrollTo(e.pageXOffset,e.pageYOffset+n):e.scrollTop+=n}function x(e,n){e===window?window.scrollTo(e.pageXOffset+n,e.pageYOffset):e.scrollLeft+=n}var E=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},L=this,A=4,T=!1;this.margin=E.margin||-1,this.scrollWhenOutside=E.scrollWhenOutside||!1;var S={},F=y(S),O=!1;window.addEventListener("mousemove",F,!1),window.addEventListener("touchmove",F,!1),isNaN(E.maxSpeed)||(A=E.maxSpeed),this.autoScroll=n(E.autoScroll),this.destroy=function(){window.removeEventListener("mousemove",F,!1),window.removeEventListener("touchmove",F,!1),window.removeEventListener("mousedown",o,!1),window.removeEventListener("touchstart",o,!1),window.removeEventListener("mouseup",i,!1),window.removeEventListener("touchend",i,!1),window.removeEventListener("scroll",t,!0),e=[]},this.add=function(){for(var n=arguments.length,t=Array(n),o=0;o<n;o++)t[o]=arguments[o];return a.apply(void 0,[e].concat(t)),this},this.remove=function(){for(var n=arguments.length,t=Array(n),o=0;o<n;o++)t[o]=arguments[o];return u.apply(void 0,[e].concat(t))};var X=null,M=void 0;"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),function(n){e=[],n.forEach(function(e){e===window?X=window:L.add(e)})}(e),Object.defineProperties(this,{down:{get:function(){return O}},maxSpeed:{get:function(){return A}},point:{get:function(){return S}},scrolling:{get:function(){return T}}});var Y=null,j=void 0;window.addEventListener("mousedown",o,!1),window.addEventListener("touchstart",o,!1),window.addEventListener("mouseup",i,!1),window.addEventListener("touchend",i,!1),window.addEventListener("mousemove",m,!1),window.addEventListener("touchmove",m,!1),window.addEventListener("mouseleave",c,!1),window.addEventListener("scroll",t,!0)}function w(e,n){return new m(e,n)}function s(e,n,t){return t?e.y>t.top&&e.y<t.bottom&&e.x>t.left&&e.x<t.right:f(e,n)}var v=["webkit","moz","ms","o"],g=function(){for(var e=0,n=v.length;e<n&&!window.requestAnimationFrame;++e)window.requestAnimationFrame=window[v[e]+"RequestAnimationFrame"];return window.requestAnimationFrame||!function(){var e=0;window.requestAnimationFrame=function(n){var t=(new Date).getTime(),o=Math.max(0,16-t-e),r=window.setTimeout(function(){return n(t+o)},o);return e=t+o,r}}(),window.requestAnimationFrame.bind(window)}(),p=function(){for(var e=0,n=v.length;e<n&&!window.cancelAnimationFrame;++e)window.cancelAnimationFrame=window[v[e]+"CancelAnimationFrame"]||window[v[e]+"CancelRequestAnimationFrame"];return window.cancelAnimationFrame||(window.cancelAnimationFrame=function(e){window.clearTimeout(e)}),window.cancelAnimationFrame.bind(window)}(),h="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},y=function(e){return function(n){if(n=n||window.event,e.target=n.target||n.srcElement||n.originalTarget,e.element=this,e.type=n.type,n.targetTouches)e.x=n.targetTouches[0].clientX,e.y=n.targetTouches[0].clientY,e.pageX=n.pageX,e.pageY=n.pageY;else{if(null===n.pageX&&null!==n.clientX){var t=n.target&&n.target.ownerDocument||document,o=t.documentElement,r=t.body;e.pageX=n.clientX+(o&&o.scrollLeft||r&&r.scrollLeft||0)-(o&&o.clientLeft||r&&r.clientLeft||0),e.pageY=n.clientY+(o&&o.scrollTop||r&&r.scrollTop||0)-(o&&o.clientTop||r&&r.clientTop||0)}else e.pageX=n.pageX,e.pageY=n.pageY;e.x=n.clientX,e.y=n.clientY}}};return w}();
{
"name": "dom-autoscroller",
"version": "2.1.6",
"version": "2.2.0",
"description": "Auto scroll dom elements",

@@ -24,3 +24,7 @@ "main": "dist/bundle.js",

"dependencies": {
"create-point-cb": "^1.0.0"
"animation-frame-polyfill": "^1.0.0",
"create-point-cb": "^1.0.0",
"dom-plane": "^1.0.0",
"dom-set": "^1.0.1",
"type-func": "^1.0.1"
},

@@ -27,0 +31,0 @@ "devDependencies": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc