1. Phần HTML

Trước hết hãy xem lại toàn bộ mã nguồn:

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
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Parallax</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="<a href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css</a>">
  <link rel="stylesheet" type="text/css" media="screen" href="parallax.css">
  <script
              src="<a href="https://code.jquery.com/jquery-3.3.1.min.js">https://code.jquery.com/jquery-3.3.1.min.js</a>"
              integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
              crossorigin="anonymous">
  </script>
  <script src="parallax.js"></script><!--b3 menu click script-->
  <!--b4 scroll effects script-->
  <!--b5 text scroll effect script-->
  <!--b6 add audio-->
  <!--b7 first page and last page-->
  <!--b8 svg shape moving-->
  <!--b9 Tom tat nguon tai lieu goi y-->
  <!--b0 gioi thieu cach tao bo cuc tham khao tai lieu-->
</head>
<body>
  <div class="parallax">
    <div class="shape">
      <img src="eagle.svg">
      <img src="rhino.svg">
      <img src="owl.svg">
      <img src="lion.svg">
      <img src="bear.svg">
    </div>
    <audio src=".forest.mp3" controls autoplay>
      <p>If you are reading this, it is because your browser does not support the audio element.</p>
    </audio>
    <div onclick="toggleMuteAudio()" class="volume">
      <span><i class="fa fa-volume-up" aria-hidden="true"></i>
      </span>
    </div>
    <div class="side-menu"><!--b2 menu-->
      <ul>
        <li class="forest" onclick="moveToImage('.forest')">Forest</li>
        <li class="eagle" onclick="moveToImage('.eagle')">Eagle</li>
        <li class="rhino" onclick="moveToImage('.rhino')">Rhino</li>
        <li class="owl" onclick="moveToImage('.owl')">Owl</li>
        <li class="lion" onclick="moveToImage('.lion')">Lion</li>
        <li class="bear" onclick="moveToImage('.bear')">Bear</li>
      </ul>
    </div>
    <!--b1 background-image-->
    <div class="forest"></div>
    <div class="eagle">
      <p>
        <span>Eagle is the common name for many large birds of prey of the family Accipitridae.</span>
        <span>Eagles belong to several groups of genera, not all of which are closely related.</span>
        <span>Most of the 60 species of eagle are from Eurasia and Africa.</span>
      </p>
    </div>
    <div class="rhino">
      <p>
        <span>A rhinoceros commonly abbreviated to rhino is one any of the numerous extinct species.</span>
        <span>Two of the extant species are native to Africa and three to Southern Asia.</span>
        <span>The term "rhinoceros" is often more broadly applied to now extinct relatives of the superfamily Rhinocerotoidea.</span>
      </p>
    </div>
    <div class="owl">
      <p>
        <span>Owls are birds from the order Strigiformes.</span>
        <span>Owls hunt mostly small mammals, insects, and other birds, although a few species specialize in hunting fish.</span>
        <span>They are found in all regions of the Earth except polar ice caps and some remote islands.</span>
      </p>
    </div>
    <div class="lion">
      <p>
        <span>The lion (Panthera leo) is a species in the family Felidae</span>
        <span>A lion pride consists of a few adult males, related females and cubs.</span>
        <span>Male lions have a prominent mane, which is the most recognisable feature of the species.</span>
      </p>
    </div>
    <div class="bear">
      <p>
        <span>Bears are carnivoran mammals of the family Ursidae.</span>
        <span>They are classified as caniforms, or doglike carnivorans.</span>
        <span>Bears are found on the continents of North America, South America, Europe, and Asia.</span>
      </p>
    </div>
    <div class="back">
      <div onclick="goHome()">
        <span><i class="fa fa-hand-pointer-o" aria-hidden="true"></i></span>
      </div>
    </div>
  </div>
  <div>Icons made by <a href="<a href="https://www.freepik.com/">https://www.freepik.com/</a>" title="Freepik">Freepik</a> from <a href="<a href="https://www.flaticon.com/">https://www.flaticon.com/</a>"               title="Flaticon">www.flaticon.com</a> is licensed by <a href="<a href="http://creativecommons.org/licenses/by/3.0/">http://creativecommons.org/licenses/by/3.0/</a>"              title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></div>
</body>
</html>

Các lưu ý chính cho phần HTML:

