Skip to content

Commit

Permalink
improve scroll performance on Safari macOS
Browse files Browse the repository at this point in the history
copied from rgalus#68
  • Loading branch information
SassNinja committed Jul 9, 2020
1 parent 35571be commit 1ef3db0
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ class Sticky {
this.resetWrapperRectangle(element);
}

element.sticky.rect = this.getRectangle(element);
element.sticky.container.rect = this.getRectangle(element.sticky.container);
element.sticky.rect = this.getRectangle(element, false);
element.sticky.container.rect = this.getRectangle(element.sticky.container, false);

if (
((element.sticky.rect.top + element.sticky.rect.height) < (element.sticky.container.rect.top + element.sticky.container.rect.height))
Expand Down Expand Up @@ -406,8 +406,8 @@ class Sticky {
* @param {node} element - Element which position & rectangle are returned
* @return {object}
*/
getRectangle(element) {
this.css(element, { position: '', width: '', top: '', left: '' });
getRectangle(element, useAnimationFrame) {
this.css(element, { position: '', width: '', top: '', left: '' }, useAnimationFrame);

const width = Math.max(element.offsetWidth, element.clientWidth, element.scrollWidth);
const height = Math.max(element.offsetHeight, element.clientHeight, element.scrollHeight);
Expand Down Expand Up @@ -467,11 +467,26 @@ class Sticky {
* @param {node} element - DOM element
* @param {object} properties - CSS properties that will be added/removed from specified element
*/
css(element, properties) {
for (let property in properties) {
if (properties.hasOwnProperty(property)) {
element.style[property] = properties[property];
css(element, properties, useAnimationFrame) {

if (typeof useAnimationFrame === 'undefined') {
useAnimationFrame = true;
}

if (!useAnimationFrame) {
for (let property in properties) {
if (properties.hasOwnProperty(property)) {
element.style[property] = properties[property];
}
}
} else {
window.requestAnimationFrame(function() {
for (let property in properties) {
if (properties.hasOwnProperty(property)) {
element.style[property] = properties[property];
}
}
});
}
}
}
Expand Down

0 comments on commit 1ef3db0

Please sign in to comment.