Parallax - Chèn nhạc nền theo chuyển động cuộn
Trong bài viết hôm này, freetuts sẽ hướng dẫn các bạn chèn nhạc nền vào trang web cùng với hiệu ứng thay đổi nhạc khi màn hình cuộn sang một trang khác. Đây là sự kết hợp thường thấy trong các trang web dùng hiệu ứng parallax. Nào chúng ta cùng bắt đầu.

1. Phần HTML
Trước hết hãy xem qua toàn bộ mã nguồn:
<body>
<div class="parallax">
<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">
<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>
<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>
</div>
</body>Ở phần này, ta thêm thẻ audio và một thẻ div mới để hiển thị bật tắt âm thanh. Lưu ý, là ta dùng thẻ i với định dạng bên dưới:
<i class="fa fa-volume-up" aria-hidden="true"></i>
Thì ta cần thêm vào thẻ head thư viện font-awesome 4.7:
Bài viết này được đăng tại [free tuts .net]
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
2. Phần CSS
Trước hết hãy xem qua toàn bộ mã nguồn:
.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;
}Các bước thực hiện:
Bước 1: ẩn thẻ audio
.parallax audio {
display: none;
}Ta chỉ cần chèn nhạc vào, còn thẻ audio sẽ được ẩn đi.
Bước 2: định dạng cho nút bật tắt nhạc
.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;
}Phần khá đơn giản chỉ canh chỉnh vị trí và màu sắc của các thẻ con bên trong thẻ div.volume .
Lưu ý: vị trí thẻ div.volume phải là position: fixed và ở vị trí góc dưới cùng bên trái. Đây là vị trí quen thuộc cho người dùng khi muốn tìm kiếm nút bật tắt âm thanh.
Bước 3: tạo hiệu ứng hover cho thẻ div.volume
.parallax > div.volume:hover {
opacity: 1;
font-size: 25px;
cursor: pointer;
}Bước này quan trọng là cho giá trị opacity ở mức cao nhất thay vì 0.4 như ban đầu.
3. Phần JavaScript
Trước hết hãy xem qua toàn bộ mã nguồn:
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");
}
}
if($("audio").attr("src") != ".forest.mp3") {
$("audio").attr("src", ".forest.mp3");
}Các bước thực hiện:
Bước 1: tạo hàm bật tắt nhạc
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");
}
}
Mục đích của hàm này là thiết lập lại giá trị của thuộc tính muted trong thẻ audio từ true sang false hoặc ngược lại. Đồng thời thay đổi icon của volume khi bật hoặc tắt nhạc nền.
Bước 2: chèn nhạc khi ảnh nền được cuộn đến
if($("audio").attr("src") != ".forest.mp3") {
$("audio").attr("src", ".forest.mp3");
}Chúng sẽ thêm đoạn mã này vào các câu điều kiện trong bài https://freetuts.net/parallax-tao-hieu-ung-parallax-scrolling-1682.html
Tên bài nhạc nền tương ứng với hình ảnh các bạn mong muốn.
4. Lời kết
Qua bài học hôm nay, các bạn đã học được cách chèn nhạc nền vào trang web với hiệu ứng parallax scrolling. Cảm ơn các bạn, hẹn gặp lại trong các bài viết tiếp theo.
Danh sách file tải về
| Tên file tải về | Pass giải nén |
|---|---|
| Parallax - Chèn nhạc nền theo chuyển động cuộn | freetuts.net hoặc gameportable.net |
| Nhạc nền | freetuts.net hoặc gameportable.net |

Các hàm xử lý chuỗi trong Javascript (cắt / tách / nối chuỗi ..)
Chia lấy phần dư / chia lấy phần nguyên trong javascript
Các cách khai báo biến trong Javascript
Các sự kiện (Event) trong Javascript
Hướng tạo thanh search bar bằng CSS
Hàm array.slice() trong Javascript
Tính tổng hai số bằng Javascript (cộng hai số)
Cách khai báo biến trong PHP, các loại biến thường gặp
Download và cài đặt Vertrigo Server
Thẻ li trong HTML
Thẻ article trong HTML5
Cấu trúc HTML5: Cách tạo template HTML5 đầu tiên
Cách dùng thẻ img trong HTML và các thuộc tính của img
Thẻ a trong HTML và các thuộc tính của thẻ a thường dùng