1. Phần HTML

Trước hết hãy xem qua 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
<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ẻ audiovà một thẻ divmới để hiển thị bật tắt âm thanh. Lưu ý, là ta dùng thẻ ivới định dạng bên dưới:

1
<i class="fa fa-volume-up" aria-hidden="true"></i>

Thì ta cần thêm vào thẻ headthư viện font-awesome 4.7:

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>">

2. Phần CSS

Trước hết hãy xem qua 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
.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

1
2
3
.parallax audio {
  display: none;
}

Ta chỉ cần chèn nhạc vào, còn thẻ audiosẽ được ẩn đi.

Bước 2: định dạng cho nút bật tắt nhạc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.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 hovercho thẻ div.volume

1
2
3
4
5
.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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

1
2
3
4
5
6
7
8
9
10
11
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 mutedtrong thẻ audiotừ truesang falsehoặc ngược lại. Đồng thời thay đổi iconcủa volumekhi 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

1
2
3
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.nparallax-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.

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

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

  • Apple Watch Series 5 Teardown - Always on Display Explained

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

  • Apple Watch Series 4 Teardown

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