vue学习笔记(10)——事件修饰符

先捕获,后冒泡

捕获阶段:由外往内

冒泡阶段:由内往外

scroll:滑动条或上下键滑动

whell滚轮滑动,只要滚轮滑动,就触发事件

事件修饰符:

  1. prevent:阻止默认事件(常用)

  1. stop:阻止事件冒泡(常用)多次弹窗

  1. once:事件只触发一次(常用)

  1. capture:使用事件的捕获方式

  1. self:只有event.target是当前操作的元素时才触发事件

    扫描二维码关注公众号,回复: 15517750 查看本文章
  1. passive:事件的默认行为立即执行,无需等待事件回调执行完毕

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>事件的基本使用</title>
    <!-- 引入Vue -->
    <script type="text/javascript" src="../js/vue.js"></script>
    <style>
        *{
            margin-top: 20px;
        }
        .demo1{
            height:50x;
            background-color:skyblue;
        }
        .box1{
            padding:20px;
            background-color:skyblue;
        }
        .box2{
            padding:20px;
            background-color:rgb(235, 135, 142);
        }
        .list{
            width:200px;
            height: 200px;
            background-color: aqua;
            overflow: auto;
        }
        li{
           
            height: 100px;
        }
    </style>
</head>
<body>
<!-- 准备一个容器 -->
<div id="root">
    <h2>欢迎来到{
    
    {name}}学习</h2>

    <!-- prevent:阻止默认事件(常用) -->
    <a href="http://www.atguigu.com" @click.prevent="showinfo">点我提示信息</a>
  
    <!-- stop:阻止事件冒泡(常用)多次弹窗 -->
    <div class="demo1" @click="showinfo">
        <button @click.stop="showinfo">点我提示信息</button>
    </div>
   
    <!-- once:事件只触发一次(常用)
    点击事件,第一次触发弹窗,以后再点击不触发弹窗 -->
    <button @click.once="showinfo">点我提示信息</button>
    
    <!-- capture:使用事件的捕获方式 -->
    <div class="box1" @click.capture="showmes(1)">
        div1
        <div class="box2" @click="showmes(2)">
            div2
        </div>
    </div>
    <!-- self:只有event.target是当前操作的元素时才触发事件 -->
    <div class="demo1" @click.self="showinfo">
        <button @click="showinfo">点我提示信息</button>
    </div>
    <!-- passive:事件的默认行为立即执行,无需等待事件回调执行完毕 -->
    <ul @scroll="demo" class="list">
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
      
    </ul>
</div>
 
<script type="text/javascript">
Vue.config.productionTip=false
 
new Vue({
    el:'#root',
    data:{
        name:'尚硅谷'
    },
    methods:{
        showinfo(){
            alert('aihh')
        },
        showmes(mes){
            console.log(mes)
        },
        demo(){
         for(let i=0;i<100000;i++)
         {
            console.log('#')
         }
         console.log('累坏了')
        }
    }
  
})
 
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_54763080/article/details/128830314
今日推荐