vue指令应用--记事本

x33g5p2x  于2021-09-24 转载在 Vue.js  
字(8.4k)|赞(0)|评价(0)|浏览(622)

一、应用介绍

该应用实现以下功能:
1、在候选框写下要做的内容按下回车可在列表中显示。
2、点击每个事项右边的x可以删除事项。
3、点击右下角clear可以清空所有事项。
4、左下角显示剩余事项的条数。
5、当记事本中无事项时,隐藏clear和剩余事项条数。

二、功能分析

1、功能一可以使用v-model关联同步候选框内容,使用@keyup.enter绑定事件使内容在记事本中显示。
2、生成删除事项可以用v-for动态生成列表结构,里面信息可以用数组存储。
3、无事项时,隐藏功能可以使用v-show或者v-if,这里建议使用v-show。

三、代码及效果

HTML,js部分

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link rel="stylesheet" href="记事本.css">
  9. </head>
  10. <body>
  11. <!-- 主体区域 -->
  12. <section id="todoapp">
  13. <!-- 输入框 -->
  14. <header class="header">
  15. <h1>todo记事本</h1>
  16. <input v-model="inputValue" @keyup.enter="add" autofocus="autofocus" autocomplete="off" placeholder="请输入任务" class="new-todo">
  17. </header>
  18. <!-- 列表区域 -->
  19. <section class="main">
  20. <ul class="todo-list">
  21. <li class="todo" v-for="(item,index) in list">
  22. <div class="view">
  23. <span class="index">{{ index+1 }}.</span>
  24. <label>{{ item }}</label>
  25. <button class="destroy" @click="remove(index)"></button>
  26. </div>
  27. </li>
  28. </ul>
  29. </section>
  30. <!-- 统计和清空 -->
  31. <footer class="footer" v-show="list.length!=0">
  32. <span class="todo-count" v-if="list.length!=0">
  33. <strong>{{ list.length }}</strong> items left
  34. </span>
  35. <button v-show="list.length!=0" class="clear-completed" @click="clear">
  36. Clear
  37. </button>
  38. </footer>
  39. </section>
  40. <!-- 底部 -->
  41. <!-- <footer class="info">
  42. <p>
  43. <a href="http://www.itheima.com/"><img src="./img/black.png" alt="" /></a>
  44. </p>
  45. </footer> -->
  46. </section>
  47. <!-- 开发环境版本,包含了有帮助的命令行警告 -->
  48. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  49. <script>
  50. var app = new Vue({
  51. el: "#todoapp",
  52. data: {
  53. list: ["吃饭", "睡觉", "打豆豆"],
  54. inputValue: "写代码",
  55. },
  56. methods: {
  57. add: function() {
  58. if (this.inputValue != '') {
  59. this.list.push(this.inputValue);
  60. }
  61. },
  62. remove: function(index) {
  63. this.list.splice(index, 1);
  64. },
  65. clear: function() {
  66. this.list = [];
  67. }
  68. }
  69. })
  70. </script>
  71. </body>
  72. </html>

