scroll-into-view
Advanced tools
Comparing version 1.0.3 to 1.0.4
{ | ||
"name": "scroll-into-view", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "scrollIntoView.js", |
@@ -1,2 +0,43 @@ | ||
function transitionScrollTo(element, x, y, startTime){ | ||
function setElementScroll(element, x, y){ | ||
if(element === window){ | ||
element.scrollTo(x, y); | ||
}else{ | ||
element.scrollLeft = x; | ||
element.scrollTop = y; | ||
} | ||
} | ||
function getTargetScrollLocation(target, parent){ | ||
var targetPosition = target.getBoundingClientRect(), | ||
x, | ||
y, | ||
differenceX, | ||
differenceY; | ||
if(parent === window){ | ||
x = targetPosition.left + window.scrollX - window.innerWidth / 2 + Math.min(targetPosition.width, window.innerWidth) / 2; | ||
y = targetPosition.top + window.scrollY - window.innerHeight / 2 + Math.min(targetPosition.height, window.innerHeight) / 2; | ||
x = Math.max(Math.min(x, document.body.clientWidth - window.innerWidth), 0); | ||
y = Math.max(Math.min(y, document.body.clientHeight - window.innerHeight), 0); | ||
differenceX = x - window.scrollX; | ||
differenceY = y - window.scrollY; | ||
}else{ | ||
x = Math.min(target.offsetLeft + (targetPosition.width / 2) - parent.clientWidth / 2, target.offsetLeft); | ||
y = Math.min(target.offsetTop + (targetPosition.height / 2) - parent.clientHeight / 2, target.offsetTop); | ||
x = Math.max(Math.min(x, parent.scrollWidth - parent.clientWidth), 0); | ||
y = Math.max(Math.min(y, parent.scrollHeight - parent.clientHeight), 0); | ||
differenceX = x - parent.scrollLeft; | ||
differenceY = y - parent.scrollTop; | ||
} | ||
return { | ||
x: x, | ||
y: y, | ||
differenceX: differenceX, | ||
differenceY: differenceY | ||
}; | ||
} | ||
function transitionScrollTo(target, parent, startTime){ | ||
if(!startTime){ | ||
@@ -6,20 +47,20 @@ startTime = +new Date(); | ||
x = Math.max(Math.min(x, element.scrollWidth - element.clientWidth), 0); | ||
y = Math.max(Math.min(y, element.scrollHeight - element.clientHeight), 0); | ||
requestAnimationFrame(function(){ | ||
var location = getTargetScrollLocation(target, parent); | ||
var differenceY = y - element.scrollTop, | ||
differenceX = x - element.scrollLeft; | ||
requestAnimationFrame(function(){ | ||
if(new Date() - startTime > 350){ | ||
// Give up and set the scroll position | ||
element.scrollTop += differenceY; | ||
element.scrollLeft += differenceX; | ||
setElementScroll(parent, | ||
location.x, | ||
location.y | ||
); | ||
return; | ||
} | ||
element.scrollTop += differenceY * 0.2 + 1; | ||
element.scrollLeft += differenceX * 0.2 + 1; | ||
setElementScroll(parent, | ||
location.x - location.differenceX * 0.3, | ||
location.y - location.differenceY * 0.3 | ||
); | ||
if(Math.abs(differenceY) > 1 || Math.abs(differenceX) > 1){ | ||
transitionScrollTo(element, x, y, startTime); | ||
if(Math.abs(location.differenceY) > 1 || Math.abs(location.differenceX) > 1){ | ||
transitionScrollTo(target, parent, startTime); | ||
} | ||
@@ -46,16 +87,9 @@ }); | ||
){ | ||
transitionScrollTo(parent, | ||
Math.min(target.offsetLeft + (targetPosition.width / 2) - parent.clientWidth / 2, target.offsetLeft), | ||
Math.min(target.offsetTop + (targetPosition.height / 2) - parent.clientHeight / 2, target.offsetTop) | ||
) | ||
transitionScrollTo(target, parent); | ||
} | ||
parent = parent.parentNode; | ||
} | ||
transitionScrollTo(parent, | ||
targetPosition.left + window.scrollX - window.innerWidth / 2 + Math.min(targetPosition.width, window.innerWidth) / 2, | ||
targetPosition.top + window.scrollY - window.innerHeight / 2 + Math.min(targetPosition.height, window.innerHeight) / 2 | ||
); | ||
transitionScrollTo(target, window); | ||
}; |
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
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
8632
192