Parallax – Hoàn thiện bố cục, hiệu ứng và mở rộng

Các bạn thân mến, như vậy là chúng ta đã đi đến cuối chặng đường của loạt bài viết về hiệu ứng parallax này. Hôm nay, kiso sẽ tổng kết lại những gì các bạn đã học được và những phần mở rộng. Nào chúng ta hãy cùng bắt đầu.

1. Phần HTML

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

<!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="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" type="text/css" media="screen" href="parallax.css">
  <script
			  src="https://code.jquery.com/jquery-3.3.1.min.js"
			  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="https://www.freepik.com/" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" 			    title="Flaticon">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/" 			    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ư jQuery 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.

Bài viết này được đăng tại [kiso.vn]

– Vấn đề bản quyền hình ảnh: như kiso đã 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ẻ div phụ để 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, kiso 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à kiso đã thiết lập.

2. Phần CSS

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

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ẻ div chứa ảnh nền và thẻ html , body.

– 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:

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ẻ li trước khi thiết lập gía trị mới.

4. Phần mở rộng

Kiso xin tặng các bạn đoạn mã JavaScript kiể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:

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. Kiso hy vọng với những kiến thức đã cung cấp, các bạn sẽ trang bị tốt cho mình những nền tảng tốt nhất cho công cuộc làm web của mình. Chào tạm biệt, hẹn gặp lại các bạn trong các bài viết tiếp theo nhé.

Danh sách file tải về

Tên file tải vềPass giải nén
Parallax – Hoàn thiện bố cục, hiệu ứng và mở rộng kiso.vn hoặc gameportable.net
Nhạc nền kiso.vn hoặc gameportable.net

Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *