-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
244 lines (239 loc) · 7.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!DOCTYPE html>
<html>
<head>
<title>TodoList</title>
<meta name="viewport" content="width=device-width,maximum-scale=1,minimum-scale=1,user-scalable=no">
<script src="./assets/js/vue.js"></script>
<link href="assets/css/animate.css" rel="stylesheet" type="text/css">
<style>
html,body,#app{
width: 100%;
height: 100%;
-webkit-box-sizing: border-box;
box-sizing: border-box;
padding: 0;
margin: 0;
overflow: hidden;
}
#app{
padding: 5px;
}
.todo-done{
color: #ccc;
text-decoration: line-through;
}
#addBtn{
width: 100%;
height: 32px;
font-size: 18px;
font-family: sans-serif;
margin: 10px 0px;
border: 3px solid #ccc;
border-radius: 8px;
-webkit-box-sizing: border-box;
box-sizing: border-box;
outline: none;
}
#addBtn:active{
border: 3px solid #0f0;
}
#addBtn:focus{
border: 3px solid #a2e563;
}
ul{
list-style: none;
padding: 0;
}
ul>li{
width: 100%;
border: none;
border-bottom: 1px solid #ccc;
padding: 8px 0px;
}
ul>li>span{
color: #ccc;
font-size: 12px;
}
#todolistBox{
overflow-x: hidden;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
}
.list-complete-item{
transition: all 1s;
display: inline-block;
}
.list-todo-item{
transition: all 1s;
/*display: inline-block;*/
}
.list-todo-enter, .list-todo-leave-active{
opacity: 0;
transform: translate3d( 100%, 0px, 0px);
}
.list-todo-leave-active{
position: absolute;
}
</style>
</head>
<body>
<section id="app">
{{ message }}
<div v-hover="vm" id="countLoadLocalStorage">
第{{ countLoadLocalStorage }}次进入页面
</div>
<input type="text" id="addBtn" placeholder="增加代办事项" v-on:keyup.enter="addTodo">
<div id="todolistBox">
<transition-group name="list-todo" tag="ul">
<li v-for="( item, index ) in todoList" v-on:click="makeTodoDone( item, index, $event )" class="list-todo-item" v-bind:key="item">
<span>{{ item.createdAt }}创建</span> {{ item.name }}
</li>
</transition-group>
<transition-group name="listComplete" enter-active-class="animated-enter fadeInRight" tag="ul">
<li v-for="item in completeList" class="todo-done list-complete-item" v-bind:key="item" >
<span>{{ item.finishedAt }}完成</span> {{ item.name }}
</li>
</transition-group>
</div>
</section>
<script>
// 注册全局的hover指令
Vue.directive('hover', function( el, binding ){
var timer = null;
el.onmouseover = function(){
var startTime = new Date();
var i = 0;
timer = setInterval( function(){
i++;
if( i === 3 ){
clearInterval( timer );
var r = confirm('你想删除我咯?')
if( r === true ){
localStorage.setItem( 'countLoadLocalStorage' , 0 );
binding.value.countLoadLocalStorage = 0;
}
}
}, 1000);
};
el.onmouseout = function(){
clearInterval( timer );
}
});
var app = new Vue({
el:'#app',
data: {
vm: null,
message: 'love wang yuan',
countLoadLocalStorage: 0,
todoList: [],
completeList:[],
newTodo: null
},
created: function () {
/* body... */
this.vm = this;
// 计算进入页面的次数
this.countingLoad();
this.initList();
this.initListBox();
},
methods: {
// 统计进入页面的次数
countingLoad: function(){
var count = parseInt(localStorage.getItem( 'countLoadLocalStorage' ));
if ( count ){
count += 1;
this.countLoadLocalStorage = count;
}else{
this.countLoadLocalStorage += 1;
localStorage.setItem( 'countLoadLocalStorage' , this.countLoadLocalStorage );
return ;
}
// created结束后 #countLoadLocalStorage 才会输入count值
localStorage.setItem( 'countLoadLocalStorage' , count );
},
// 清除进入页面次数
// 与注册的directive hover配合使用
clearCountLoad: function(){
localStorage.setItem( 'countLoadLocalStorage' , 0 );
this.countLoadLocalStorage = 0;
},
// 初始化页面,先从本地存储获取
initList: function(){
// 用本地存储,需要跟本地存储进行比较
// 若存在本地缓存,用本地缓存处理
var list = JSON.parse( localStorage.getItem( 'list' ) );
if ( list ){
this.todoList = list.todoList;
this.completeList = list.completeList;
}else{
this.updateLocalStorageList();
}
},
// 初始化页面,先从本地存储获取
initListBox: function(){
var listBox = document.querySelector( '#todolistBox' );
var app = document.querySelector( '#app' );
listBox.style.height = app.offsetHeight - listBox.offsetTop + 'px';
},
// 增加代办事务
addTodo: function( event ){
var v = this;
var item = {
name: null,
isDone: false,
createdAt: new Date().toLocaleString(),
finishedAt: null
};
var value = event.target.value.trim();
if( value !== ''){
item.name = value;
v.todoList.push( item );
v.updateLocalStorageList();
event.target.value = '';
event.target.blur();
}else{
alert( '什么都不填还想骗?!' )
}
},
// 处理代办事务完成
makeTodoDone: function( todo, index, event ){
var v = this;
if( todo.isDone ){
return ;
}else{
var r = confirm( '你确定找汪小苑确定完成的咯?' );
if( r === true ){
todo.isDone = true;
todo.isMoveIn = false;
todo.finishedAt = new Date().toLocaleString();
v.todoList.splice( index, 1 );
v.addToCompleteList( todo, index );
}else {
return ;
}
}
},
// 在事务完成列表里增加完成的事务
addToCompleteList: function( todo, index ) {
var v = this;
v.completeList.unshift( todo );
v.updateLocalStorageList();
todo.isMoveIn = true;
setTimeout( function(){
todo.isMoveIn = false;
}, 1000 );
},
// 更新本地数据
updateLocalStorageList: function(){
var list = {
"todoList": this.todoList,
"completeList": this.completeList
}
localStorage.setItem( 'list' , JSON.stringify(list) );
}
}
});
</script>
</body>
</html>