css部分

  1. html,
  2. body {
  3. margin: 0;
  4. padding: 0;
  5. }
  6. body {
  7. background: #fff;
  8. }
  9. button {
  10. margin: 0;
  11. padding: 0;
  12. border: 0;
  13. background: none;
  14. font-size: 100%;
  15. vertical-align: baseline;
  16. font-family: inherit;
  17. font-weight: inherit;
  18. color: inherit;
  19. -webkit-appearance: none;
  20. appearance: none;
  21. -webkit-font-smoothing: antialiased;
  22. -moz-osx-font-smoothing: grayscale;
  23. }
  24. body {
  25. font: 14px "Helvetica Neue", Helvetica, Arial, sans-serif;
  26. line-height: 1.4em;
  27. background: #f5f5f5;
  28. color: #4d4d4d;
  29. min-width: 230px;
  30. max-width: 550px;
  31. margin: 0 auto;
  32. -webkit-font-smoothing: antialiased;
  33. -moz-osx-font-smoothing: grayscale;
  34. font-weight: 300;
  35. }
  36. :focus {
  37. outline: 0;
  38. }
  39. .hidden {
  40. display: none;
  41. }
  42. #todoapp {
  43. background: #fff;
  44. margin: 180px 0 40px 0;
  45. position: relative;
  46. box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);
  47. }
  48. #todoapp input::-webkit-input-placeholder {
  49. font-style: italic;
  50. font-weight: 300;
  51. color: #e6e6e6;
  52. }
  53. #todoapp input::-moz-placeholder {
  54. font-style: italic;
  55. font-weight: 300;
  56. color: #e6e6e6;
  57. }
  58. #todoapp input::input-placeholder {
  59. font-style: italic;
  60. font-weight: 300;
  61. color: gray;
  62. }
  63. #todoapp h1 {
  64. position: absolute;
  65. top: -160px;
  66. width: 100%;
  67. font-size: 60px;
  68. font-weight: 100;
  69. text-align: center;
  70. color: rgba(175, 47, 47, .8);
  71. -webkit-text-rendering: optimizeLegibility;
  72. -moz-text-rendering: optimizeLegibility;
  73. text-rendering: optimizeLegibility;
  74. }
  75. .new-todo,
  76. .edit {
  77. position: relative;
  78. margin: 0;
  79. width: 100%;
  80. font-size: 24px;
  81. font-family: inherit;
  82. font-weight: inherit;
  83. line-height: 1.4em;
  84. border: 0;
  85. color: inherit;
  86. padding: 6px;
  87. border: 1px solid #999;
  88. box-shadow: inset 0 -1px 5px 0 rgba(0, 0, 0, 0.2);
  89. box-sizing: border-box;
  90. -webkit-font-smoothing: antialiased;
  91. -moz-osx-font-smoothing: grayscale;
  92. }
  93. .new-todo {
  94. padding: 16px;
  95. border: none;
  96. background: rgba(0, 0, 0, 0.003);
  97. box-shadow: inset 0 -2px 1px rgba(0, 0, 0, 0.03);
  98. }
  99. .main {
  100. position: relative;
  101. z-index: 2;
  102. border-top: 1px solid #e6e6e6;
  103. }
  104. .toggle-all {
  105. width: 1px;
  106. height: 1px;
  107. border: none;
  108. /* Mobile Safari */
  109. opacity: 0;
  110. position: absolute;
  111. right: 100%;
  112. bottom: 100%;
  113. }
  114. .toggle-all+label {
  115. width: 60px;
  116. height: 34px;
  117. font-size: 0;
  118. position: absolute;
  119. top: -52px;
  120. left: -13px;
  121. -webkit-transform: rotate(90deg);
  122. transform: rotate(90deg);
  123. }
  124. .toggle-all+label:before {
  125. content: "❯";
  126. font-size: 22px;
  127. color: #e6e6e6;
  128. padding: 10px 27px 10px 27px;
  129. }
  130. .toggle-all:checked+label:before {
  131. color: #737373;
  132. }
  133. .todo-list {
  134. margin: 0;
  135. padding: 0;
  136. list-style: none;
  137. max-height: 420px;
  138. overflow: auto;
  139. }
  140. .todo-list li {
  141. position: relative;
  142. font-size: 24px;
  143. border-bottom: 1px solid #ededed;
  144. height: 60px;
  145. box-sizing: border-box;
  146. }
  147. .todo-list li:last-child {
  148. border-bottom: none;
  149. }
  150. .todo-list .view .index {
  151. position: absolute;
  152. color: gray;
  153. left: 10px;
  154. top: 20px;
  155. font-size: 16px;
  156. }
  157. .todo-list li .toggle {
  158. text-align: center;
  159. width: 40px;
  160. /* auto, since non-WebKit browsers doesn't support input styling */
  161. height: auto;
  162. position: absolute;
  163. top: 0;
  164. bottom: 0;
  165. margin: auto 0;
  166. border: none;
  167. /* Mobile Safari */
  168. -webkit-appearance: none;
  169. appearance: none;
  170. }
  171. .todo-list li .toggle {
  172. opacity: 0;
  173. }
  174. .todo-list li .toggle+label {
  175. /* Firefox requires `#` to be escaped - https://bugzilla.mozilla.org/show_bug.cgi?id=922433 IE and Edge requires *everything* to be escaped to render, so we do that instead of just the `#` - https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/7157459/ */
  176. background-image: url("data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23ededed%22%20stroke-width%3D%223%22/%3E%3C/svg%3E");
  177. background-repeat: no-repeat;
  178. background-position: center left;
  179. }
  180. .todo-list li .toggle:checked+label {
  181. background-image: url("data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23bddad5%22%20stroke-width%3D%223%22/%3E%3Cpath%20fill%3D%22%235dc2af%22%20d%3D%22M72%2025L42%2071%2027%2056l-4%204%2020%2020%2034-52z%22/%3E%3C/svg%3E");
  182. }
  183. .todo-list li label {
  184. word-break: break-all;
  185. padding: 15px 15px 15px 60px;
  186. display: block;
  187. line-height: 1.2;
  188. transition: color 0.4s;
  189. }
  190. .todo-list li.completed label {
  191. color: #d9d9d9;
  192. text-decoration: line-through;
  193. }
  194. .todo-list li .destroy {
  195. display: none;
  196. position: absolute;
  197. top: 0;
  198. right: 10px;
  199. bottom: 0;
  200. width: 40px;
  201. height: 40px;
  202. margin: auto 0;
  203. font-size: 30px;
  204. color: #cc9a9a;
  205. margin-bottom: 11px;
  206. transition: color 0.2s ease-out;
  207. }
  208. .todo-list li .destroy:hover {
  209. color: #af5b5e;
  210. }
  211. .todo-list li .destroy:after {
  212. content: "×";
  213. }
  214. .todo-list li:hover .destroy {
  215. display: block;
  216. }
  217. .todo-list li .edit {
  218. display: none;
  219. }
  220. .todo-list li.editing:last-child {
  221. margin-bottom: -1px;
  222. }
  223. .footer {
  224. color: #777;
  225. padding: 10px 15px;
  226. height: 20px;
  227. text-align: center;
  228. border-top: 1px solid #e6e6e6;
  229. }
  230. .footer:before {
  231. content: "";
  232. position: absolute;
  233. right: 0;
  234. bottom: 0;
  235. left: 0;
  236. height: 50px;
  237. overflow: hidden;
  238. 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);
  239. }
  240. .todo-count {
  241. float: left;
  242. text-align: left;
  243. }
  244. .todo-count strong {
  245. font-weight: 300;
  246. }
  247. .filters {
  248. margin: 0;
  249. padding: 0;
  250. list-style: none;
  251. position: absolute;
  252. right: 0;
  253. left: 0;
  254. }
  255. .filters li {
  256. display: inline;
  257. }
  258. .filters li a {
  259. color: inherit;
  260. margin: 3px;
  261. padding: 3px 7px;
  262. text-decoration: none;
  263. border: 1px solid transparent;
  264. border-radius: 3px;
  265. }
  266. .filters li a:hover {
  267. border-color: rgba(175, 47, 47, 0.1);
  268. }
  269. .filters li a.selected {
  270. border-color: rgba(175, 47, 47, 0.2);
  271. }
  272. .clear-completed,
  273. html .clear-completed:active {
  274. float: right;
  275. position: relative;
  276. line-height: 20px;
  277. text-decoration: none;
  278. cursor: pointer;
  279. }
  280. .clear-completed:hover {
  281. text-decoration: underline;
  282. }
  283. .info {
  284. margin: 50px auto 0;
  285. color: #bfbfbf;
  286. font-size: 15px;
  287. text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  288. text-align: center;
  289. }
  290. .info p {
  291. line-height: 1;
  292. }
  293. .info a {
  294. color: inherit;
  295. text-decoration: none;
  296. font-weight: 400;
  297. }
  298. .info a:hover {
  299. text-decoration: underline;
  300. }
  301. /* Hack to remove background from Mobile Safari. Can't use it globally since it destroys checkboxes in Firefox */
  302. @media screen and (-webkit-min-device-pixel-ratio: 0) {
  303. .toggle-all,
  304. .todo-list li .toggle {
  305. background: none;
  306. }
  307. .todo-list li .toggle {
  308. height: 40px;
  309. }
  310. }
  311. @media (max-width: 430px) {
  312. .footer {
  313. height: 50px;
  314. }
  315. .filters {
  316. bottom: 10px;
  317. }
  318. }

效果图

相关文章

最新文章

更多