UNPKG

7.9 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3 typeof define === 'function' && define.amd ? define(['exports'], factory) :
4 (global = global || self, factory(global.window = global.window || {}));
5}(this, (function (exports) { 'use strict';
6
7 /*!
8 * ScrollToPlugin 3.10.3
9 * https://greensock.com
10 *
11 * @license Copyright 2008-2022, GreenSock. All rights reserved.
12 * Subject to the terms at https://greensock.com/standard-license or for
13 * Club GreenSock members, the agreement issued with that membership.
14 * @author: Jack Doyle, jack@greensock.com
15 */
16 var gsap,
17 _coreInitted,
18 _window,
19 _docEl,
20 _body,
21 _toArray,
22 _config,
23 _windowExists = function _windowExists() {
24 return typeof window !== "undefined";
25 },
26 _getGSAP = function _getGSAP() {
27 return gsap || _windowExists() && (gsap = window.gsap) && gsap.registerPlugin && gsap;
28 },
29 _isString = function _isString(value) {
30 return typeof value === "string";
31 },
32 _isFunction = function _isFunction(value) {
33 return typeof value === "function";
34 },
35 _max = function _max(element, axis) {
36 var dim = axis === "x" ? "Width" : "Height",
37 scroll = "scroll" + dim,
38 client = "client" + dim;
39 return element === _window || element === _docEl || element === _body ? Math.max(_docEl[scroll], _body[scroll]) - (_window["inner" + dim] || _docEl[client] || _body[client]) : element[scroll] - element["offset" + dim];
40 },
41 _buildGetter = function _buildGetter(e, axis) {
42 var p = "scroll" + (axis === "x" ? "Left" : "Top");
43
44 if (e === _window) {
45 if (e.pageXOffset != null) {
46 p = "page" + axis.toUpperCase() + "Offset";
47 } else {
48 e = _docEl[p] != null ? _docEl : _body;
49 }
50 }
51
52 return function () {
53 return e[p];
54 };
55 },
56 _clean = function _clean(value, index, target, targets) {
57 _isFunction(value) && (value = value(index, target, targets));
58
59 if (typeof value !== "object") {
60 return _isString(value) && value !== "max" && value.charAt(1) !== "=" ? {
61 x: value,
62 y: value
63 } : {
64 y: value
65 };
66 } else if (value.nodeType) {
67 return {
68 y: value,
69 x: value
70 };
71 } else {
72 var result = {},
73 p;
74
75 for (p in value) {
76 result[p] = p !== "onAutoKill" && _isFunction(value[p]) ? value[p](index, target, targets) : value[p];
77 }
78
79 return result;
80 }
81 },
82 _getOffset = function _getOffset(element, container) {
83 element = _toArray(element)[0];
84
85 if (!element || !element.getBoundingClientRect) {
86 return console.warn("scrollTo target doesn't exist. Using 0") || {
87 x: 0,
88 y: 0
89 };
90 }
91
92 var rect = element.getBoundingClientRect(),
93 isRoot = !container || container === _window || container === _body,
94 cRect = isRoot ? {
95 top: _docEl.clientTop - (_window.pageYOffset || _docEl.scrollTop || _body.scrollTop || 0),
96 left: _docEl.clientLeft - (_window.pageXOffset || _docEl.scrollLeft || _body.scrollLeft || 0)
97 } : container.getBoundingClientRect(),
98 offsets = {
99 x: rect.left - cRect.left,
100 y: rect.top - cRect.top
101 };
102
103 if (!isRoot && container) {
104 offsets.x += _buildGetter(container, "x")();
105 offsets.y += _buildGetter(container, "y")();
106 }
107
108 return offsets;
109 },
110 _parseVal = function _parseVal(value, target, axis, currentVal, offset) {
111 return !isNaN(value) && typeof value !== "object" ? parseFloat(value) - offset : _isString(value) && value.charAt(1) === "=" ? parseFloat(value.substr(2)) * (value.charAt(0) === "-" ? -1 : 1) + currentVal - offset : value === "max" ? _max(target, axis) - offset : Math.min(_max(target, axis), _getOffset(value, target)[axis] - offset);
112 },
113 _initCore = function _initCore() {
114 gsap = _getGSAP();
115
116 if (_windowExists() && gsap && document.body) {
117 _window = window;
118 _body = document.body;
119 _docEl = document.documentElement;
120 _toArray = gsap.utils.toArray;
121 gsap.config({
122 autoKillThreshold: 7
123 });
124 _config = gsap.config();
125 _coreInitted = 1;
126 }
127 };
128
129 var ScrollToPlugin = {
130 version: "3.10.3",
131 name: "scrollTo",
132 rawVars: 1,
133 register: function register(core) {
134 gsap = core;
135
136 _initCore();
137 },
138 init: function init(target, value, tween, index, targets) {
139 _coreInitted || _initCore();
140 var data = this,
141 snapType = gsap.getProperty(target, "scrollSnapType");
142 data.isWin = target === _window;
143 data.target = target;
144 data.tween = tween;
145 value = _clean(value, index, target, targets);
146 data.vars = value;
147 data.autoKill = !!value.autoKill;
148 data.getX = _buildGetter(target, "x");
149 data.getY = _buildGetter(target, "y");
150 data.x = data.xPrev = data.getX();
151 data.y = data.yPrev = data.getY();
152
153 if (snapType && snapType !== "none") {
154 data.snap = 1;
155 data.snapInline = target.style.scrollSnapType;
156 target.style.scrollSnapType = "none";
157 }
158
159 if (value.x != null) {
160 data.add(data, "x", data.x, _parseVal(value.x, target, "x", data.x, value.offsetX || 0), index, targets);
161
162 data._props.push("scrollTo_x");
163 } else {
164 data.skipX = 1;
165 }
166
167 if (value.y != null) {
168 data.add(data, "y", data.y, _parseVal(value.y, target, "y", data.y, value.offsetY || 0), index, targets);
169
170 data._props.push("scrollTo_y");
171 } else {
172 data.skipY = 1;
173 }
174 },
175 render: function render(ratio, data) {
176 var pt = data._pt,
177 target = data.target,
178 tween = data.tween,
179 autoKill = data.autoKill,
180 xPrev = data.xPrev,
181 yPrev = data.yPrev,
182 isWin = data.isWin,
183 snap = data.snap,
184 snapInline = data.snapInline,
185 x,
186 y,
187 yDif,
188 xDif,
189 threshold;
190
191 while (pt) {
192 pt.r(ratio, pt.d);
193 pt = pt._next;
194 }
195
196 x = isWin || !data.skipX ? data.getX() : xPrev;
197 y = isWin || !data.skipY ? data.getY() : yPrev;
198 yDif = y - yPrev;
199 xDif = x - xPrev;
200 threshold = _config.autoKillThreshold;
201
202 if (data.x < 0) {
203 data.x = 0;
204 }
205
206 if (data.y < 0) {
207 data.y = 0;
208 }
209
210 if (autoKill) {
211 if (!data.skipX && (xDif > threshold || xDif < -threshold) && x < _max(target, "x")) {
212 data.skipX = 1;
213 }
214
215 if (!data.skipY && (yDif > threshold || yDif < -threshold) && y < _max(target, "y")) {
216 data.skipY = 1;
217 }
218
219 if (data.skipX && data.skipY) {
220 tween.kill();
221 data.vars.onAutoKill && data.vars.onAutoKill.apply(tween, data.vars.onAutoKillParams || []);
222 }
223 }
224
225 if (isWin) {
226 _window.scrollTo(!data.skipX ? data.x : x, !data.skipY ? data.y : y);
227 } else {
228 data.skipY || (target.scrollTop = data.y);
229 data.skipX || (target.scrollLeft = data.x);
230 }
231
232 if (snap && (ratio === 1 || ratio === 0)) {
233 y = target.scrollTop;
234 x = target.scrollLeft;
235 snapInline ? target.style.scrollSnapType = snapInline : target.style.removeProperty("scroll-snap-type");
236 target.scrollTop = y + 1;
237 target.scrollLeft = x + 1;
238 target.scrollTop = y;
239 target.scrollLeft = x;
240 }
241
242 data.xPrev = data.x;
243 data.yPrev = data.y;
244 },
245 kill: function kill(property) {
246 var both = property === "scrollTo";
247
248 if (both || property === "scrollTo_x") {
249 this.skipX = 1;
250 }
251
252 if (both || property === "scrollTo_y") {
253 this.skipY = 1;
254 }
255 }
256 };
257 ScrollToPlugin.max = _max;
258 ScrollToPlugin.getOffset = _getOffset;
259 ScrollToPlugin.buildGetter = _buildGetter;
260 _getGSAP() && gsap.registerPlugin(ScrollToPlugin);
261
262 exports.ScrollToPlugin = ScrollToPlugin;
263 exports.default = ScrollToPlugin;
264
265 Object.defineProperty(exports, '__esModule', { value: true });
266
267})));