Trong bài viết hôm này, kiso 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 [kiso.vn]
<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://kiso.vn/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 | kiso.vn hoặc gameportable.net |
Nhạc nền | kiso.vn hoặc gameportable.net |
Bài viết liên quan
[CSF-2] Một số thiết lập CSF, LFD
Hôm nay mình sẽ thực hiện một số thiết lập trên CSF Mở file config để sửa đổi một số tính năng dưới /etc/csf/csf.conf Nội dung chính1. Phần HTML2. Phần CSS3. Phần JavaScript4. Lời kết1....
[CSF-1] Tăng bảo mật Server với ConfigServer Firewall (CSF)
Nội dung chính1. Phần HTML2. Phần CSS3. Phần JavaScript4. Lời kết1. Khái niệm CSF: CSF (ConfigServer & Firewall) là một bộ ứng dụng hoạt động trên Linux như một firewall được phát hành miễn phí...
Sử dụng SSH Key với Gitlab và Github
Bài viết này mình sẽ hướng dẫn các bạn tạo ssh key cho Gitlab và Github SSH là gì? Secure Socket Shell là một giao thức mạng dùng để thiết lập kết nối mạng một...
Directory traversal vulnerabilities (phần 4)
Nội dung chính1. Phần HTML2. Phần CSS3. Phần JavaScript4. Lời kếtV. Phân tích và khai thác các lỗ hổng Directory traversal (tiếp) 5. Bypass lỗ hổng khi trang web sử dụng đường dẫn đầy đủ...
Directory traversal vulnerabilities (phần 3)
Nội dung chính1. Phần HTML2. Phần CSS3. Phần JavaScript4. Lời kếtV. Phân tích và khai thác các lỗ hổng Directory traversal 1. Lỗ hổng xảy ra khi sử dụng các hàm đọc file và tin...
Directory traversal vulnerabilities (phần 2)
Nội dung chính1. Phần HTML2. Phần CSS3. Phần JavaScript4. Lời kếtIII. Vì sao lỗ hổng Directory traversal xuất hiện? Với mỗi ngôn ngữ lập trình khác nhau, điểm xuất hiện các lỗ hổng Directory traversal...