vue设置input与按钮在一行_vue+css:制作todo事项(待办事项)的代码解析

本文通过实例代码解析如何在Vue中将input输入框和按钮设置在同一行,涉及Vue基础、CSS样式及HTML布局。文章重点讨论了v-show、v-if指令的区别以及v-for循环的使用,适合Vue初学者学习。
摘要由CSDN通过智能技术生成

1 效果图:

87f3cb595f29cbeedceafbe4cd9744c7.gif

2 环境:

======

2.1 谷歌浏览器:

强调这一点,是因为css设置中,并没有对浏览器兼容问题进行代码设置,主要是突出重点。

2.2 微软编辑器vscode:

用于代码编写。

3 知识点:

===代码注释中有说明===

3.1 html基础知识。

3.2 css基础知识。

3.3 vue基础知识。

===开始===

4 html基础框架:

          vue-note       ......             ......            ......      

5 两个文件:

3bcb2ec58008d79e8bd9aeda4874133e.png

6 vue-note.html代码:

          vue-note

vue note txt

{{index+1}}. {{item}}
{{this.list.length}} items left Clear

7 index.css代码:

/*----第1步:整体设置----*//*这是设置默认的内外边距。因为浏览器标签自带的属性是不统一的,设置为0,为了兼容所有的浏览器。*/html,body {  margin: 0;  padding: 0;}body {  /*字体大小和样式*/  font: 14px Arial;  line-height: 1.4em;  /*网页整体的背景颜色*/  background: #0695ce;  /*字体的颜色,注意h1的标题字体颜色是单独设置的*/  /*color: #4d4d4d;*/  /*body大小设置,其实就是表格todo框的大小*/  min-width: 230px;  max-width: 550px;  margin: 0 auto;}/*按钮设置:包括×按钮和clear按钮设置*/button {  margin: 0;  padding: 0;  border: 0;  background: none;  font-size: 100%;  vertical-align: baseline;  font-family: inherit;  font-weight: inherit;  color: inherit;  -webkit-appearance: none;  appearance: none;}/*----第2步:的设置,也是重点----*/#todoapp {  /*todoapp的box的背景颜色*/  background: #fff;  margin: 180px 0 40px 0;  position: relative;  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);}/*---第2-1步:头部分的文字标题名的设置*/#todoapp h1 {  position: absolute;  top: -160px;  width: 100%;  font-size: 60px;  font-weight: 100;  text-align: center;  /*文字颜色设置*/  color: rgba(175, 47, 47, .8);}/*----第2-2步:第一行输入框的设置---*//*输入框的placeholder文本设置*/#todoapp input::input-placeholder {  font-style: italic;  font-weight: 300;  color: gray;}/*输入框的边界框线设置*/:focus {  outline: 0;}/*第一行输入框设置,尤其是处于编辑状态下的*/.new-todo,.edit {  position: relative;  margin: 0;  width: 100%;  font-size: 24px;  font-family: inherit;  font-weight: inherit;  line-height: 1.4em;  border: 0;  color: inherit;  padding: 6px;  border: 1px solid #999;  box-shadow: inset 0 -1px 5px 0 rgba(0, 0, 0, 0.2);  box-sizing: border-box;}/*第一行输入框的新的输入信息设置*/.new-todo {  padding: 16px;  border: none;  background: rgba(0, 0, 0, 0.003);  box-shadow: inset 0 -2px 1px rgba(0, 0, 0, 0.03);}/*----第2-3步:列表区域的设置---*/.main {  position: relative;  z-index: 2;  border-top: 1px solid #e6e6e6;}.todo-list {  margin: 0;  padding: 0;  list-style: none;  max-height: 420px;  overflow: auto;}.todo-list li {  position: relative;  font-size: 24px;  border-bottom: 1px solid #ededed;  height: 60px;  box-sizing: border-box;}.todo-list .view .index {  position: absolute;  color: gray;  left: 10px;  top: 20px;  font-size: 16px;}.todo-list li label {  word-break: break-all;  padding: 15px 15px 15px 60px;  display: block;  line-height: 1.2;  transition: color 0.4s;}.todo-list li.completed label {  color: #d9d9d9;  text-decoration: line-through;}.todo-list li .destroy {  display: none;  position: absolute;  top: 0;  right: 10px;  bottom: 0;  width: 40px;  height: 40px;  margin: auto 0;  font-size: 30px;  /*×→叉的飘过所在行的颜色*/  color: chartreuse;  margin-bottom: 11px;  transition: color 0.2s ease-out;}.todo-list li .destroy:hover {  /*×→叉的选中颜色*/  color: #f12113;}.todo-list li .destroy:after {  /*×→叉的符号*/  content: "×";}/*列表的每一行右边的×,当鼠标划过时的可变设置*/.todo-list li:hover .destroy {  display: block;}.todo-list li .edit {  display: none;}.todo-list li.editing:last-child {  margin-bottom: -1px;}/*----第2-4步:注脚区域的设置---*/.footer {  color: #777;  padding: 10px 15px;  height: 20px;  text-align: center;  border-top: 1px solid #e6e6e6;}.footer:before {  content: "";  position: absolute;  right: 0;  bottom: 0;  left: 0;  height: 50px;  overflow: hidden;  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2), 0 8px 0 -3px #f6f6f6,    0 9px 1px -3px rgba(0, 0, 0, 0.2), 0 16px 0 -6px #f6f6f6,    0 17px 2px -6px rgba(0, 0, 0, 0.2);}/*注脚在左下的设置*/.todo-count {  float: left;  text-align: left;}.todo-count strong {  font-weight: 300;}.clear-completed,html .clear-completed:active {  float: right;  position: relative;  line-height: 20px;  text-decoration: none;  cursor: pointer;}/*clear的下划线设置*/.clear-completed:hover {  text-decoration: underline;}

8 主要是熟悉vue的单文件结构布局,指令:v-show和v-if区别,v-for复习;vue函数定义等。

0928c02750c4037f9b0e6921db8bfb27.png

==自己整理并分享出来===

喜欢的人,请点赞、关注、评论、转发和收藏。

2.删除待办事项 3.标记已完成待办事项 4.编辑待办事项 首先,需要安装Vue、ElementUI和Vuex。 ``` npm install vue npm install element-ui npm install vuex ``` 在main.js中引入Vue、ElementUI和Vuex: ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import Vuex from 'vuex' import App from './App.vue' Vue.use(ElementUI) Vue.use(Vuex) Vue.config.productionTip = false new Vue({ render: h => h(App), }).$mount('#app') ``` 接下来,创建store.js文件,定义state、mutations、actions和getters: ```javascript import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { todos: [ { id: 1, text: '写代码', done: false }, { id: 2, text: '看电影', done: true }, { id: 3, text: '做饭', done: false } ] }, mutations: { addTodo (state, todo) { state.todos.push(todo) }, deleteTodo (state, id) { state.todos = state.todos.filter(todo => todo.id !== id) }, toggleTodo (state, id) { state.todos = state.todos.map(todo => { if (todo.id === id) { todo.done = !todo.done } return todo }) }, editTodo (state, todo) { state.todos = state.todos.map(item => (item.id === todo.id ? todo : item)) } }, actions: { addTodo ({ commit }, todo) { commit('addTodo', todo) }, deleteTodo ({ commit }, id) { commit('deleteTodo', id) }, toggleTodo ({ commit }, id) { commit('toggleTodo', id) }, editTodo ({ commit }, todo) { commit('editTodo', todo) } }, getters: { todos: state => state.todos, doneTodos: state => state.todos.filter(todo => todo.done), activeTodos: state => state.todos.filter(todo => !todo.done) } }) ``` 在App.vue中,使用ElementUI的组件实现待办事项页面: ```vue <template> <div class="container"> <h1 class="title">Todo List</h1> <div class="add-todo"> <el-input v-model="newTodo" placeholder="请输入待办事项"></el-input> <el-button type="primary" @click="addTodo">添加</el-button> </div> <el-divider></el-divider> <el-card v-for="todo in activeTodos" :key="todo.id" class="todo-card"> <div class="todo-content"> <el-checkbox v-model="todo.done" @change="toggleTodo" class="todo-checkbox">{{ todo.text }}</el-checkbox> <el-button-group> <el-button type="text" @click="editTodo">编辑</el-button> <el-button type="text" @click="deleteTodo">删除</el-button> </el-button-group> </div> </el-card> <el-divider></el-divider> <h2 class="subtitle">已完成</h2> <el-card v-for="todo in doneTodos" :key="todo.id" class="todo-card"> <div class="todo-content"> <el-checkbox v-model="todo.done" @change="toggleTodo" class="todo-checkbox">{{ todo.text }}</el-checkbox> <el-button-group> <el-button type="text" @click="editTodo">编辑</el-button> <el-button type="text" @click="deleteTodo">删除</el-button> </el-button-group> </div> </el-card> <el-dialog :visible.sync="editVisible" title="编辑待办事项"> <el-input v-model="editTodoText" placeholder="请输入待办事项"></el-input> <div slot="footer"> <el-button @click="editVisible = false">取消</el-button> <el-button type="primary" @click="saveEdit">保存</el-button> </div> </el-dialog> </div> </template> <script> import { mapGetters, mapActions } from 'vuex' export default { name: 'App', computed: { ...mapGetters(['activeTodos', 'doneTodos']) }, data () { return { newTodo: '', editTodoId: null, editTodoText: '', editVisible: false } }, methods: { ...mapActions(['addTodo', 'deleteTodo', 'toggleTodo', 'editTodo']), addTodo () { if (this.newTodo.trim()) { this.addTodo({ id: Date.now(), text: this.newTodo, done: false }) this.newTodo = '' } }, deleteTodo (todo) { this.deleteTodo(todo.id) }, toggleTodo (todo) { this.toggleTodo(todo.id) }, editTodo (todo) { this.editTodoId = todo.id this.editTodoText = todo.text this.editVisible = true }, saveEdit () { if (this.editTodoText.trim()) { this.editTodo({ id: this.editTodoId, text: this.editTodoText, done: false }) this.editVisible = false } } } } </script> <style> .container { max-width: 800px; margin: 0 auto; padding: 20px; } .title { text-align: center; margin-bottom: 20px; } .add-todo { display: flex; align-items: center; margin-bottom: 20px; } .todo-card { margin-bottom: 10px; } .todo-content { display: flex; align-items: center; justify-content: space-between; } .todo-checkbox { flex: 1; margin-right: 10px; } .subtitle { margin-top: 20px; margin-bottom: 10px; } </style> ``` 最后,在index.html中添加id为app的div: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Todo List</title> </head> <body> <div id="app"></div> <script src="./dist/js/app.js"></script> </body> </html> ``` 这样,就完成了一个基于Vue、ElementUI和Vuex的待办事项页面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值