实现h5、css3便签列表如下所示,要求第一次打开便签页面时,判断localStorage是否存储有便签数据,便签本地存储可以认为存储在localStorage['note'],若localStorage['note']没有数据先通过js将便签数组数据存储到localStorage['note'],若存在数据,则从localStorage取出数据渲染到页面上,通过nth-child选择器实现对便签的背景色和字体颜色控制,标签有旋转、阴影效果,并且鼠标箭头移上便签时有相应过渡显示效果。提交整个网页文件代码块。 便签数据参考结构: [{ "noteid": 1, "noteName": "便签1", "noteContent": "便签内容1" }, { "noteid": 2, "noteName": "便签2", "noteContent": "便签内容2" }, { "noteid": 3, "noteName": "便签3", "noteContent": "便签内容3" }]
时间: 2023-07-03 19:23:37 浏览: 48
以下是实现的代码块:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>便签列表</title>
<style>
.note {
position: relative;
width: 150px;
height: 200px;
padding: 10px;
background-color: #f5f5f5;
color: #333;
box-shadow: 5px 5px 5px #999;
border-radius: 10px;
transform: rotate(-5deg);
transition: transform .3s ease-in-out;
}
.note:hover {
transform: rotate(0deg);
box-shadow: 10px 10px 10px #999;
}
.note:nth-child(2n) {
background-color: #fff;
color: #666;
}
.note .note-title {
font-size: 16px;
font-weight: bold;
margin-bottom: 10px;
}
.note .note-content {
font-size: 14px;
line-height: 1.5;
height: 120px;
overflow: hidden;
}
</style>
</head>
<body>
<div id="noteList"></div>
<script>
// 获取便签数据
var notes = localStorage.getItem('note');
if (notes) {
notes = JSON.parse(notes);
} else {
notes = [{
"noteid": 1,
"noteName": "便签1",
"noteContent": "便签内容1"
}, {
"noteid": 2,
"noteName": "便签2",
"noteContent": "便签内容2"
}, {
"noteid": 3,
"noteName": "便签3",
"noteContent": "便签内容3"
}];
localStorage.setItem('note', JSON.stringify(notes));
}
// 渲染便签列表
var noteList = document.getElementById('noteList');
for (var i = 0; i < notes.length; i++) {
var note = notes[i];
var noteHtml = '<div class="note">' +
'<div class="note-title">' + note.noteName + '</div>' +
'<div class="note-content">' + note.noteContent + '</div>' +
'</div>';
noteList.innerHTML += noteHtml;
}
</script>
</body>
</html>
```
代码实现了以下功能:
1. 判断是否存在 localStorage['note'],如果不存在,使用默认数据生成便签列表,并将数据存储到 localStorage['note'] 中;
2. 如果存在 localStorage['note'],从中获取数据并渲染便签列表;
3. 使用 nth-child 选择器实现对便签的背景色和字体颜色控制;
4. 使用 transform 和 transition 实现便签的旋转和过渡显示效果。