-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTML5存储数据和缓存
167 lines (147 loc) · 4.68 KB
/
HTML5存储数据和缓存
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>html5缓存</title>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js" type="text/javascript"></script>
</head>
<style type="text/css">
section {
width: 600px;
margin: 0 auto;
border: 1px solid #000;
box-shadow: 0px 5px 10px #c3edf3;
border-radius: 4px;
background: #e9dfc7;
}
section p {
text-align: center;
}
div {
display: inline-block;
}
.bg {
width: 20px;
height: 20px;
border-radius: 10px;
border: 1px solid #000;
margin: 5px;
vertical-align: middle;
}
.bg1 {
background: rgb(40, 53, 72);
}
.bg2 {
background: #e9dfc7;
}
.div1 {
text-align: center;
display: block;
}
</style>
<body>
<section id="section">
<p>我爱谁 跨不过 从来也不觉得错</p>
<p>自以为 抓着痛 就能往回忆里躲</p>
<p>偏执相信着 受诅咒的水晶球</p>
<p>阻挡可能心动的理由</p>
<p>而你却 靠近了 逼我们视线交错</p>
<p>原地不动 或像前走 突然在意这分钟</p>
<p>眼前黄沙弥漫了等候</p>
<p>耳边传来孱弱的呼救</p>
<p>追赶要我爱的不保留</p>
<p>我身骑白马 走三关</p>
</section>
<div class="div1">
<p>字体 <button id="large_button">放大</button> <button id="small_button">缩小</button></p>
<div><span>背景色</span>
<div class=" bg bg1"></div>
<div class=" bg bg2"></div>
</div>
</div>
<!-- 闭包处理 (function(){})();-->
<script type="text/javascript">
// 封装一个函数 --开始---
(function() {
//存储数据
var Util = (function() {
//封装一个类,作为key的前缀
var pirfix = 'html5_reader';
//获取key在本地的存储值
var stroageGet = function(key) {
return localStorage.getItem(pirfix + key);
}
//讲value存进key字段
var stroageSet = function(key, value) {
return localStorage.setItem(pirfix + key, value);
}
return {
stroageGet: stroageGet,
stroageSet: stroageSet
}
})();
//封装一个函数 ---结束---
//定义一些变量
var bgColor;
var textColor;
//存储字体的大小 --开始--
//字体要放大的容器id
var Rootcontainer = $('#section');
//取存储的字体大小,是一串字符串
var initFontsize = Util.stroageGet('font-size');
//转换为数字
initFontsize = parseInt(initFontsize);
//如果没有存储的字体,就设置初始字号
if (!initFontsize) {
initFontsize = 14;
}
Rootcontainer.css('font-size', initFontsize);
//存储字体的大小 --结束--
//存储背景色和字体颜色
textColor = Util.stroageGet('color');
$('#section p').css('color', textColor);
bgColor = Util.stroageGet('background');
Rootcontainer.css('background', bgColor);
//交互数据的绑定
function EvantHeader() {
//放大字体
$('#large_button').click(function() {
if (initFontsize > 20) {
return;
}
initFontsize += 1;
Rootcontainer.css('font-size', initFontsize);
Util.stroageSet('font-size', initFontsize);
});
//缩小字体
$('#small_button').click(function() {
if (initFontsize < 12) {
return;
}
initFontsize -= 1;
Rootcontainer.css('font-size', initFontsize);
Util.stroageSet('font-size', initFontsize);
});
//改变背景颜色和字体颜色
$('.bg1').click(function() {
bgColor = 'rgb(40, 53, 72)';
textColor = '#fff';
$('#section p').css('color', textColor);
Util.stroageSet('color', textColor);
Util.stroageSet('background', bgColor);
Rootcontainer.css('background', bgColor);
});
$('.bg2').click(function() {
bgColor = '#e9dfc7';
textColor = '#000';
Rootcontainer.css('background', bgColor);
Util.stroageSet('background', bgColor);
$('#section p').css('color', textColor);
Util.stroageSet('color', textColor);
});
}
EvantHeader();
})();
</script>
</body>
</html>