forked from hsnaydd/moveTo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support scroll any element container
- Loading branch information
Showing
5 changed files
with
97 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<script src="../dist/moveTo.js"></script> | ||
<title>Title</title> | ||
</head> | ||
<body> | ||
<div id="container"> | ||
<div id="content"> | ||
1<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> | ||
2<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> | ||
3<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> | ||
|
||
<div id="target">target</div> | ||
|
||
4<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> | ||
5<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> | ||
</div> | ||
</div> | ||
|
||
<style> | ||
#container { | ||
width: 300px; | ||
border: 1px solid #000; | ||
height: 700px; | ||
overflow: scroll; | ||
} | ||
#content { | ||
width: 100%; | ||
background-color: #ff6600; | ||
font-size: 50px; | ||
} | ||
</style> | ||
|
||
<script> | ||
window.onload = function () { | ||
new MoveTo({ | ||
tolerance: 0, | ||
duration: 800, | ||
easing: 'easeOutQuart', | ||
container: document.getElementById('container'), | ||
}).move(document.getElementById('target')); | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/*! | ||
* MoveTo - A lightweight scroll animation javascript library without any dependency. | ||
* Version 1.7.4 (28-09-2018 16:02) | ||
* Version 1.7.4 (06-01-2019 19:51) | ||
* Licensed under MIT | ||
* Copyright 2018 Hasan Aydoğdu <[email protected]> | ||
* Copyright 2019 Hasan Aydoğdu <[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
|
@@ -15,17 +15,18 @@ var MoveTo = function () { | |
tolerance: 0, | ||
duration: 800, | ||
easing: 'easeOutQuart', | ||
callback: function callback() {} }; | ||
callback: function callback() {}, | ||
container: window }; | ||
|
||
|
||
/** | ||
* easeOutQuart Easing Function | ||
* @param {number} t - current time | ||
* @param {number} b - start value | ||
* @param {number} c - change in value | ||
* @param {number} d - duration | ||
* @return {number} - calculated value | ||
*/ | ||
* easeOutQuart Easing Function | ||
* @param {number} t - current time | ||
* @param {number} b - start value | ||
* @param {number} c - change in value | ||
* @param {number} d - duration | ||
* @return {number} - calculated value | ||
*/ | ||
function easeOutQuart(t, b, c, d) { | ||
t /= d; | ||
t--; | ||
|
@@ -62,6 +63,18 @@ var MoveTo = function () { | |
}); | ||
}; | ||
|
||
/** | ||
* Count a number of item scrolled top | ||
* @param {Window|HTMLElement} container | ||
* @return {number} | ||
*/ | ||
function countScrollTop(container) { | ||
if (container instanceof HTMLElement) { | ||
return container.scrollTop; | ||
} | ||
return container.pageYOffset; | ||
}; | ||
|
||
/** | ||
* MoveTo Constructor | ||
* @param {object} options Options | ||
|
@@ -118,14 +131,14 @@ var MoveTo = function () { | |
options = mergeObject(this.options, options); | ||
|
||
var distance = typeof target === 'number' ? target : target.getBoundingClientRect().top; | ||
var from = window.pageYOffset; | ||
var from = countScrollTop(options.container); | ||
var startTime = null; | ||
var lastPageYOffset = void 0; | ||
distance -= options.tolerance; | ||
|
||
// rAF loop | ||
var loop = function loop(currentTime) { | ||
var currentPageYOffset = window.pageYOffset; | ||
var currentPageYOffset = countScrollTop(_this2.options.container); | ||
|
||
if (!startTime) { | ||
// To starts time from 1, we subtracted 1 from current time | ||
|
@@ -150,12 +163,12 @@ var MoveTo = function () { | |
timeElapsed, from, distance, options.duration); | ||
|
||
|
||
window.scroll(0, val); | ||
options.container.scroll(0, val); | ||
|
||
if (timeElapsed < options.duration) { | ||
window.requestAnimationFrame(loop); | ||
} else { | ||
window.scroll(0, distance + from); | ||
options.container.scroll(0, distance + from); | ||
options.callback(target); | ||
} | ||
}; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters