diff --git a/.nvmrc b/.nvmrc index ec635144..f599e28b 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -9 +10 diff --git a/dist/jquery.stickybits.min.js b/dist/jquery.stickybits.min.js index 93b3c530..ada64c08 100644 --- a/dist/jquery.stickybits.min.js +++ b/dist/jquery.stickybits.min.js @@ -1,8 +1,8 @@ /** stickybits - Stickybits is a lightweight alternative to `position: sticky` polyfills - @version v3.3.1 + @version v3.3.2 @link https://github.com/dollarshaveclub/stickybits#readme @author Jeff Wainwright (https://jeffry.in) @license MIT **/ -!function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s():"function"==typeof define&&define.amd?define(s):s()}(0,function(){"use strict";var t=function(){function t(t,s){var e=void 0!==s?s:{};this.version="3.3.1",this.userAgent=window.navigator.userAgent||"no `userAgent` provided by the browser",this.props={customStickyChangeNumber:e.customStickyChangeNumber||null,noStyles:e.noStyles||!1,stickyBitStickyOffset:e.stickyBitStickyOffset||0,parentClass:e.parentClass||"js-stickybit-parent",scrollEl:document.querySelector(e.scrollEl)||window,stickyClass:e.stickyClass||"js-is-sticky",stuckClass:e.stuckClass||"js-is-stuck",stickyChangeClass:e.stickyChangeClass||"js-is-sticky--change",useStickyClasses:e.useStickyClasses||!1,verticalPosition:e.verticalPosition||"top"};var i=this.props;i.positionVal=this.definePosition()||"fixed";var n=i.verticalPosition,o=i.noStyles,a=i.positionVal;this.els="string"==typeof t?document.querySelectorAll(t):t,"length"in this.els||(this.els=[this.els]),this.instances=[];for(var r=0;r=r&&"sticky"===n;v>o&&v=a&&v<=r;return vr?m(function(){g(e,h)}):S&&m(function(){g(e,"stub",h)}),s},s.update=function(){for(var t=0;t=r&&"sticky"===n;v>o&&v=a&&v<=r;return vr?m(function(){g(e,h)}):S&&m(function(){g(e,"stub",h)}),s},s.update=function(){for(var t=0;t (https://jeffry.in) @license MIT @@ -50,6 +50,7 @@ - .definePosition = defines sticky or fixed - .addInstance = an array of objects for each Stickybits Target - .getClosestParent = gets the parent for non-window scroll + - .getOffsetTop = gets the element offsetTop from the top level of the DOM - .computeScrollOffsets = computes scroll position - .toggleClasses = older browser toggler - .manageState = manages sticky state @@ -62,7 +63,7 @@ var Stickybits = function () { function Stickybits(target, obj) { var o = typeof obj !== 'undefined' ? obj : {}; - this.version = '3.3.1'; + this.version = '3.3.2'; this.userAgent = window.navigator.userAgent || 'no `userAgent` provided by the browser'; this.props = { customStickyChangeNumber: o.customStickyChangeNumber || null, @@ -202,6 +203,24 @@ function () { return p; }; + /* + -------- + getOffsetTop + -------- + - a helper function that gets the offsetTop of the element + - from the top level of the DOM + */ + + + _proto.getOffsetTop = function getOffsetTop(el) { + var offsetTop = 0; + + do { + offsetTop = el.offsetTop + offsetTop; + } while (el = el.offsetParent); + + return offsetTop; + }; /* computeScrollOffsets 📊 --- @@ -220,8 +239,8 @@ function () { var parent = it.parent; var isCustom = !this.isWin && p.positionVal === 'fixed'; var isBottom = p.verticalPosition !== 'bottom'; - var scrollElOffset = isCustom ? p.scrollEl.getBoundingClientRect().top : 0; - var stickyStart = isCustom ? parent.getBoundingClientRect().top - scrollElOffset : parent.getBoundingClientRect().top; + var scrollElOffset = isCustom ? this.getOffsetTop(p.scrollEl) : 0; + var stickyStart = isCustom ? this.getOffsetTop(parent) - scrollElOffset : this.getOffsetTop(parent); var stickyChangeOffset = p.customStickyChangeNumber !== null ? p.customStickyChangeNumber : el.offsetHeight; it.offset = scrollElOffset + p.stickyBitStickyOffset; it.stickyStart = isBottom ? stickyStart - it.offset : 0; diff --git a/dist/stickybits.js b/dist/stickybits.js index 734c9cba..1398a96b 100644 --- a/dist/stickybits.js +++ b/dist/stickybits.js @@ -1,6 +1,6 @@ /** stickybits - Stickybits is a lightweight alternative to `position: sticky` polyfills - @version v3.3.1 + @version v3.3.2 @link https://github.com/dollarshaveclub/stickybits#readme @author Jeff Wainwright (https://jeffry.in) @license MIT @@ -56,6 +56,7 @@ - .definePosition = defines sticky or fixed - .addInstance = an array of objects for each Stickybits Target - .getClosestParent = gets the parent for non-window scroll + - .getOffsetTop = gets the element offsetTop from the top level of the DOM - .computeScrollOffsets = computes scroll position - .toggleClasses = older browser toggler - .manageState = manages sticky state @@ -68,7 +69,7 @@ function () { function Stickybits(target, obj) { var o = typeof obj !== 'undefined' ? obj : {}; - this.version = '3.3.1'; + this.version = '3.3.2'; this.userAgent = window.navigator.userAgent || 'no `userAgent` provided by the browser'; this.props = { customStickyChangeNumber: o.customStickyChangeNumber || null, @@ -208,6 +209,24 @@ return p; }; + /* + -------- + getOffsetTop + -------- + - a helper function that gets the offsetTop of the element + - from the top level of the DOM + */ + + + _proto.getOffsetTop = function getOffsetTop(el) { + var offsetTop = 0; + + do { + offsetTop = el.offsetTop + offsetTop; + } while (el = el.offsetParent); + + return offsetTop; + }; /* computeScrollOffsets 📊 --- @@ -226,8 +245,8 @@ var parent = it.parent; var isCustom = !this.isWin && p.positionVal === 'fixed'; var isBottom = p.verticalPosition !== 'bottom'; - var scrollElOffset = isCustom ? p.scrollEl.getBoundingClientRect().top : 0; - var stickyStart = isCustom ? parent.getBoundingClientRect().top - scrollElOffset : parent.getBoundingClientRect().top; + var scrollElOffset = isCustom ? this.getOffsetTop(p.scrollEl) : 0; + var stickyStart = isCustom ? this.getOffsetTop(parent) - scrollElOffset : this.getOffsetTop(parent); var stickyChangeOffset = p.customStickyChangeNumber !== null ? p.customStickyChangeNumber : el.offsetHeight; it.offset = scrollElOffset + p.stickyBitStickyOffset; it.stickyStart = isBottom ? stickyStart - it.offset : 0; diff --git a/dist/stickybits.min.js b/dist/stickybits.min.js index 3a2322d3..637f7991 100644 --- a/dist/stickybits.min.js +++ b/dist/stickybits.min.js @@ -1,8 +1,8 @@ /** stickybits - Stickybits is a lightweight alternative to `position: sticky` polyfills - @version v3.3.1 + @version v3.3.2 @link https://github.com/dollarshaveclub/stickybits#readme @author Jeff Wainwright (https://jeffry.in) @license MIT **/ -!function(t,s){"object"==typeof exports&&"undefined"!=typeof module?module.exports=s():"function"==typeof define&&define.amd?define(s):t.stickybits=s()}(this,function(){"use strict";var t=function(){function t(t,s){var e=void 0!==s?s:{};this.version="3.3.1",this.userAgent=window.navigator.userAgent||"no `userAgent` provided by the browser",this.props={customStickyChangeNumber:e.customStickyChangeNumber||null,noStyles:e.noStyles||!1,stickyBitStickyOffset:e.stickyBitStickyOffset||0,parentClass:e.parentClass||"js-stickybit-parent",scrollEl:document.querySelector(e.scrollEl)||window,stickyClass:e.stickyClass||"js-is-sticky",stuckClass:e.stuckClass||"js-is-stuck",stickyChangeClass:e.stickyChangeClass||"js-is-sticky--change",useStickyClasses:e.useStickyClasses||!1,verticalPosition:e.verticalPosition||"top"};var i=this.props;i.positionVal=this.definePosition()||"fixed";var n=i.verticalPosition,o=i.noStyles,a=i.positionVal;this.els="string"==typeof t?document.querySelectorAll(t):t,"length"in this.els||(this.els=[this.els]),this.instances=[];for(var r=0;r=r&&"sticky"===n;v>o&&v=a&&v<=r;return vr?m(function(){g(e,h)}):w&&m(function(){g(e,"stub",h)}),s},s.update=function(){for(var t=0;t=r&&"sticky"===n;v>o&&v=a&&v<=r;return vr?m(function(){g(e,h)}):w&&m(function(){g(e,"stub",h)}),s},s.update=function(){for(var t=0;t (https://jeffry.in) @license MIT @@ -56,6 +56,7 @@ - .definePosition = defines sticky or fixed - .addInstance = an array of objects for each Stickybits Target - .getClosestParent = gets the parent for non-window scroll + - .getOffsetTop = gets the element offsetTop from the top level of the DOM - .computeScrollOffsets = computes scroll position - .toggleClasses = older browser toggler - .manageState = manages sticky state @@ -68,7 +69,7 @@ function () { function Stickybits(target, obj) { var o = typeof obj !== 'undefined' ? obj : {}; - this.version = '3.3.1'; + this.version = '3.3.2'; this.userAgent = window.navigator.userAgent || 'no `userAgent` provided by the browser'; this.props = { customStickyChangeNumber: o.customStickyChangeNumber || null, @@ -208,6 +209,24 @@ return p; }; + /* + -------- + getOffsetTop + -------- + - a helper function that gets the offsetTop of the element + - from the top level of the DOM + */ + + + _proto.getOffsetTop = function getOffsetTop(el) { + var offsetTop = 0; + + do { + offsetTop = el.offsetTop + offsetTop; + } while (el = el.offsetParent); + + return offsetTop; + }; /* computeScrollOffsets 📊 --- @@ -226,8 +245,8 @@ var parent = it.parent; var isCustom = !this.isWin && p.positionVal === 'fixed'; var isBottom = p.verticalPosition !== 'bottom'; - var scrollElOffset = isCustom ? p.scrollEl.getBoundingClientRect().top : 0; - var stickyStart = isCustom ? parent.getBoundingClientRect().top - scrollElOffset : parent.getBoundingClientRect().top; + var scrollElOffset = isCustom ? this.getOffsetTop(p.scrollEl) : 0; + var stickyStart = isCustom ? this.getOffsetTop(parent) - scrollElOffset : this.getOffsetTop(parent); var stickyChangeOffset = p.customStickyChangeNumber !== null ? p.customStickyChangeNumber : el.offsetHeight; it.offset = scrollElOffset + p.stickyBitStickyOffset; it.stickyStart = isBottom ? stickyStart - it.offset : 0; diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 00000000..f408cac8 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1 @@ +{"compilerOptions":{"target":"es6","experimentalDecorators":true},"exclude":["node_modules","bower_components","tmp","vendor",".git","dist"]} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index b2703a5f..a5a29d35 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "stickybits", - "version": "3.3.0", + "version": "3.3.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -9283,9 +9283,9 @@ } }, "rollup": { - "version": "0.58.0", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-0.58.0.tgz", - "integrity": "sha512-xu49WFv3R9CkXpRHxKH19PV7dV493XYkhQgIsGJfh7DGqKOp//NKoGjiCSK3reIFTX6PRGzXzQ2nPeVyXKr1Jg==", + "version": "0.58.2", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-0.58.2.tgz", + "integrity": "sha512-RZVvCWm9BHOYloaE6LLiE/ibpjv1CmI8F8k0B0Cp+q1eezo3cswszJH1DN0djgzSlo0hjuuCmyeI+1XOYLl4wg==", "dev": true, "requires": { "@types/estree": "0.0.38", diff --git a/package.json b/package.json index 11691ea8..2da45583 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stickybits", - "version": "3.3.1", + "version": "3.3.2", "description": "Stickybits is a lightweight alternative to `position: sticky` polyfills", "main": "dist/stickybits.js", "module": "dist/stickybits.es.js", diff --git a/tests/unit/test.stickybits.js b/tests/unit/test.stickybits.js index 47c5eb25..603746e2 100644 --- a/tests/unit/test.stickybits.js +++ b/tests/unit/test.stickybits.js @@ -112,6 +112,16 @@ test('stickybits .getClosestParent interface', () => { expect(parent.id).toBe('parent') }) +test('stickybits .getOffsetTop interface', () => { + // Set up our document + document.body.innerHTML = '
' + const child = document.getElementById('child') + const parentEl = document.getElementById('parent') + const stickybit = stickybits('#manage-sticky') + const parentOffsetTop = stickybit.getOffsetTop(parentEl) + expect(parentOffsetTop).toBe(0) +}) + test('stickybits .computeScrollOffsets interface', () => { // Set up our document document.body.innerHTML = '
'