|
7 | 7 | Use 'data-module="sticky-element-container"' to instantiate, and add
|
8 | 8 | `[data-sticky-element]` to the child you want to position.
|
9 | 9 | */
|
10 |
| -(function (Modules, root) { |
| 10 | +(function (Modules) { |
11 | 11 | 'use strict'
|
12 | 12 |
|
13 | 13 | Modules.StickyElementContainer = function () {
|
14 | 14 | var self = this
|
15 | 15 |
|
16 | 16 | self.getWindowDimensions = function () {
|
17 | 17 | return {
|
18 |
| - height: root.innerHeight, |
19 |
| - width: root.innerWidth |
| 18 | + height: window.innerHeight, |
| 19 | + width: window.innerWidth |
20 | 20 | }
|
21 | 21 | }
|
22 | 22 |
|
23 | 23 | self.getWindowPositions = function () {
|
24 | 24 | return {
|
25 |
| - scrollTop: root.scrollY |
| 25 | + scrollTop: window.scrollY |
26 | 26 | }
|
27 | 27 | }
|
28 | 28 |
|
29 | 29 | self.start = function ($el) {
|
30 |
| - var el = $el[0] |
31 |
| - var element = el.querySelector('[data-sticky-element]') |
| 30 | + var wrapper = $el[0] |
| 31 | + var stickyElement = wrapper.querySelector('[data-sticky-element]') |
32 | 32 |
|
33 | 33 | var hasResized = true
|
34 | 34 | var hasScrolled = true
|
|
39 | 39 | initialise()
|
40 | 40 |
|
41 | 41 | function initialise () {
|
42 |
| - root.onresize = onResize |
43 |
| - root.onscroll = onScroll |
| 42 | + window.onresize = onResize |
| 43 | + window.onscroll = onScroll |
44 | 44 | setInterval(checkResize, interval)
|
45 | 45 | setInterval(checkScroll, interval)
|
46 | 46 | checkResize()
|
47 | 47 | checkScroll()
|
48 |
| - element.classList.add('sticky-element--enabled') |
| 48 | + stickyElement.classList.add('sticky-element--enabled') |
49 | 49 | }
|
50 | 50 |
|
51 | 51 | function onResize () {
|
|
62 | 62 | hasScrolled = true
|
63 | 63 |
|
64 | 64 | var windowDimensions = self.getWindowDimensions()
|
65 |
| - var elementHeight = el.offsetHeight || parseFloat(el.style.height.replace('px', '')) |
66 |
| - startPosition = el.offsetTop |
67 |
| - stopPosition = el.offsetTop + elementHeight - windowDimensions.height |
| 65 | + var elementHeight = wrapper.offsetHeight || parseFloat(wrapper.style.height.replace('px', '')) |
| 66 | + startPosition = wrapper.offsetTop |
| 67 | + stopPosition = wrapper.offsetTop + elementHeight - windowDimensions.height |
68 | 68 | }
|
69 | 69 | }
|
70 | 70 |
|
|
98 | 98 | }
|
99 | 99 |
|
100 | 100 | function stickToWindow () {
|
101 |
| - element.classList.add('sticky-element--stuck-to-window') |
| 101 | + stickyElement.classList.add('sticky-element--stuck-to-window') |
102 | 102 | }
|
103 | 103 |
|
104 | 104 | function stickToParent () {
|
105 |
| - element.classList.remove('sticky-element--stuck-to-window') |
| 105 | + stickyElement.classList.remove('sticky-element--stuck-to-window') |
106 | 106 | }
|
107 | 107 |
|
108 | 108 | function show () {
|
109 |
| - element.classList.remove('sticky-element--hidden') |
| 109 | + stickyElement.classList.remove('sticky-element--hidden') |
110 | 110 | }
|
111 | 111 |
|
112 | 112 | function hide () {
|
113 |
| - element.classList.add('sticky-element--hidden') |
| 113 | + stickyElement.classList.add('sticky-element--hidden') |
114 | 114 | }
|
115 | 115 | }
|
116 | 116 | }
|
117 |
| -})(window.GOVUK.Modules, window) |
| 117 | +})(window.GOVUK.Modules) |
0 commit comments