– Thư viện liên kết như jQueryvà font-awesome 4.7: khi đã bước chân và thế giới web thì nhất định bạn phải biết đến những thư viện cơ bản này. Vì độ phổ biến của chúng rất rộng rãi.

– Vấn đề bản quyền hình ảnh: như freetuts đã giới thiệu cho các bạn những trang web cung cấp những hình ảnh miễn phí. Tuy nhiên, chúng ta cũng cần chú ý thêm thẻ divphụ để ghi rõ nguồn tránh những vấn đề pháp lý về sau.

– Cách đặt tên class: ở trong loạt bài này, freetuts chủ động tạo những tên class tương đồng nhau để tiện cho việc sử dụng lại trong đoạn JavaScript.

– Thứ tự các thẻ div ngang hàng rất quan trọng vì ta đang trong một trang web có hiệu ứng parallax. Các bạn chú ý những giá trị z-index mà freetuts đã thiết lập.

2. Phần CSS

Trước hết hãy xem lại toàn bộ mã nguồn:

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
html, body, div {
  height: 100%;
}
.parallax {
  position: relative;
}
.parallax > div {
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 100%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  position: relative;
}
.parallax > div > p {
  width: 60%;
  text-align: justify;
  color: white;
  mix-blend-mode: difference;
  margin: 50px;
  font-size: 20px;
}
.parallax div.shape {
  position: absolute;
  z-index: 99;
}
.parallax .shape > img {
  height: 50px;
  width: 50px;
  position: absolute;
}
.parallax .shape > img:nth-child(1) {
  top: 50px;
  left: 100px;
  transform: translate(0, 0);
  animation: eagle 10s ease-in infinite;
}
.parallax .shape > img:nth-child(2) {
  bottom: 150px;
  left: 180px;
  transform: rotateZ(0deg) translateY(-10px);
  animation: rhino 4s linear infinite;
}
.parallax .shape > img:nth-child(3) {
  bottom: 500px;
  left: 800px;
  transform: rotateZ(40deg) translate(0px,0px);
  animation: owl 3s ease-in infinite;
}
.parallax .shape > img:nth-child(4) {
  top: 400px;
  left: 500px;
  animation: lion 1.5s ease-out infinite;
}
.parallax .shape > img:nth-child(5) {
  top: 500px;
  left: 800px;
  opacity: 0;
  transition: 0.5s ease;
  animation: bear 2s ease-in-out infinite;
}
@keyframes eagle {
  0% {
    transform: translate(0,0);
  }
  50% {
    transform: translate(500px, 50px);
  }
  55% {
    transform: rotateY(180deg) translate(-540px, 45px);
  }
  95% {
    transform: rotateY(180deg) translate(40px, 45px);
  }
  100% {
    transform: translate(0,0) rotateY(0deg);
  }
}
@keyframes owl {
  0% {
    transform: rotateZ(40deg) translate(0px,0px);
  }
  30% {
    transform: rotateZ(-40deg) translate(20px,10px);
  }
  50% {
    transform: rotateZ(40deg) translate(10px,40px);
  }
  70% {
    transform: rotateZ(-40deg) translate(0px,50px);
  }
  100% {
    transform: rotateZ(40deg) translate(0px,0px);
  }
}
@keyframes bear {
  50% {
    opacity: 1;
    transform: scale(2,2)
  }
  100% {
    opacity: 0;
    transform: scale(0.5, 0.5);
  }
}
@keyframes lion {
  10% {
    transform: rotateX(360deg);
  }
  15% {
    transform: rotateZ(360deg);
  }
  20% {
    transform: rotateY(360deg)
  }
}
@keyframes rhino {
  50% {
    transform: rotateZ(-40deg) translateY(10px);
  }
  70% {
    transform: rotateZ(-40deg) translateY(20px);
  }
  100% {
    transform: rotateZ(0deg) translateY(-10px);
  }
}
.parallax audio {
  display: none;
}
.parallax > div.volume {
  height: 50px;
  width: 50px;
  position: fixed;
  left: 20px;
  bottom: 20px;
  z-index: 99;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: white;
  color: black;
  opacity: 0.4;
  border-radius: 10px;
  transition: 0.5s ease-in;
}
.parallax > div.volume:hover {
  opacity: 1;
  font-size: 25px;
  cursor: pointer;
}
.parallax > div.side-menu {
  position: fixed;
  top: 30%;
  left: 10px;
  background-color: transparent;
  opacity: 0.5;
  height: fit-content;
  height: -moz-fit-content;
  color: white;
  font-size: 20px;
  font-weight: 800;
  mix-blend-mode: difference;
  z-index: 99;
}
div.side-menu ul {
  padding: 0 10px;
  list-style: none;
}
div.side-menu ul li {
  margin: 20px;
  width: 100px;
  padding-bottom: 5px;
  transition: 1s ease;
  border-bottom: 2px solid white;
  box-sizing: border-box;
}
div.side-menu ul li:hover {
  cursor: pointer;
  padding-left: 50px;
}
.active {
  padding-left: 50px;
}
div.forest {
  background-image: url("forest.jpg");
}
div.eagle {
  background-image: url("eagle.jpg");
}
div.rhino {
  background-image: url("rhino.jpg");
}
div.owl {
  background-image: url("owl.jpg");
}
div.lion {
  background-image: url("lion.jpg");
}
div.bear {
  background-image: url("bear.jpg");
}
div.back {
  background-image: url("back.jpg");
  justify-content: center;
}
div.back > div {
  font-size: 50px;
  height: 80px;
  width: 80px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  border: 2px solid black;
}
div.back > div:hover {
  transition: 1s ease-in;
  background-color: azure;
  color: #196f3d ;
  border: 2px solid #196f3d ;
  cursor: pointer;
}
.parallax > div > p > span {
  width: 100%;
  float: left;
  -webkit-clip-path: polygon(100% 0, 100% 100%, 0 100%, 0 80%);
  clip-path: polygon(100% 0, 100% 100%, 0 100%, 0 80%);
  transform: translateY(-50px);
  opacity: 0;
  animation: text 10s 1.5s ease-in-out infinite;
}
.parallax > div > p > span:first-child{
  animation-delay: 2s;
}
.parallax > div > p > span:last-child{
  animation-delay: 1s;
}
@keyframes text {
  0% {
    transform: translateY(-50px);
    opacity: 0;
    -webkit-clip-path: polygon(100% 0, 100% 100%, 0 100%, 0 80%);
    clip-path: polygon(100% 0, 100% 100%, 0 100%, 0 80%);
  }
  20% {
    transform: translateY(0);
    opacity: 1;
    -webkit-clip-path: polygon(100% 0, 100% 100%, 0 100%, 0 15%);
    clip-path: polygon(100% 0, 100% 100%, 0 100%, 0 15%);
  }
  80% {
    transform: translateY(0);
    opacity: 1;
    -webkit-clip-path: polygon(100% 0, 100% 100%, 0 100%, 0 15%);
    clip-path: polygon(100% 0, 100% 100%, 0 100%, 0 15%);
  }
  100% {
    transform: translateY(50px);
    opacity: 0;
    -webkit-clip-path: polygon(100% 0, 100% -0%, 0 100%, 0 100%);
    clip-path: polygon(100% 0, 100% -0%, 0 100%, 0 100%);
  }
}

