Skip to content

Commit

Permalink
Merge pull request #3 from kiccer/dev
Browse files Browse the repository at this point in the history
update
  • Loading branch information
kiccer committed Oct 24, 2019
2 parents b406b59 + 13b46b8 commit f3319ad
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 16 deletions.
53 changes: 50 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
npm install --save vue-directive-extend
```

### Using
### Usage
> In `main.js` files.
```javascript
// main.js
import Vue from 'vue'
import App from './App.vue'
import vueDirectiveExtend from 'vue-directive-extend'
Expand All @@ -24,7 +25,8 @@ new Vue({
}).$mount('#app')
```

In `.vue` files.
> In `.vue` files.
```vue
<template lang="html">
<div class="demoVue">
Expand All @@ -50,3 +52,48 @@ export default {
</style>
```

### Event listener list

<escape>
<table>
<tr>
<th>Listener</th>
<th>description</th>
<th>Modifiers</th>
<th>Modifiers description</th>
</tr>
<tr>
<td rowspan="2"><code>v-resize</code></td>
<td rowspan="2">Use the element-resize-detector plug-in to listener element resize events.</td>
<td><code>.lazy</code></td>
<td>Lazy mode. Triggering resize too often will only work for the last time.</td>
</tr>
<tr>
<td><code>.100</code></td>
<td>After <code>.lazy</code>, set maximum time difference. If not set, then default is 100.</td>
</tr>
<tr>
<td rowspan="2"><code>v-mousewheel</code></td>
<td rowspan="2">Use the jquery-mousewheel plug-in to listener mouse wheel events.</td>
<td><code>.stop</code></td>
<td>Same as <code>e.stopPropagation()</code></td>
</tr>
<tr>
<td><code>.prevent</code></td>
<td>Same as <code>e.preventDefault()</code></td>
</tr>
<tr>
<td><code>v-load</code></td>
<td>Triggered when the element is inserted into the DOM.</td>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td><code>v-unload</code></td>
<td>Triggered when the element is removed from the DOM.</td>
<td>-</td>
<td>-</td>
</tr>
</table>
</escape>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-directive-extend",
"version": "0.1.4",
"version": "0.1.5",
"description": "This project is used to extend more event listener for Vue.",
"main": "./public/js/vue-directive-extend.js",
"keywords": [
Expand Down
2 changes: 2 additions & 0 deletions public/js/vue-directive-extend.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import vueDirectiveResize from './vue-directive-resize'
import vueDirectiveMousewheel from './vue-directive-mousewheel'
import vueDirectiveLoad from './vue-directive-load'

function vueDirectiveExtend (vm) {
vm.use(vueDirectiveResize)
vm.use(vueDirectiveMousewheel)
vm.use(vueDirectiveLoad)
}

export default vueDirectiveExtend
22 changes: 22 additions & 0 deletions public/js/vue-directive-load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function vueDirectiveLoad (vm) {
vm.directive('load', {
// bind (el, binding) {},
inserted (el, binding) {
binding.value && binding.value()
}
// componentUpdated (el) {},
// update (el) {},
// unbind (el, binding) {}
})
vm.directive('unload', {
// bind (el, binding) {},
// inserted (el, binding) {},
// componentUpdated (el) {},
// update (el) {},
unbind (el, binding) {
binding.value && binding.value()
}
})
}

export default vueDirectiveLoad
19 changes: 12 additions & 7 deletions public/js/vue-directive-mousewheel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ function vueDirectiveMousewheel (vm) {
vm.directive('mousewheel', {
// bind (el, binding) {},
inserted (el, binding) {
// el.addEventListener('mousewheel', binding.value)
$(el).on('mousewheel', binding.value)
$(el).off('mousewheel')
$(el).on('mousewheel', e => {
let { modifiers, value } = binding
value && value(e)
modifiers.stop && e.stopPropagation()
modifiers.prevent && e.preventDefault()
if (modifiers.stop && modifiers.prevent) return false
})
},
// componentUpdated (el) {},
// update (el) {},
unbind (el, binding) {
// el.removeEventListener('mousewheel', binding.value)
$(el).off('mousewheel', binding.value)
// componentUpdated (el, binding) {},
// update (el, binding) {},
unbind (el) {
$(el).off('mousewheel')
}
})
}
Expand Down
24 changes: 22 additions & 2 deletions public/js/vue-directive-resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,35 @@ function vueDirectiveResize (vm) {
strategy: 'scroll'
})

let lazyHandle = function (binding) {
let nums = Object.keys(binding.modifiers).map(n => +n).filter(n => !isNaN(n))
clearTimeout(binding.lazyTimer)
binding.lazyTimer = setTimeout(() => {
binding.value && binding.value()
}, nums.length ? Math.max(...nums) : 100)
}

let resizeHandle = function (binding) {
if (binding.modifiers.lazy) {
lazyHandle(binding)
} else {
binding.value && binding.value()
}
}

vm.directive('resize', {
// bind (el, binding) {},
inserted (el, binding) {
erd.listenTo(el, binding.value)
erd.listenTo(el, () => {
resizeHandle(binding)
})
},
// componentUpdated (el) {},
// update (el) {},
unbind (el, binding) {
erd.removeAllListeners(el, binding.value)
erd.removeAllListeners(el, () => {
resizeHandle(binding)
})
}
})
}
Expand Down
1 change: 1 addition & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ export default {
text-align: center;
color: #2c3e50;
margin-top: 60px;
height: 1000px;
}
</style>
16 changes: 13 additions & 3 deletions src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="hello" v-resize="onResize" v-mousewheel="onMousewheel">
<h1>{{ msg }}</h1>
<div class="hello" v-resize.lazy="onResize" v-mousewheel.prevent="onMousewheel" v-load="onLoad" v-unload="onUnload">
<h1 v-mousewheel.stop="onMousewheel">{{ msg }}</h1>
<p>
For a guide and recipes on how to configure / customize this project,<br>
check out the
Expand Down Expand Up @@ -38,8 +38,14 @@ export default {
msg: String
},
methods: {
onLoad () {
console.log('onLoad')
},
onUnload () {
console.log('onUnload')
},
onResize () {
console.log(1111)
console.log('onResize')
},
onMousewheel (e) {
console.log(e)
Expand All @@ -50,6 +56,10 @@ export default {

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.hello {
height: 200px;
overflow: auto;
}
h3 {
margin: 40px 0 0;
}
Expand Down

0 comments on commit f3319ad

Please sign in to comment.