2024-09-30 課後作業-JavaScript 系列五:第1課 ── 學會 Cookie 相關功能

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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript 系列五:第1課 ── 學會 Cookie 相關功能</title>
<link rel="stylesheet" href="./01_js_cookie.css">
</head>

<body>
<div class="container">
<div class="img_bkg">
<img src="https://images.unsplash.com/photo-1541260894924-7ff059b93d54?w=500&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8NTAwfHxoYXNvbWUlMjBtYW58ZW58MHx8MHx8fDA%3D"
alt="帥哥圖">

</div>
<div class="alert_wrap">
<div class="alert">
<p class="alert_txt" tabindex="0">
警告︰您即將進入之網頁內容需滿十八歲方可瀏覽。

根據「兒童及少年福利與權益保障法」規定,
本網站已於非闔家皆宜之網頁加以標示。
若您尚未年滿十八歲,請點選離開。
若您已滿十八歲,亦不可將本站之內容派發、傳閱、出售、出租、
交給或借予年齡未滿18歲的人士瀏覽,
或將本網站內容向該人士出示、播放或放映。

您年滿十八歲嗎?

</p>
<div class="btn_group">
<button class="exit_btn" onclick="goto_url()">[離開]</button>
<button class="yes_btn" onclick="set_cookie()">[是,我已年滿十八歲]</button>
</div>

</div>

</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@3.0.5/dist/js.cookie.min.js"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="./01_js_cookie.js"> </script>
</body>

</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function set_cookie() {
Cookies.set('yes_18', 'yes');
$('.alert').css('display', 'none');
$('.img_bkg img').css('display', 'block');
}

function goto_url() {
window.location.href = 'https://www.google.com';
}


if (Cookies.get('yes_18') === 'yes') {
$('.alert').css('display', 'none');
$('.img_bkg img').css('display', 'block');
} else {
$('.alert').css('display', 'block');
}

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
*{
margin:0;
border:0;
list-style: none;
}
body {
background-image: url('https://i.imgur.com/B7wsgw4.jpeg');
background-size: cover;
background-position: center; /* 圖片居中顯示 */
background-repeat: no-repeat; /* 不重複圖片 */
}
.container{

position: relative;
height: 100vh;
background-color: rgba(157, 17, 17, 0.09);
}
.img_bkg{

position: absolute;
left: 0;
right: 0;
display: flex;
justify-content: center;
align-items: center;

}
.img_bkg img{
display: none;
max-width: 100%;
max-height: 100%;
}
.alert_wrap{
position: absolute;
top: 0;
left: 0;
right: 0;
display:flex;
height: 100vh;
justify-content: center;
align-items: center;

}


.alert {
display: none;
width: 600px;
height: 500px;
background-color: #202124;
color: #cb3744;
padding: 30px;
border-radius: 8px;
border: 1px solid #2c2f33;

/* 多層陰影增加立體感 */
box-shadow:
0 2px 4px rgba(0, 0, 0, 0.1), /* 輕微陰影,模糊較小 */
0 6px 12px rgba(0, 0, 0, 0.2), /* 中等陰影 */
0 12px 24px rgba(0, 0, 0, 0.3); /* 深層陰影,模糊較大 */

z-index: 3;
}



.alert_txt {
display: flex;
justify-content: center;
align-items: center;
line-height: 1.8rem;

white-space: pre-wrap; /* 保留空白和換行符 */
}
.btn_group{
display:flex;
justify-content: center;
gap: 30px;
}
.btn_group button{
font-size: 20px ;
padding: 5px 10px;
cursor:pointer;
border-radius: 5px;
transition: transform 0.1s ease;
}
.btn_group :active {
transform: scale(0.95); /* 當按鈕被按下時縮小 5% */

}
.exit_btn:hover{
color:#daeaf9;
background-color: #11579d;
border:1px solid #11579d;

}

.yes_btn:hover{
color: #d9f7d9ed;
background-color: #079a07;
border:1px solid #079a07;

}
@media (min-width: 1200px) {
.alert_txt {
line-height: 2.2rem;
}
}

作業取自: https://codelove.tw/@howtomakeaturn/post/pxdbma
by 站長阿川