Các lưu ý chính cho phần CSS:

– Thiết lập height: 100%cho thẻ divchứa ảnh nền và thẻ htmlbody.

– Màu sắc của văn bản phải tương phản với màu của ảnh nền bằng cách thiết lập mix-blend-mode: difference .

– Độ trễ và thời gian của các hiệu ứng là điều quan trọng trong web parallax.

3. Phần Javascript

Trước hết hãy xem lại toàn bộ mã nguồn:

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
function moveToImage( elem ) {
  setTimeout( function () {
    $( 'html, body' ).animate({scrollTop: $('div' + elem).offset().top}, 1500);
    setTimeout( function () {
      $( "audio" ).attr("src", elem + ".mp3");
    }, 1500)
  }, 500);
}
function toggleMuteAudio(){
  $("audio").prop("muted",!$("audio").prop("muted"));
  if($("audio").prop("muted")) {
    $(".volume i").removeClass("fa-volume-up");
    $(".volume i").addClass("fa-volume-off");
  }
  else {
    $(".volume i").removeClass("fa-volume-off");
    $(".volume i").addClass("fa-volume-up");
  }
}
function goHome() {
  moveToImage(".forest");
}
$(document).ready(function () {
  //$("html, body").animate({scrollTop: $(document).height()}, 50000);
  $("ul li").click(function () {
      setTimeout(function () {
          
      }, 300);
  });
  $(window).scroll(function() {
    $("li").removeClass("active");
    if(Math.abs($(window).scrollTop() - $("div.forest").offset().top) < $(window).height()/2) {
      if($("audio").attr("src") != ".forest.mp3") {
        $("audio").attr("src", ".forest.mp3");
      }
      $("li.forest").addClass("active");
    }
    else if(Math.abs($(window).scrollTop() - $("div.eagle").offset().top) < $(window).height()/2) {
      if($("audio").attr("src") != ".eagle.mp3") {
        $("audio").attr("src", ".eagle.mp3");
      }
      $("li.eagle").addClass("active");
    }
    else if(Math.abs($(window).scrollTop() - $("div.rhino").offset().top) < $(window).height()/2) {
      if($("audio").attr("src") != ".rhino.mp3") {
        $("audio").attr("src", ".rhino.mp3");
      }
      $("li.rhino").addClass("active");
    }
    else if(Math.abs($(window).scrollTop() - $("div.owl").offset().top) < $(window).height()/2) {
      if($("audio").attr("src") != ".owl.mp3") {
        $("audio").attr("src", ".owl.mp3");
      }
      $("li.owl").addClass("active");
    }
    else if(Math.abs($(window).scrollTop() - $("div.lion").offset().top) < $(window).height()/2) {
      if($("audio").attr("src") != ".lion.mp3") {
        $("audio").attr("src", ".lion.mp3");
      }
      $("li.lion").addClass("active");
    }
    else if(Math.abs($(window).scrollTop() - $("div.bear").offset().top) < $(window).height()/2) {
      if($("audio").attr("src") != ".bear.mp3") {
        $("audio").attr("src", ".bear.mp3");
      }
      $("li.bear").addClass("active");
    }
    else if(Math.abs($(window).scrollTop() - $("div.back").offset().top) < $(window).height()/2) {
      if($("audio").attr("src") != ".back.mp3") {
        $("audio").attr("src", ".back.mp3");
      }
      $("li.back").addClass("active");
    }
  });
});

