-
Notifications
You must be signed in to change notification settings - Fork 1
/
9_7_scrolled_svg.html
174 lines (147 loc) · 7.92 KB
/
9_7_scrolled_svg.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
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
167
168
169
170
171
172
173
174
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<style>
.earth-img {
position: fixed;
height: 100%;
object-fit: cover;
/* object-position: right;*/
opacity: 1;
}
.transit-effect1 {
transition: bottom 1s;
}
</style>
<title>Scrolling to control svg path displaying percentage</title>
</head>
<body>
<div style="height:100vh;background:deepskyblue"></div>
<div class="sticky-top" style="z-index:-1">
<svg id="svgobj" style="transition:all ease-in 1s;" height="200vh" viewBox="0 0 7200 4005" version="1.1"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve"
xmlns:serif="http://www.serif.com/"
style="fill-rule:evenodd;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:1.5;">
<g transform="matrix(1.5,0,0,1.5,0,0)">
<!-- <use xlink:href="Linpen2019_lg.jpg" x="0" y="0" width="4800px" height="2670px"/> -->
<image href="images/Linpen2019_lg.jpg" x="0" y="0" width="4800px" height="2670px" />
<g id="fill1">
<path d="M1220,1460L1420,1460L1480,1560L1430,1560L1300,1760L1260,1820L1130,1800L1220,1460Z"
style="fill:none;fill-opacity:0.5;" />
<path d="M940,1500L1160,1570L1120,1720L850,1670L940,1500Z" style="fill:none;fill-opacity:0.5;" />
</g>
<path id="path1" d="M970,2230L1171,1584"
style="fill:none;stroke:blue;stroke-width:10px;stroke-opacity:0;" />
<g id="fill2">
<path
d="M1360,1010.57L1500,960L1620,920L1660,967.454L1611.38,1020L1560,1020L1540,1060L1420,1080L1291.73,1220L1340,1020L1360,1010.57Z"
style="fill:none;fill-opacity:0.49;" />
</g>
<path id="path2" d="M1171,1584L1330,1010"
style="fill:none;stroke:rgb(255,0,181);stroke-width:10px;stroke-opacity:0;" />
<path id="path3_1" d="M1330,1010L1540,190L1650,30"
style="fill:none;stroke:LIME;stroke-width:10px;stroke-opacity:0;" />
<path id="path3_2" d="M1650,30L1820,120L2405,335"
style="fill:none;stroke:LIME;stroke-width:10px;stroke-opacity:0;" />
<path id="path3_3" d="M2405,335L2990,550L3800,1070"
style="fill:none;stroke:LIME;stroke-width:10px;stroke-opacity:0;" />
<g id="fill3">
<path d="M3560,1300L3680,1120L3780,1080L3840,1120L3680,1380L3560,1300Z"
style="fill:none;fill-opacity:0.36;" />
<path d="M3500,720L3560,640L3660,720L3600,800L3500,720Z" style="fill:none;fill-opacity:0.36;" />
</g>
<circle r="20" cy="0" cx="0" id="dot" style="fill:white" />
</g>
</svg>
</div>
<div class="row">
<div class="col-6"></div>
<div class="col-6">
<div data-x="-10%" data-y="-50%" data-scale="1" data-fill="#fill1" data-path="#path1" class="maploc"
style="height:100vh;background:rgba(0,0,0,0.3)">
<h1 style="width:30vw;background:rgba(255,255,255,0.7);">Section 1</h1>
</div>
<div data-x="-10%" data-y="-30%" data-scale="1" data-fill="#fill2" data-path="#path2" class="maploc"
style="height:100vh;background:rgba(0,0,0,0.2)">
<h1 style="width:30vw;background:rgba(255,255,255,0.7);">Section 2</h1>
</div>
<div data-x="-10%" data-y="-0%" data-scale="1" data-fill="#fill3" data-path="#path3_1" class="maploc"
style="height:200vh;background:rgba(0,0,0,0.1)">
<h1 style="width:30vw;background:rgba(255,255,255,0.7);">Section 3_1</h1>
</div>
<div data-x="-30%" data-y="-1.5%" data-scale="1.2" data-fill="#fill3" data-path="#path3_2" class="maploc"
style="height:200vh;background:rgba(0,0,0,0.1)">
<h1 style="width:30vw;background:rgba(255,255,255,0.7);">Section 3_2</h1>
</div>
<div data-x="-80%" data-y="-4%" data-scale="1.5" data-fill="#fill3" data-path="#path3_3" class="maploc"
style="height:400vh;background:rgba(0,0,0,0.1)">
<h1 style="width:30vw;background:rgba(255,255,255,0.7);">Section 3_3</h1>
</div>
</div>
</div>
<div style="height:100vh;"></div>
<div style="height:100vh;background:grey"></div>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous">
</script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous">
</script>
<script>
$(window).scroll(function () {
var scrolled = $(document).scrollTop();
var winh = $(window).height();
$(".maploc").each(function () {
var thispos = $(this).offset().top;
var thish = $(this).height();
// filling polygon later
if (scrolled >= thispos && scrolled < thispos + thish){
var fill_id = $(this).data("fill");
$(fill_id + " path").css("fill", "green");
// "#fill1 path"
}
if (scrolled >= thispos - winh / 2 && scrolled < thispos + thish - winh / 2) {
// Moving bg image by transoform
$("#svgobj").css({
"transform": "translate(" + $(this).data("x") + ", " + $(this).data(
"y") + ") scale(" + $(this).data("scale") + ")"
});
// Showing path with scrolled percentage
var path = $($(this).data("path"))[0];
$($(this).data("path")).css("stroke-opacity", "1");
var length = path.getTotalLength();
console.log(length);
path.style.strokeDasharray = length;
path.style.strokeDashoffset = length;
var scrollpercent = (scrolled - thispos + winh / 2) / thish;
var draw = length * scrollpercent;
path.style.strokeDashoffset = length - draw;
//Showing dot with path
var p = path.getPointAtLength(draw);
var dot = document.getElementById('dot');
dot.setAttribute("transform", `translate(${p.x}, ${p.y})`);
// https://www.w3schools.com/howto/howto_js_scrolldrawing.asp
// https://potatodie.nl/diffuse-write-ups/move-a-dot-along-a-path/
}
});
if (scrolled == 0) {
$("svg g path").css({
"fill": "none",
});
$("#svgobj").css("transform", "translate(0%, 0%) scale(1)");
}
});
</script>
</body>
</html>