Membuat Sticky Sidebar Dengan Menggunakan Javascript

Hallo teman-teman Source Code Aplikasi. Kembali lagi dengan saya di kursus Web Design. Kali ini saya akan membahas mengenai cara Membuat Sticky Sidebar Dengan Menggunakkan Javascript. Sticky yakni posisi tetap yang kita bikin kepada suatu faktor atau objek meskipun menjalankan scroll kebawah di suatu halaman laman. Posisi ini biasanya digunakan bagi fitur di suatu laman ataupun sidebar.



Bagi definisi sidebar itu sendiri yakni suatu faktor atau objek yang berada di posisi kiri atau kanan dengan berisikan link navigasi dengan ukuran lebar kecil. Nah bagi dapat Membuat Sticky Dengan Menggunakkan Javascript kita terlebih dahulu mesti membangun struktur htmlnya dan berikut scriptnya ya teman-teman.



<!DOCTYPE html>
<html>
<head>
<meta name=viewport content=width=device-width, initial-scale=1>
<title>Membangun 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>Membangun Sticky Sidebar Dengan Menggunakkan Javascript</h2>
<p>Scroll kebawah bagi 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>


Seandainya telah selesai karenanya kita seketika saja membangun javascriptnya bagi dapat membangun posisi sticky di sidebar yang telah 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>


Bagi menjalankan sticky sidebar saya menggunakkan cara add dan remove di class sticky. Sehingga ketika kita scroll ke bawah karenanya secara otomatis posisinya akan tetap di atas dengan jarak topnya kosong.



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




Sumber https://kursuswebsite.org

Popular posts from this blog

Perbedaan Antara Keyup dan Keydown Pada jQuery

Membuat Table Warna Berselang dengan PHP