-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove.html
207 lines (206 loc) · 6.09 KB
/
move.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用transition实现平移</title>
<style>
*{
padding: 0;
margin: 0;
list-style: none;
}
body{
background: #FFFFFF;
}
header{
width: 100%;
height: 40px;
border-bottom: 1px solid #6eb49c;
}
form{
width: 900px;
margin: 10px auto;
}
form input[type="submit"]{
margin-left: 20px;
}
main{
width: 900px;
height: auto;
margin: 20px auto;
}
main > .tab{
height: 40px;
display: flex;
position: relative;
}
.tab >li{
height: 100%;
flex-grow: 1;
text-align: center;
line-height: 40px;
transition: all 0.5s linear;
}
.tab li.hot{
color: #fff;
}
.tab > .top1{
width: 300px;
height: 100%;
position: absolute;
left: 0;
top:0;
background: #6eb49c;
z-index: -1;
transition: all 0.5s linear;
}
.tab > li.hot:first-child ~ .top1{
transform: translate(0,0);
}
.tab >li.hot:nth-child(2) ~ .top1 {
transform: translate(100%,0);
}
.tab >li.hot:nth-child(3) ~ .top1{
transform: translate(200%,0);
}
.content li{
font-size: 18px;
line-height: 3;
color: #333333;
padding: 10px 0;
border-bottom: 1px solid #c55353;
}
.content li p{
display: inline-block;
}
.content li time{
float: right;
}
.content li span{
float: right;
margin-left: 20px;
}
</style>
</head>
<body>
<header>
<form action="">
<input type="text" placeholder="请输入要添加的内容" name="textt">
<input type="submit" name="submitBtn">
</form>
</header>
<main>
<ul class="tab">
<li class="hot" type="all">全部</li>
<li type="done">完成</li>
<li type="doing">未完</li>
<div class="top1"></div>
</ul>
<ul class="content">
</ul>
</main>
<script>
window.addEventListener('load',function(){
let tab=document.querySelectorAll('.tab > li');
let prev=0;
let type='all';
//创建对象列表
let content=document.querySelector('.content');
let todoList=[
{
id:1,content:"侄女好可爱",ctime:'2019/6/12',checked:false
},
{
id:2,content:"侄女好可爱!",ctime:'2019/6/10',status:true
},
{
id:3,content:"侄女好可爱!!",ctime:'2019/6/8',status:false
},
{
id:4,content:"侄女好可爱!!!",ctime:'2019/6/8',status:true
}
];
tab.forEach(function (ele,index) {
ele.onclick=function () {
tab[prev].classList.remove('hot');
this.classList.add('hot');
prev=index;
//获取状态,三种状态跳转
type=this.getAttribute('type');
render(optionType(type));
}
});
tab[0].onclick();
function optionType(type) {
let arr=[];
switch (type) {
case 'all':arr=todoList;break;
case 'done':arr=todoList.filter(function(ele){
return ele.status;
});break;
case 'doing':arr=todoList.filter(ele=>!ele.status);break;
}
return arr;
}
// render(todoList);
content.onclick=function (e) {
let target=e.target;
let id=target.parentNode.id;
if(target.nodeName==='INPUT'){
//找到该条记录id,finfIndex,filter(注意filter返回是数组)
// let find=todoList.findIndex(ele=>ele.id==id);
let ele=todoList.filter(ele=>ele.id==id)[0];
//获取当前复选框状态
console.log(ele);
ele.status=target.checked;
console.log(ele);
}else if(target.nodeName==='SPAN'){
//找到id
let find=todoList.findIndex(ele=>ele.id==id);
console.log(find);
//数组删除元素
todoList.splice(find,1);
}
render(optionType(type)); //type没有定义
};
// //渲染
function render(arr){
let html='';
arr.forEach(function (elem,index) {
if(elem.status){
html+= `
<li id="${elem.id}">
<input type="checkbox" checked="checked"><p>${elem.content}</p><span>X</span><time>${elem.ctime}</time>
</li>
`;
}else{
html+= `
<li id="${elem.id}">
<input type="checkbox"><p>${elem.content}</p> <span>X</span><time>${elem.ctime}</time>
</li>
`;
}
});
content.innerHTML=html;
}
let submitBtn=document.getElementsByName('submitBtn');
let textContent=document.getElementsByName('textt');
console.log(submitBtn[0]);
submitBtn[0].onclick=function (e){
//消除submit提交的默认
e.preventDefault();
let obj=createObj();
todoList.push(obj); //对象推进对象数组里
render(optionType(type));
}
function createObj(){
let content=textContent[0].value;
let id=todoList[todoList.length-1].id+1;
let ctime=new Date().toLocaleDateString();
let status=false;
return {id,content,ctime,status}
}
})
</script>
</body>
</html>