Các lưu ý chính cho phần JavaScript:

– Hiểu được điều kiện cho khi cuộn vào ảnh nền mới.

– Kiểm tra bài nhạc đã tồn tại trước đó trong thẻ audio giúp thao tác cuộn không làm ngắt quãng âm nền.

– Trả về giá trị mặc định của thẻ litrước khi thiết lập gía trị mới.

4. Phần mở rộng

Freetuts xin tặng các bạn đoạn mã JavaScriptkiểm tra một thẻ bất kỳ có năm trong màn hình cửa sổ trình duyệt hiện tại không:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();
    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();
    return (
      ((elemBottom <= docViewBottom) && (elemTop >= docViewTop)) ||
      (((elemBottom >= docViewBottom) && (elemTop >= docViewTop)) &&
      ((docViewBottom - elemTop) > $(window).height()/2)) ||
      (((elemBottom <= docViewBottom) && (elemTop <= docViewTop)) &&
      ((elemBottom - docViewTop) > $(window).height()/2))
    );
}

5. Lời kết

Như vậy là các bạn đã hoàn thành nội dụng của loạt bài hiệu ứng parallax. Cảm ơn các bạn đã theo dõi bài viết này.

Theo:https://freetuts.net

 

 

ĐĂNG KÝ THÀNH VIÊN

NẾU NỘI DUNG HỮU ÍCH HÃY XEM QUẢNG CÁO ĐỂ ỦNG HỘ

NẾU NỘI DUNG HỮU ÍCH HÃY XEM QUẢNG CÁO ĐỂ ỦNG HỘ

Được quan tâm nhiều nhất

  • Apple Watch Series 5 Teardown - Always on Display Explained

  • Apple Watch Series 4 Teardown

  • Phim Ngắn Đột Kích - Phiên bản 10 năm trước

  • iPhone 11 Pro Max Teardown - Tiny Motherboard & BIG Battery!

Bạn thấy bài viết này thế nào?
Thể hiện yêu thương tác giả ở đây nhé!

Thích bài viết

thích

Chia sẻ ngay!

phuongle

Thành viên từ: 10/12/2019

Điểm uy tín: 5,987

SMod: 1,289 hướng dẫn đã chia sẻ

Team

Lập Trình Thành viên của Lập Trình

1 Thành viên

1,289 bài viết

Thêm bình luận

Bình luận bằng tài khoản Facebook

After viewing your support content - Please click advertisement for Support & Donate us team! Đóng