其他分享
首页 > 其他分享> > vue-事件处理

vue-事件处理

作者:互联网

**

vue-事件处理

**
一般使用v-on来进行事件监听,在监听DOM事件时会触发一部分的js代码。

滚轮事件(不加修饰符passive)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo1</title>
    <script src="../js/vue.js"></script>
    <style>
        .list{
            width: 200px;
            height: 200px;
            background: peru;
            overflow: scroll;

        }
        .item{
            height: 100px;
        }
    </style>
</head>
<body>
    <div id="root">
        <ul class="list" @wheel="showInfo">
            <li class="item"></li>
            <li class="item"></li>
            <li class="item"></li>
            <li class="item"></li>
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false;

        const vm = new Vue({
            el:"#root",
            methods:{
                showInfo(){
                    for (let index = 0; index < 100000; index++) {
                        console.log("#");
                    }
                }
            }
        });        
    </script>
</body>
</html>

向下滚动滚轮触发事件,执行回调showInfo,showInfo执行完后,滚动条才会往下滑动。
滚轮事件(加修饰符passive)

<div id="root">
    <ul class="list" @wheel.passive="showInfo">
        <li class="item"></li>
        <li class="item"></li>
        <li class="item"></li>
        <li class="item"></li>
    </ul>
</div>

@wheel.passive时,滚动滚轮触发事件,滚动条会立即滑动,不用等回调showInfo执行完。
键盘修饰符

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo1</title>
    <script src="../js/vue.js"></script>
    <style>
        .container{
            background-color: skyblue;
            width: 200px;
            height: 100px;
        }
        .content{
            padding-top: 50px;
        }
    </style>
</head>
<body>
<div id="root">
    <div class="container" @click="showInfo">
        <div class="content">
            <a href="http://www.baidu.com" @click.stop.prevent="showInfo">点我提示信息</a>
        </div>
        
    </div>
</div>
    <script>
        Vue.config.productionTip = false;

        const vm = new Vue({
            el:"#root",
            methods:{
                showInfo(){    
                    alert("Hello");
                }
            }
        });        
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo1</title>
    <script src="../js/vue.js"></script>
</head>
<body>
<div id="root">
    <input type="text" @keyup.ctrl.y="showInfo">
</div>
    <script>
        Vue.config.productionTip = false;

        const vm = new Vue({
            el:"#root",
            methods:{
                showInfo(e){    
                    console.log(e.target.value);                 
                }
            }
        });        
    </script>
</body>
</html>

标签:事件处理,vue,keyup,keydown,键盘,事件,按键,回调
来源: https://blog.csdn.net/sharp0016/article/details/122769047