Vue案例:小笔记本

外观

笔记本分三个部分:

  1. 输入框添加
  2. 事项列表
  3. 选择删除部分

输入框添加

输入框会监听enter键,当点击enter键或者是傍边的加号按钮时,获取输入框的内容,然后调用add方法,把输入框的内容添加到事项列表中,并将show属性设置为false状态,这是来实现事项是否完成,然后将输入框的内容清空。

           <div class="input-box">
                <input type="text" ref="inputValue" @keyup.enter='add'>
                <i @click='add'></i>
            </div>
add() {
            if (this.$refs.inputValue.value === '') return
            this.incident.unshift({
                text: this.$refs.inputValue.value,
                show: false
            })
            this.$refs.inputValue.value = ''
},

输入
添加

事项列表

事项的组成有三部分,一是完成按钮,二是事项内容,三是删除按钮,完成按钮点击后表示事项已经完成,内容会出现一条删除线,再点击一下取消完成,后面的删除按钮可以进行单条事项的删除。

 <ul class="text-box" ref="textbox">
                <li v-for='(item,index) in incident' :key="index">
                    <div>
                        <i @click='finish(index)' :class='{settle:item.show}'></i>
                    </div>
                    <p><s :style='{textDecoration:item.show?"line-through":"none"}'>{{item.text}}</s></p>
                    <i class="deletebutton" @click='deleteli(index)'></i>
                </li>
</ul>
finish(a) {
            // this.incident[a].show = !this.incident[a].show
            this.$set(this.incident[a], 'show', !this.incident[a].show)
},
deleteli(a) {
            if (confirm('确定要删除吗?')) {
                this.incident.splice(a, 1)
            }
},
完成

选择删除部分

选择删除部分,左边有一个统计完成事项,右边是选择删除按钮,统计完成的事项只会是已经点上已完成的才会统计,勾选完成的可以点击删除按钮把完成的事项全部删掉。

         <div class="statistics-box" v-if='incident.length !== 0'>
                <p>完成了{{statistics}}件事</p>
                <button @click='selectdel'>删除</button>
          </div>
       selectdel() {
            if (this.statistics === 0) return alert('还没完成什么事呢')
            if (confirm('确定删除选中的吗?')) {
                this.incident = this.incident.filter((item) => {
                    return item.show !== true
                })
            }
        }
删除选中

以下是全部代码:

<!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>小笔记本</title>
    <link rel="stylesheet" href="./css/index.css">
</head>

<body>
    <div id="app">
        <div class="big-box">
            <!-- 标题 -->
            <h3>{{title}}</h3>
            <!-- 添加输入框,添加按钮 -->
            <div class="input-box">
                <input type="text" ref="inputValue" @keyup.enter='add'>
                <i @click='add'></i>
            </div>
            <!--笔记内容  -->
            <ul class="text-box" ref="textbox">
                <li v-for='(item,index) in incident' :key="index">
                    <div>
                        <i @click='finish(index)' :class='{settle:item.show}'></i>
                    </div>
                    <p><s :style='{textDecoration:item.show?"line-through":"none"}'>{{item.text}}</s></p>
                    <i class="deletebutton" @click='deleteli(index)'></i>
                </li>
            </ul>
            <!--删除、统计按钮  -->
            <div class="statistics-box" v-if='incident.length !== 0'>
                <p>完成了{{statistics}}件事</p>
                <button @click='selectdel'>删除</button>
            </div>
        </div>
    </div>
    <script src="../../js/vue.js"></script>
    <script src="./js/index.js"></script>
</body>

</html>
body {
    margin: 0;
    padding: 0;
    background-image: linear-gradient(90deg, #f093fb 0%, #f5576c 100%);
}

h3,
i,
ul,
li,
p,
input {
    margin: 0;
    padding: 0;
    border: 0;
    list-style: none;
}


.big-box {
    width: 30%;
    background-color: rgb(24, 22, 61);
    border-radius: 10px;
    margin: 100px auto;
    box-shadow: 0 3px 6px rgba(24, 22, 61, 0.3);
    overflow: hidden;
}

.big-box h3 {
    text-align: center;
    font-size: 20px;
    padding: 8px;
    color: white;
}

.input-box {
    height: 38px;
    width: 100%;
    padding: 0 24px;
    box-sizing: border-box;
    margin: 10px 0;
}

.input-box input {
    float: left;
    width: 85%;
    height: 36px;
    border: 0;
    border-bottom: 2px solid #777;
    background-color: transparent;
    color: #999;
    font-size: 20px;
    outline: none;
}

.input-box i {
    float: left;
    display: block;
    width: 15%;
    height: 36px;
    background: url('../images/jia.png') no-repeat;
    background-position: 6px 6px;
    background-size: 30px;
    cursor: pointer;
}

.text-box {
    width: 100%;
    padding-left: 24px;
    box-sizing: border-box;
    max-height: 200px;
    overflow-y: auto;
}

.text-box::-webkit-scrollbar {
    display: none
}

.text-box li {
    width: 80%;
    height: 34px;
    background-color: rgb(43, 41, 83);
    margin: 10px 0;
    border-radius: 34px;
    overflow: hidden;
    display: flex;
    align-items: center;
}

.text-box li div {
    width: 12%;
    height: 100%;
    text-align: center;
    line-height: 38px;
}

.text-box li div i {
    display: inline-block;
    width: 18px;
    height: 18px;
    border-radius: 18px;
    background-color: rgb(108, 105, 150);
    cursor: pointer;

}

.text-box li div .settle {
    background: url('../images/xz.png') no-repeat;
    background-size: 18px;
    background-position: 100%;
}


.text-box li p {
    width: 70%;
    color: #999;
    overflow: hidden;
}

.text-box li .deletebutton {
    display: block;
    width: 18%;
    height: 100%;
    background: url('../images/sc.png') no-repeat;
    background-size: 20px;
    background-position: 50%;
    cursor: pointer;
}

.statistics-box {
    height: 42px;
    width: 100%;
}

.statistics-box p {
    float: left;
    margin-left: 16px;
    color: white;
    margin-top: 10px;
}

.statistics-box button {
    float: right;
    width: 60px;
    height: 28px;
    color: white;
    background-color: rgb(245, 87, 108);
    border: 0;
    border-radius: 5px;
    margin-top: 5px;
    margin-right: 16px;
}
nt.unshift({
                text: this.$refs.inputValue.value,
                show: false
            })
            this.$refs.inputValue.value = ''
        },
        finish(a) {
            // this.incident[a].show = !this.incident[a].show
            this.$set(this.incident[a], 'show', !this.incident[a].show)
        },
        deleteli(a) {
            if (confirm('确定要删除吗?')) {
                this.incident.splice(a, 1)
            }
        },
        selectdel() {
            if (this.statistics === 0) return alert('还没完成什么事呢')
            if (confirm('确定删除选中的吗?')) {
                this.incident = this.incident.filter((item) => {
                    return item.show !== true
                })
            }
        }
    },
    computed: {
        statistics() {
            let a = 0
            this.incident.forEach(element => {
                if (element.show) {
                    a = a + 1
                }
            });
            return a
        }
    }
})

本文章由javascript技术分享原创和收集

发表评论 (审核通过后显示评论):