实现h5、css3便签列表如下所示,要求第一次打开便签页面时,判断localStorage是否存储有便签数据,便签本地存储可以认为存储在localStorage['note'],若localStorage['note']没有数据先通过js将便签数组数据存储到localStorage['note'],若存在数据,则从localStorage取出数据渲染到页面上,通过nth-child选择器实现对便签的背景色和字体颜色控制,标签有旋转、阴影效果,并且鼠标箭头移上便签时有相应过渡显示效果
时间: 2024-02-21 11:00:04 浏览: 28
好的,下面是实现代码:
HTML部分:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>便签列表</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="notes">
<!-- 这里将通过JS生成便签 -->
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS部分:
```css
body {
background-color: #f0f0f0;
}
#notes {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.note {
position: relative;
width: 200px;
height: 200px;
margin: 10px;
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16);
transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
}
.note:hover {
transform: rotate(-5deg);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.3);
}
.note:nth-child(2n) {
background-color: #f9d3b7;
color: #4c2c1d;
}
.note:nth-child(3n) {
background-color: #b7d9f9;
color: #1d3b4c;
}
.note:nth-child(4n) {
background-color: #b7f9d3;
color: #1d4c2c;
}
.note:nth-child(5n) {
background-color: #f9b7d3;
color: #4c1d2c;
}
.note h2 {
margin: 0;
font-size: 20px;
text-align: center;
text-shadow: 1px 1px #fff;
}
.note p {
margin: 10px 0 0;
font-size: 16px;
text-align: justify;
text-shadow: 1px 1px #fff;
}
```
JavaScript部分:
```javascript
// 判断localStorage是否已经有便签数据
if (!localStorage.getItem('note')) {
// 若没有,则创建新的便签数据
let notes = [{
title: '便签1',
content: '这是便签1的内容'
}, {
title: '便签2',
content: '这是便签2的内容'
}, {
title: '便签3',
content: '这是便签3的内容'
}, {
title: '便签4',
content: '这是便签4的内容'
}, {
title: '便签5',
content: '这是便签5的内容'
}];
// 将便签数据存入localStorage
localStorage.setItem('note', JSON.stringify(notes));
}
// 从localStorage获取便签数据并解析成对象数组
let notes = JSON.parse(localStorage.getItem('note'));
// 生成便签DOM节点并添加到页面中
let notesContainer = document.getElementById('notes');
for (let i = 0; i < notes.length; i++) {
let note = document.createElement('div');
note.className = 'note';
note.innerHTML = '<h2>' + notes[i].title + '</h2><p>' + notes[i].content + '</p>';
notesContainer.appendChild(note);
}
```
以上代码实现了一个简单的便签列表,其中包括了便签的背景色、字体颜色、旋转、阴影和鼠标移上效果等特性,并且通过localStorage实现了便签数据的本地存储。