-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex.07.html
104 lines (79 loc) · 3.17 KB
/
ex.07.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
margin: 0;
height: 300vh;
transition: .5s;
}
.wrap {
width: 100%;
height: 30px;
background-color: #ccc;
position: fixed;
top: 0;
}
.box {
background-color: rgb(181, 255, 43);
height: 30px;
width: 100%;
}
.img-wrap {
margin: 100px auto;
width: 100px;
border-radius: 50%;
overflow: hidden;
transition: 2s;
}
.img-wrap img {
width: 100%;
object-fit: cover;
}
</style>
</head>
<body>
<div class="wrap">
<div class="box"></div>
</div>
<div class="img-wrap">
<img src="./imgs/person4.jpg" alt="">
</div>
<script src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<script>
console.log('body 全長:', $('body').height());
$('.box').css('width', (50 + '%'));
//window是特殊字代表瀏覽器視窗
console.log('分子/分母:', $(window).scrollTop() / $('body').height());
console.log('乘以100:', ($(window).scrollTop() / $('body').height()) * 100);
console.log('去掉後面小數點:', Math.floor(($(window).scrollTop() / $('body').height()) * 100));
console.log('後面加一個%的符號:', Math.floor(($(window).scrollTop() / $('body').height()) * 100) + '%');
$('.box').css('width', Math.floor(($(window).scrollTop() / $('body').height()) * 100) + '%');
$(window).scroll(function () {
//body.height是整個視窗,window.height = scrollbar的長度放大,所以body長-window長就是scrollbar的長度
const bodyWindowheight = $('body').height() - $(window).height();
const percentPage = Math.floor(($(window).scrollTop() / bodyWindowheight) * 100)
$('.box').css('width', Math.floor(($(window).scrollTop() / bodyWindowheight) * 100) + '%');
console.log(' myscroll page ', Math.floor(($(window).scrollTop() / bodyWindowheight) * 100) + '%');
// 在事件裡面,加入一個條件判斷式
// 兩個或以上條件想一起判斷,可以用 && 來連結,所有條件同時成立才為 true
if ($(window).scrollTop() > 500 && $(window).scrollTop() <= 600) {
$('body').css('background', 'orange');
$('.img-wrap').css('transform', 'translateX(0)').css('opacity', '1');
}
else if ($(window).scrollTop() > 600) {
$('body').css('background', 'lightyellow');
}
else {
$('body').css('background', 'black');
$('.img-wrap').css('transform', 'translateX(500px)').css('opacity', '0');
}
})
</script>
</body>
</html>