Commit 87c343e461a3c9b9dcba94dd23ef95b69f75d002

Authored by 崔琼雪
1 parent 52830ca6

增加逻辑:

1.当吸顶的时候增加一个div标签占位,可让过度更加顺滑(原有逻辑当到达顶部的时候会会变成绝对定位,后面的标签就会立即顶上去,会有跳动的过程,体验不佳)
examples/routers/affix.vue
... ... @@ -10,12 +10,24 @@
10 10 }
11 11 </style>
12 12 <template>
  13 +<div>
13 14 <Affix>
14   - <span class="demo-affix">固定在最顶部</span>
  15 + <div class="demo-affix">固定在最顶部</div>
15 16 </Affix>
  17 + <div v-for="(item,index) in arr">{{item}}</div>
  18 +</div>
16 19 </template>
17 20 <script>
18 21 export default {
19   -
  22 + data(){
  23 + return {
  24 + arr: []
  25 + }
  26 + },
  27 + created(){
  28 + for(let i = 0 ; i < 100 ; i++){
  29 + this.arr.push(i);
  30 + }
  31 + }
20 32 }
21 33 </script>
... ...
src/components/affix/affix.vue
1 1 <template>
2 2 <div>
3   - <div :class="classes" :style="styles">
  3 + <div ref="point" :class="classes" :style="styles">
4 4 <slot></slot>
5 5 </div>
  6 + <div v-show="slot" :style="slotStyle"></div>
6 7 </div>
7 8 </template>
8 9 <script>
... ... @@ -52,7 +53,9 @@
52 53 data () {
53 54 return {
54 55 affix: false,
55   - styles: {}
  56 + styles: {},
  57 + slot: false,
  58 + slotStyle: {}
56 59 };
57 60 },
58 61 computed: {
... ... @@ -95,6 +98,11 @@
95 98 // Fixed Top
96 99 if ((elOffset.top - this.offsetTop) < scrollTop && this.offsetType == 'top' && !affix) {
97 100 this.affix = true;
  101 + this.slotStyle = {
  102 + width: this.$refs.point.clientWidth + 'px',
  103 + height: this.$refs.point.clientHeight + 'px'
  104 + }
  105 + this.slot = true;
98 106 this.styles = {
99 107 top: `${this.offsetTop}px`,
100 108 left: `${elOffset.left}px`,
... ... @@ -103,6 +111,8 @@
103 111  
104 112 this.$emit('on-change', true);
105 113 } else if ((elOffset.top - this.offsetTop) > scrollTop && this.offsetType == 'top' && affix) {
  114 + this.slot = false;
  115 + this.slotStyle = {};
106 116 this.affix = false;
107 117 this.styles = null;
108 118  
... ...