集成自定义指令

vui 内部集成一些指令以供使用

1. v-click-outside


判断是否点击了元素之外的元素


测试是否点击了外部

<template>
    <div>
        <Alert v-click-outside="clickOutside">
            <p @click="clickInside" class="click-outside-paragraph">{{text}}</p>
        </Alert>
    </div>
</template>
<script>
export default {
    data() {
        return {
            text: '测试是否点击了外部',
        };
    },
    methods: {
        clickOutside() {
            this.text = '你点击了外部';
        },
        clickInside() {
            this.text = '你点击了内部';
        },
    },
};
</script>
显示代码

2. v-resize


窗口尺寸改变指令, 支持传入 function 或 option, 当绑定元素卸载时,页面 resize 事件也会随之清除

option 参数 说明 类型 可选值 默认值
value function 执行函数 function -- --
debounce 函数触发防抖时间,单位 ms number -- 200
quiet 是否挂载时执行一遍 function boolean false true

Window Size: width: 0px height: 0px

<template>
    <div>
        <Alert v-resize="resizeObj">
            <p>Window Size: width: {{width}}px height: {{height}}px</p>
        </Alert>
    </div>
</template>
<script>
export default {
    data() {
        return {
            width: 0,
            height: 0,
            resizeObj: {
                value: this.resizeFunc,
                debounce: 500,
                quiet: false,
            },
        };
    },
    methods: {
        resizeFunc() {
            this.width = window.innerWidth;
            this.height = window.innerHeight;
        },
    },
};
</script>
显示代码

3. v-mouse-wheel


元素滚轮滚动指令, 当绑定元素卸载时,滚轮事件也会随之清除


指令绑定值为 function, 其携带两个参数,eventnormalizeWheel(pixelX, pixelY, spinX,spinY), 可根据 normalizeWheel 判断滚动的方向数值等

Window content

Window content

Window content

Window content

Window content

<template>
    <div>
        <Alert v-mouse-wheel="mouseWheelFunc" style="height: 200px; overflow: auto">
            <p>Window content</p>
            <p>Window content</p>
            <p>Window content</p>
            <p>Window content</p>
            <p>Window content</p>
        </Alert>
    </div>
</template>
<script>
export default {
    methods: {
        mouseWheelFunc(event, normalizeWheel) {
            console.log(normalizeWheel);
            console.log(event);
        },
    },
};
</script>
显示代码

4. v-long-press


长按触发指令,v-long-press 使用即可,默认设置长按时间为 1s


5. v-lazy


图片懒加载自定义指令, v-lazy 使用即可。默认的占位图是小皮咖的头像,如果你想自定义修改,修改Vue.$vuiLazyLoad.img即可实现

Vue.$vuiLazyLoad.img = '你的占位图url';
<img
    v-lazy="'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'"
/>