Blame view

src/components/affix/affix.vue 4.69 KB
7fa943eb   梁灏   init
1
2
  <template>
      <div>
87c343e4   崔琼雪   增加逻辑:
3
          <div ref="point" :class="classes" :style="styles">
7fa943eb   梁灏   init
4
5
              <slot></slot>
          </div>
87c343e4   崔琼雪   增加逻辑:
6
          <div v-show="slot" :style="slotStyle"></div>
7fa943eb   梁灏   init
7
8
      </div>
  </template>
7fa943eb   梁灏   init
9
  <script>
2919aa36   梁灏   update Affix & Ba...
10
      import { on, off } from '../../utils/dom';
7fa943eb   梁灏   init
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
      const prefixCls = 'ivu-affix';
  
      function getScroll(target, top) {
          const prop = top ? 'pageYOffset' : 'pageXOffset';
          const method = top ? 'scrollTop' : 'scrollLeft';
  
          let ret = target[prop];
  
          if (typeof ret !== 'number') {
              ret = window.document.documentElement[method];
          }
  
          return ret;
      }
  
      function getOffset(element) {
          const rect = element.getBoundingClientRect();
  
          const scrollTop = getScroll(window, true);
          const scrollLeft = getScroll(window);
  
          const docEl = window.document.body;
          const clientTop = docEl.clientTop || 0;
          const clientLeft = docEl.clientLeft || 0;
  
          return {
              top: rect.top + scrollTop - clientTop,
              left: rect.left + scrollLeft - clientLeft
b0893113   jingsam   :art: add eslint
39
          };
7fa943eb   梁灏   init
40
41
42
      }
  
      export default {
06322514   梁灏   support Radio
43
          name: 'Affix',
7fa943eb   梁灏   init
44
45
46
47
48
49
50
51
52
53
54
55
          props: {
              offsetTop: {
                  type: Number,
                  default: 0
              },
              offsetBottom: {
                  type: Number
              }
          },
          data () {
              return {
                  affix: false,
87c343e4   崔琼雪   增加逻辑:
56
57
58
                  styles: {},
                  slot: false,
                  slotStyle: {}
b0893113   jingsam   :art: add eslint
59
              };
7fa943eb   梁灏   init
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
          },
          computed: {
              offsetType () {
                  let type = 'top';
                  if (this.offsetBottom >= 0) {
                      type = 'bottom';
                  }
  
                  return type;
              },
              classes () {
                  return [
                      {
                          [`${prefixCls}`]: this.affix
                      }
b0893113   jingsam   :art: add eslint
75
                  ];
7fa943eb   梁灏   init
76
77
              }
          },
fcf37f49   梁灏   update webpack & ...
78
          mounted () {
2919aa36   梁灏   update Affix & Ba...
79
80
81
82
  //            window.addEventListener('scroll', this.handleScroll, false);
  //            window.addEventListener('resize', this.handleScroll, false);
              on(window, 'scroll', this.handleScroll);
              on(window, 'resize', this.handleScroll);
07ad4da4   msidolphin   fix dose't work p...
83
84
85
              this.$nextTick(() => {
                  this.handleScroll();
              })
7fa943eb   梁灏   init
86
87
          },
          beforeDestroy () {
2919aa36   梁灏   update Affix & Ba...
88
89
90
91
  //            window.removeEventListener('scroll', this.handleScroll, false);
  //            window.removeEventListener('resize', this.handleScroll, false);
              off(window, 'scroll', this.handleScroll);
              off(window, 'resize', this.handleScroll);
7fa943eb   梁灏   init
92
93
94
95
96
97
98
99
100
101
102
103
          },
          methods: {
              handleScroll () {
                  const affix = this.affix;
                  const scrollTop = getScroll(window, true);
                  const elOffset = getOffset(this.$el);
                  const windowHeight = window.innerHeight;
                  const elHeight = this.$el.getElementsByTagName('div')[0].offsetHeight;
  
                  // Fixed Top
                  if ((elOffset.top - this.offsetTop) < scrollTop && this.offsetType == 'top' && !affix) {
                      this.affix = true;
87c343e4   崔琼雪   增加逻辑:
104
105
106
                      this.slotStyle = {
                          width: this.$refs.point.clientWidth + 'px',
                          height: this.$refs.point.clientHeight + 'px'
8a0cb3ce   梁灏   update
107
                      };
87c343e4   崔琼雪   增加逻辑:
108
                      this.slot = true;
7fa943eb   梁灏   init
109
110
111
112
113
114
115
116
                      this.styles = {
                          top: `${this.offsetTop}px`,
                          left: `${elOffset.left}px`,
                          width: `${this.$el.offsetWidth}px`
                      };
  
                      this.$emit('on-change', true);
                  } else if ((elOffset.top - this.offsetTop) > scrollTop && this.offsetType == 'top' && affix) {
87c343e4   崔琼雪   增加逻辑:
117
118
                      this.slot = false;
                      this.slotStyle = {};
7fa943eb   梁灏   init
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
                      this.affix = false;
                      this.styles = null;
  
                      this.$emit('on-change', false);
                  }
  
                  // Fixed Bottom
                  if ((elOffset.top + this.offsetBottom + elHeight) > (scrollTop + windowHeight) && this.offsetType == 'bottom' && !affix) {
                      this.affix = true;
                      this.styles = {
                          bottom: `${this.offsetBottom}px`,
                          left: `${elOffset.left}px`,
                          width: `${this.$el.offsetWidth}px`
                      };
  
                      this.$emit('on-change', true);
                  } else if ((elOffset.top + this.offsetBottom + elHeight) < (scrollTop + windowHeight) && this.offsetType == 'bottom' && affix) {
                      this.affix = false;
                      this.styles = null;
b0893113   jingsam   :art: add eslint
138
  
7fa943eb   梁灏   init
139
140
141
142
                      this.$emit('on-change', false);
                  }
              }
          }
b0893113   jingsam   :art: add eslint
143
      };
7fa943eb   梁灏   init
144
  </script>