|
1 | | -(function() { |
2 | | - const vueSticky = {}; |
3 | | - let listenAction; |
4 | | - vueSticky.install = Vue => { |
5 | | - Vue.directive('sticky', { |
6 | | - inserted(el, binding) { |
7 | | - const params = binding.value || {}, |
8 | | - stickyTop = params.stickyTop || 0, |
9 | | - zIndex = params.zIndex || 1000, |
10 | | - elStyle = el.style; |
| 1 | +const vueSticky = {}; |
| 2 | +let listenAction; |
| 3 | +vueSticky.install = Vue => { |
| 4 | + Vue.directive('sticky', { |
| 5 | + inserted(el, binding) { |
| 6 | + const params = binding.value || {}, |
| 7 | + stickyTop = params.stickyTop || 0, |
| 8 | + zIndex = params.zIndex || 1000, |
| 9 | + elStyle = el.style; |
11 | 10 |
|
12 | | - elStyle.position = '-webkit-sticky'; |
13 | | - elStyle.position = 'sticky'; |
| 11 | + elStyle.position = '-webkit-sticky'; |
| 12 | + elStyle.position = 'sticky'; |
14 | 13 | // if the browser support css sticky(Currently Safari, Firefox and Chrome Canary) |
15 | 14 | // if (~elStyle.position.indexOf('sticky')) { |
16 | 15 | // elStyle.top = `${stickyTop}px`; |
17 | 16 | // elStyle.zIndex = zIndex; |
18 | 17 | // return |
19 | 18 | // } |
20 | | - const elHeight = el.getBoundingClientRect().height; |
21 | | - const elWidth = el.getBoundingClientRect().width; |
22 | | - elStyle.cssText = `top: ${stickyTop}px; z-index: ${zIndex}`; |
| 19 | + const elHeight = el.getBoundingClientRect().height; |
| 20 | + const elWidth = el.getBoundingClientRect().width; |
| 21 | + elStyle.cssText = `top: ${stickyTop}px; z-index: ${zIndex}`; |
23 | 22 |
|
24 | | - const parentElm = el.parentNode || document.documentElement; |
25 | | - const placeholder = document.createElement('div'); |
26 | | - placeholder.style.display = 'none'; |
27 | | - placeholder.style.width = `${elWidth}px`; |
28 | | - placeholder.style.height = `${elHeight}px`; |
29 | | - parentElm.insertBefore(placeholder, el) |
| 23 | + const parentElm = el.parentNode || document.documentElement; |
| 24 | + const placeholder = document.createElement('div'); |
| 25 | + placeholder.style.display = 'none'; |
| 26 | + placeholder.style.width = `${elWidth}px`; |
| 27 | + placeholder.style.height = `${elHeight}px`; |
| 28 | + parentElm.insertBefore(placeholder, el) |
30 | 29 |
|
31 | | - let active = false; |
| 30 | + let active = false; |
32 | 31 |
|
33 | | - const getScroll = (target, top) => { |
34 | | - const prop = top ? 'pageYOffset' : 'pageXOffset'; |
35 | | - const method = top ? 'scrollTop' : 'scrollLeft'; |
36 | | - let ret = target[prop]; |
37 | | - if (typeof ret !== 'number') { |
38 | | - ret = window.document.documentElement[method]; |
39 | | - } |
40 | | - return ret; |
41 | | - }; |
| 32 | + const getScroll = (target, top) => { |
| 33 | + const prop = top ? 'pageYOffset' : 'pageXOffset'; |
| 34 | + const method = top ? 'scrollTop' : 'scrollLeft'; |
| 35 | + let ret = target[prop]; |
| 36 | + if (typeof ret !== 'number') { |
| 37 | + ret = window.document.documentElement[method]; |
| 38 | + } |
| 39 | + return ret; |
| 40 | + }; |
42 | 41 |
|
43 | | - const sticky = () => { |
44 | | - if (active) { |
45 | | - return |
46 | | - } |
47 | | - if (!elStyle.height) { |
48 | | - elStyle.height = `${el.offsetHeight}px` |
49 | | - } |
| 42 | + const sticky = () => { |
| 43 | + if (active) { |
| 44 | + return |
| 45 | + } |
| 46 | + if (!elStyle.height) { |
| 47 | + elStyle.height = `${el.offsetHeight}px` |
| 48 | + } |
50 | 49 |
|
51 | | - elStyle.position = 'fixed'; |
52 | | - elStyle.width = `${elWidth}px`; |
53 | | - placeholder.style.display = 'inline-block'; |
54 | | - active = true |
55 | | - }; |
| 50 | + elStyle.position = 'fixed'; |
| 51 | + elStyle.width = `${elWidth}px`; |
| 52 | + placeholder.style.display = 'inline-block'; |
| 53 | + active = true |
| 54 | + }; |
56 | 55 |
|
57 | | - const reset = () => { |
58 | | - if (!active) { |
59 | | - return |
60 | | - } |
| 56 | + const reset = () => { |
| 57 | + if (!active) { |
| 58 | + return |
| 59 | + } |
61 | 60 |
|
62 | | - elStyle.position = ''; |
63 | | - placeholder.style.display = 'none'; |
64 | | - active = false; |
65 | | - }; |
| 61 | + elStyle.position = ''; |
| 62 | + placeholder.style.display = 'none'; |
| 63 | + active = false; |
| 64 | + }; |
66 | 65 |
|
67 | | - const check = () => { |
68 | | - const scrollTop = getScroll(window, true); |
69 | | - const offsetTop = el.getBoundingClientRect().top; |
70 | | - if (offsetTop < stickyTop) { |
71 | | - sticky(); |
72 | | - } else { |
73 | | - if (scrollTop < elHeight + stickyTop) { |
74 | | - reset() |
75 | | - } |
| 66 | + const check = () => { |
| 67 | + const scrollTop = getScroll(window, true); |
| 68 | + const offsetTop = el.getBoundingClientRect().top; |
| 69 | + if (offsetTop < stickyTop) { |
| 70 | + sticky(); |
| 71 | + } else { |
| 72 | + if (scrollTop < elHeight + stickyTop) { |
| 73 | + reset() |
76 | 74 | } |
77 | | - }; |
78 | | - listenAction = () => { |
79 | | - check() |
80 | | - }; |
| 75 | + } |
| 76 | + }; |
| 77 | + listenAction = () => { |
| 78 | + check() |
| 79 | + }; |
| 80 | + |
| 81 | + window.addEventListener('scroll', listenAction) |
| 82 | + }, |
81 | 83 |
|
82 | | - window.addEventListener('scroll', listenAction) |
83 | | - }, |
| 84 | + unbind() { |
| 85 | + window.removeEventListener('scroll', listenAction) |
| 86 | + } |
| 87 | + }) |
| 88 | +}; |
84 | 89 |
|
85 | | - unbind() { |
86 | | - window.removeEventListener('scroll', listenAction) |
87 | | - } |
88 | | - }) |
89 | | - }; |
90 | | - if (typeof exports == 'object') { |
91 | | - module.exports = vueSticky |
92 | | - } else if (typeof define == 'function' && define.amd) { |
93 | | - define([], () => vueSticky) |
94 | | - } else if (window.Vue) { |
95 | | - window.vueSticky = vueSticky; |
96 | | - Vue.use(vueSticky) |
97 | | - } |
98 | | -}()); |
| 90 | +export default vueSticky |
99 | 91 |
|
0 commit comments