-
Notifications
You must be signed in to change notification settings - Fork 1
/
9_5_scroll_move_large_bg.html
102 lines (86 loc) · 3.65 KB
/
9_5_scroll_move_large_bg.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
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<style>
#bg {
background-image: url("assets/stand90n.jpg");
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 180vw;
background-position: 0 0;
}
</style>
<title>Scrolled-image: Moving a large bg image with scrolling</title>
</head>
<body>
<div id="bg" class="sticky-top" style="height:100vh;width:100vw;z-index:-1"></div>
<div class="loc" data-sx="0" data-sy="0" data-ex="0" data-ey="80"
style="width:100vw;height:200vh;background:rgba(0,0,0,0.1);">
<div class="row">
<div class="offset-4 col-8">
<p>從頭介紹到腳</p>
</div>
</div>
</div>
<div class="loc" data-sx="0" data-sy="80" data-ex="120" data-ey="80"
style="width:100vw;height:200vh;background:rgba(0,0,0,0.1);">
<div class="row">
<div class="offset-4 col-8">
<p>從腳介紹到側身的腳</p>
</div>
</div>
</div>
<div class="loc" data-sx="120" data-sy="80" data-ex="120" data-ey="0"
style="width:100vw;height:200vh;background:rgba(0,0,0,0.1);">
<div class="row">
<div class="offset-4 col-8">
<p>從側身的腳介紹到側身的頭</p>
</div>
</div>
</div>
<!-- section 4 -->
<div class="loc" data-sx="120" data-sy="0" data-ex="0" data-ey="0"
style="width:100vw;height:200vh;background:rgba(0,0,0,0.1);">
<div class="row">
<div class="offset-4 col-8">
<p>從側身的頭介紹回頭</p>
</div>
</div>
</div>
<div style="background:rgba(0,0,0,0.1);height:100vh"></div>
<div style="background:grey;height:100vh"></div>
<!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous">
</script>
<script>
$(window).scroll(function () {
var scrolled = $(document).scrollTop();
$(".loc").each(function () {
var thispos = $(this).offset().top;
var thish = $(this).height();
if (scrolled >= thispos && scrolled < thispos + thish) {
var perc = (scrolled - thispos) / thish;
var newx = perc * ($(this).data("ex") - $(this).data("sx")) + $(this).data("sx");
var newy = perc * ($(this).data("ey") - $(this).data("sy")) + $(this).data("sy");
$("#bg").css("background-position", newx + "% " + newy + "%");
console.log(newx + "% " + newy + "%");
}
});
// if (scrolled > winh) {
// $("#bg").css("position", "fixed");
// $("#bg").css("left", (-1) * winw * ((scrolled - winh) / winh));
// }
});
</script>
</body>
</html>