Vue侦听器的使用

Vue侦听器小练习——进度条

  Vue中的侦听器可以侦听data中数据的变化,并做出操作。下面是使用Vue的侦听器写的一个进度条的小demo。每完成一件事,进度条都会自动更新。
  当我们需要提示用户填写信息的完整度时,就可以使用侦听器,判断用户填写了多少信息,然后除以总信息条数,就可以提示用户了。
Vue侦听器的使用_第1张图片


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Documenttitle>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            width: 100vw;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(to right, #22c1c3, #fdbb2d);
        }
        #app{
            width: 460px;
            padding: 10px;
            border: 1px solid white;
            display: flex;
            flex-direction: column;
            align-items: center;
            color: #414141;
        }
        .container{
            width: 100%;
            height: 20px;
            margin-top: 15px;
            font-size: 14px;
            display: flex;
        }
        .container > p{
            width: 60px;
            height: 20px;
            line-height: 20px;
            text-align: center;
            margin-right: 10px;
            border: 1px solid white;
            color: white;
        }
        .rate{
            width: calc(100% - 70px);
            height: 100%;
            border: 1px solid white;
        }
        .rate > div{
            width: 0%;
            height: 100%;
            background-color: rgba(255, 255, 255, 0.2);
            transition-property: "width";
            transition-duration: 0.5s;
            transition-timing-function: linear;
        }
        ul{
            width: 100%;
            margin-top: 15px;
            list-style: none;
        }
        li{
            width: 100%;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 15px;
            font-size: 14px;
        }
        li > p{
            width: calc(100% - 70px);
            height: 30px;
            padding: 0 5px;
            line-height: 30px;
            background-color: rgba(255, 255, 255, 0.2);
        }
        li > div{
            width: 50px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            background-color: rgba(255, 255, 255, 0.2);
        }
        li > div:hover{
            cursor: pointer;
        }
        [v-cloak]{
            display: none;
        }
    style>
head>
<body>
    <div id="app">
        <p>Vue侦听器p>
        <div class="container">
            <p v-cloak>{{ info }}p>
            <div class="rate">
                <div ref="rate">div>
            div>
        div>
        <ul>
            <li>
                <p>Thing 1p>
                <div @click.once="done">Donediv>
                
            li>
            <li>
                <p>Thing 2p>
                <div @click.once="done">Donediv>
            li>
            <li>
                <p>Thing 3p>
                <div @click.once="done">Donediv>
            li>
            <li>
                <p>Thing 4p>
                <div @click.once="done">Donediv>
            li>
        ul>
    div>
body>
<script src="https://cdn.bootcss.com/vue/2.6.5/vue.js">script>
<script>
    const vm = new Vue({
        el: "#app",
        data: {
            num: 0,
            info: "0%"
        },
        // 侦听器
        watch: {
            // 侦听data中的num
            // 当num发生变化时,下面的函数就会执行
            num:function(){
                // console.log("侦听到了num的变化");
                this.info = this.num * 25 + "%";
                this.$refs.rate.style.width = Number(this.$refs.rate.style.width.slice(0, -1)) + 25 + "%";
            }
        },
        methods: {
            done:function(event){
                // event.currentTarget为当前点击的DOM元素
                event.currentTarget.innerHTML = "✔";
                if(this.num < 4){
                    this.num++;
                }else{
                    alert("已完成!");
                }
            }
        },
    });
script>
html>

你可能感兴趣的:(Vue)