Hallo teman-teman Dumet School. Kembali lagi dengan saya di kursus Web Design. Kali ini saya akan membahas tentang cara Membuat Sticky Sidebar Dengan Menggunakkan Javascript. Sticky adalah posisi tetap yang kita buat terhadap suatu elemen atau objek meskipun melakukan scroll kebawah pada suatu halaman website. Posisi ini biasanya digunakan untuk menu pada suatu website ataupun sidebar.

Untuk definisi sidebar itu sendiri adalah suatu elemen atau objek yang berada di posisi kiri atau kanan dengan berisikan link navigasi dengan ukuran lebar kecil. Nah untuk bisa Membuat Sticky Dengan Menggunakkan Javascript kita terlebih dahulu harus membuat struktur htmlnya dan berikut scriptnya ya teman-teman.

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Membuat Sticky Sidebar Dengan Menggunakkan Javascript</title>
  <style>
    body {
      margin: 0;
      padding: 0;
    }
    .container{
      margin: auto;
      width: 900px;
    }

    .header {
      background-color: #f1f1f1;
      padding: 30px;
      text-align: center;
    }

    .content {
      padding: 16px;
      overflow: hidden;
    }
    .left{
      width: 150px;
      float: left;
      background: salmon;
      padding: 10px;
    }
    .left ul{
      list-style: none;
      margin:0 !important;
      padding: 0px !important;
    }

    .right{
      width: 685px;
      float: right;
      font-size: 30px;
    }

    .sticky {
      position: fixed;
      top: 0;
    }

    .sticky + .content {
      padding-top: 60px;
    }
    </style>
</head>
<body>
  <div class="container">

    <div class="header">
      <h2>Membuat Sticky Sidebar Dengan Menggunakkan Javascript</h2>
      <p>Scroll kebawah untuk melihat efek sticky</p>
    </div>

    <div class="content">
      <div class="left" id="left">
        <h3>Sticky Sidebar</h3>
        <ul>
          <li><a class="active" href="javascript:void(0)">Home</a></li>
          <li><a href="javascript:void(0)">News</a></li>
          <li><a href="javascript:void(0)">Contact</a></li>
        </ul>
      </div>
      <div class="right">
      <h3>Sticky Sidebar</h3>
        <p>Where does it come from?</p>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
        <p>Where does it come from?</p>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
        <p>Where does it come from?</p>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
      </div>  
    </div>
  </div>
</body>
</html>

Jika sudah selesai maka kita langsung saja membuat javascriptnya untuk bisa membuat posisi sticky pada sidebar yang sudah kita tentukan.

<script>
window.onscroll = function() {myFunction()};

var left = document.getElementById("left");
var sticky = left.offsetTop;

function myFunction() {
  if (window.pageYOffset >= sticky) {
    left.classList.add("sticky")
  } else {
    left.classList.remove("sticky");
  }
}
</script>

Untuk menjalankan sticky sidebar saya menggunakkan metode add dan remove pada class sticky. Sehingga ketika kita scroll ke bawah maka secara otomatis posisinya akan tetap di atas dengan jarak topnya kosong.

Demikian artikel tentang cara Membuat Sticky Sidebar Dengan Menggunakan Javascript. Semoga bermanfaat dan selamat mencoba ya teman-teman.