Commit 2919aa362d14fef5f16c10064261ea3b0af441b4
1 parent
e4767e5a
update Affix & BackTop
Showing
3 changed files
with
55 additions
and
9 deletions
Show diff stats
src/components/affix/affix.vue
... | ... | @@ -5,8 +5,8 @@ |
5 | 5 | </div> |
6 | 6 | </div> |
7 | 7 | </template> |
8 | - | |
9 | 8 | <script> |
9 | + import { on, off } from '../../utils/dom'; | |
10 | 10 | const prefixCls = 'ivu-affix'; |
11 | 11 | |
12 | 12 | function getScroll(target, top) { |
... | ... | @@ -73,12 +73,16 @@ |
73 | 73 | } |
74 | 74 | }, |
75 | 75 | mounted () { |
76 | - window.addEventListener('scroll', this.handleScroll, false); | |
77 | - window.addEventListener('resize', this.handleScroll, false); | |
76 | +// window.addEventListener('scroll', this.handleScroll, false); | |
77 | +// window.addEventListener('resize', this.handleScroll, false); | |
78 | + on(window, 'scroll', this.handleScroll); | |
79 | + on(window, 'resize', this.handleScroll); | |
78 | 80 | }, |
79 | 81 | beforeDestroy () { |
80 | - window.removeEventListener('scroll', this.handleScroll, false); | |
81 | - window.removeEventListener('resize', this.handleScroll, false); | |
82 | +// window.removeEventListener('scroll', this.handleScroll, false); | |
83 | +// window.removeEventListener('resize', this.handleScroll, false); | |
84 | + off(window, 'scroll', this.handleScroll); | |
85 | + off(window, 'resize', this.handleScroll); | |
82 | 86 | }, |
83 | 87 | methods: { |
84 | 88 | handleScroll () { | ... | ... |
src/components/back-top/back-top.vue
... | ... | @@ -9,6 +9,7 @@ |
9 | 9 | </template> |
10 | 10 | <script> |
11 | 11 | import { scrollTop } from '../../utils/assist'; |
12 | + import { on, off } from '../../utils/dom'; | |
12 | 13 | const prefixCls = 'ivu-back-top'; |
13 | 14 | |
14 | 15 | export default { |
... | ... | @@ -36,12 +37,16 @@ |
36 | 37 | }; |
37 | 38 | }, |
38 | 39 | mounted () { |
39 | - window.addEventListener('scroll', this.handleScroll, false); | |
40 | - window.addEventListener('resize', this.handleScroll, false); | |
40 | +// window.addEventListener('scroll', this.handleScroll, false); | |
41 | +// window.addEventListener('resize', this.handleScroll, false); | |
42 | + on(window, 'scroll', this.handleScroll); | |
43 | + on(window, 'resize', this.handleScroll); | |
41 | 44 | }, |
42 | 45 | beforeDestroy () { |
43 | - window.removeEventListener('scroll', this.handleScroll, false); | |
44 | - window.removeEventListener('resize', this.handleScroll, false); | |
46 | +// window.removeEventListener('scroll', this.handleScroll, false); | |
47 | +// window.removeEventListener('resize', this.handleScroll, false); | |
48 | + off(window, 'scroll', this.handleScroll); | |
49 | + off(window, 'resize', this.handleScroll); | |
45 | 50 | }, |
46 | 51 | computed: { |
47 | 52 | classes () { | ... | ... |
1 | +import Vue from 'vue'; | |
2 | + | |
3 | +const isServer = Vue.prototype.$isServer; | |
4 | + | |
5 | +/* istanbul ignore next */ | |
6 | +export const on = (function() { | |
7 | + if (!isServer && document.addEventListener) { | |
8 | + return function(element, event, handler) { | |
9 | + if (element && event && handler) { | |
10 | + element.addEventListener(event, handler, false); | |
11 | + } | |
12 | + }; | |
13 | + } else { | |
14 | + return function(element, event, handler) { | |
15 | + if (element && event && handler) { | |
16 | + element.attachEvent('on' + event, handler); | |
17 | + } | |
18 | + }; | |
19 | + } | |
20 | +})(); | |
21 | + | |
22 | +/* istanbul ignore next */ | |
23 | +export const off = (function() { | |
24 | + if (!isServer && document.removeEventListener) { | |
25 | + return function(element, event, handler) { | |
26 | + if (element && event) { | |
27 | + element.removeEventListener(event, handler, false); | |
28 | + } | |
29 | + }; | |
30 | + } else { | |
31 | + return function(element, event, handler) { | |
32 | + if (element && event) { | |
33 | + element.detachEvent('on' + event, handler); | |
34 | + } | |
35 | + }; | |
36 | + } | |
37 | +})(); | |
0 | 38 | \ No newline at end of file | ... | ... |