diff --git a/README.md b/README.md index 5a5c2f2..0f7ccfc 100644 --- a/README.md +++ b/README.md @@ -23,18 +23,51 @@ vue-affix, 但是其必须指定相对元素, 而不可以默认跟随window, 所以和我的需求略微不同, 就动手做了一个。

+## 文档和示例 + +[https://dream2023.github.io/easy-affix/](https://dream2023.github.io/easy-affix/) + ## 安装 ```bash npm install easy-affix --save ``` -## 文档和示例 +## 使用 -[https://dream2023.github.io/easy-affix/](https://dream2023.github.io/easy-affix/) +```js +// 局部引入 +import Affix from 'easy-affix' +export default { + components: { + Affix + } +} +``` + +```js +// 全局引入 +import Affix from 'easy-affix' +Vue.component('affix', Affix) +``` ## Props 参数 +```html + +内容.... +``` + +```html + +当距离顶部50px时, 开始绝对定位... +``` + +```html + +内容.... +``` + ```js props: { // 类型(仅能为bottom 和 top) diff --git a/documentation/App.vue b/documentation/App.vue index 06afc29..0304b8f 100644 --- a/documentation/App.vue +++ b/documentation/App.vue @@ -29,6 +29,26 @@

使用

+
+
// 局部引入
+import Affix from 'easy-affix'
+export default {
+  components: {
+    Affix
+  }
+}
+
+
+
// 全局引入
+import Affix from 'easy-affix'
+Vue.component('affix', Affix)
+
+
+
+ +
+

Props参数

+

左侧导航栏使用了最基本的形式:

<affix>导航栏...</affix>
@@ -41,10 +61,6 @@
<affix type="bottom">相对于底部而言</affix>
-
- -
-

Props参数

                 
diff --git a/public/index.html b/public/index.html
index 751189f..3f05ee1 100644
--- a/public/index.html
+++ b/public/index.html
@@ -4,7 +4,7 @@
     
     
     
-    
+    
     
     
     easy-affix
diff --git a/src/Affix.vue b/src/Affix.vue
index a73bda7..30aa2c3 100644
--- a/src/Affix.vue
+++ b/src/Affix.vue
@@ -1,9 +1,12 @@
 
 
@@ -44,9 +47,11 @@ export default {
   },
   data () {
     return {
+      throttled: null,
       isAffix: false,
       scrollDistance: 0,
       winHeight: 0,
+      styles: {},
       elOffsetPageTop: 0
     }
   },
@@ -76,21 +81,6 @@ export default {
       } else {
         return null
       }
-    },
-    // 样式
-    styles () {
-      if (this.isAffix && this.enabled) {
-        const elRect = this.elRect
-        return {
-          position: 'fixed',
-          zIndex: this.zIndex,
-          left: elRect.left + 'px',
-          width: elRect.width + 'px',
-          [this.type]: this.offset + 'px'
-        }
-      } else {
-        return {}
-      }
     }
   },
   mounted () {
@@ -101,20 +91,15 @@ export default {
         this.elOffsetPageTop = this.getElOffsetPageTop()
 
         // 事件节流(因为不需要页面响应式, 所以没有在data中设置)
-        this.scrollFn = throttle(this.delay, () => {
-          this.updateScrollDistance()
-        })
-        this.resizeFn = throttle(this.delay, () => {
-          this.updateWinHeight()
+        this.throttled = throttle(this.delay, () => {
+          this.handleChange()
         })
 
         // 初始化时需要执行一次
-        this.updateScrollDistance()
-        this.updateWinHeight()
+        this.handleChange()
 
         // 开启事件监听
-        window.addEventListener('scroll', this.scrollFn, false)
-        window.addEventListener('resize', this.resizeFn, false)
+        this.startListen()
       })
     }
   },
@@ -122,12 +107,6 @@ export default {
     this.stopListen()
   },
   watch: {
-    scrollDistance () {
-      this.handleChange()
-    },
-    winHeight () {
-      this.handleChange()
-    },
     enabled (value) {
       if (value === false) {
         this.stopListen()
@@ -147,18 +126,11 @@ export default {
       return selfTop + scrollDistance - this.offsetTop - clientTop
     },
 
-    // 更新滚动距离
-    updateScrollDistance () {
-      this.scrollDistance = window.pageYOffset || document.documentElement.scrollTop
-    },
-
-    // 窗口高度
-    updateWinHeight () {
-      this.winHeight = window.innerHeight
-    },
-
     // 检测变化
     handleChange () {
+      this.winHeight = window.innerHeight
+      this.scrollDistance = window.pageYOffset || document.documentElement.scrollTop
+
       const isAffix = this.isAffix
       if (this.type === 'top') {
         if (!isAffix && this.scrollDistance >= this.elOffsetPageTop) {
@@ -179,12 +151,37 @@ export default {
           this.isAffix = false
         }
       }
+
+      // 设置样式
+      this.setStyles()
+    },
+
+    // 设置样式
+    setStyles () {
+      let styles = {}
+      if (this.isAffix && this.enabled) {
+        const elRect = this.elRect
+        styles = {
+          position: 'fixed',
+          zIndex: this.zIndex,
+          left: elRect.left + 'px',
+          width: this.$el.offsetWidth + 'px',
+          [this.type]: this.offset + 'px'
+        }
+      }
+      this.styles = styles
+    },
+
+    // 开启事件监听
+    startListen () {
+      window.addEventListener('scroll', this.throttled, false)
+      window.addEventListener('resize', this.throttled, false)
     },
 
     // 删除事件监听
     stopListen () {
-      window.removeEventListener('scroll', this.scrollFn, false)
-      window.removeEventListener('resize', this.resizeFn, false)
+      window.removeEventListener('scroll', this.throttled, false)
+      window.removeEventListener('resize', this.throttled, false)
     }
   }
 }
diff --git a/src/index.js b/src/index.js
index d53c2d6..51bd09f 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,15 +1,3 @@
 import Affix from './Affix'
 
-const Plugin = {}
-
-Plugin.install = Vue => {
-  if (Plugin.install.installed) return
-
-  Vue.component('affix', Affix)
-}
-
-if (typeof window !== 'undefined' && window.Vue) {
-  Plugin.install(window.Vue)
-}
-
 export default Affix