UNPKG

@talentsoft-opensource/uxp-themes

Version:

Talentsoft UX themes

37 lines (36 loc) 1.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Sticky = /** @class */ (function () { function Sticky(elementIsLooked, options) { this.elementTarget = elementIsLooked; this.className = options.className; this.elementAddClass = options.elementAddClass; this.stuck = false; this.stickPoint = this.getDistance(this.elementTarget); this.handleScroll(); } Sticky.prototype.getDistance = function ($element) { return $element.offsetTop; }; /** * Init Scroll Event */ Sticky.prototype.handleScroll = function () { var _this = this; window.addEventListener('scroll', function (event) { var distance = _this.getDistance(_this.elementTarget) - window.pageYOffset; var offset = window.pageYOffset; var $el = _this.elementAddClass ? _this.elementAddClass : _this.elementTarget; if ((distance <= 0) && !_this.stuck) { $el.classList.add(_this.className); _this.stuck = true; } else if (_this.stuck && (offset <= _this.stickPoint)) { $el.classList.remove(_this.className); _this.stuck = false; } }); }; return Sticky; }()); exports.default = Sticky;