Cara membuat menu scroll di html

https://codingrakitan.blogspot.com/2020/09/cara-membuat-menu-auto-fixed-ketika-di.html

Cara membuat menu scroll di html

Show

 

Auto Fixed disini biasanya banyak di terapkan pada web-website yang ingin memiliki tampilan atau design web yang lebih menarik. Jadi simpelnya ketika kita melakukan scroll ke bawah menu yang tadinya posisinya tetap akan berubah menjadi fixed di atas atau di bagian tertentu yang sudah di tentukan.

Untuk membuat tampilan website anda seperti ini sangatlah mudah anda bisa memanfaatkan fitur javascript. Sebagai contoh disini kita akan membuatnya dengan tampilan html sederhana seperti berikut :
 

Cara membuat menu scroll di html


 

Ketika anda melakukan aksi scroll maka tampilan akan menjadi seperti berikut :



Cara membuat menu scroll di html


Kode cara Membuat Menu Auto Fixed Ketika di Scroll

<!DOCTYPE html>
<html>
<head>
<title>Coding Rakitan</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style type="text/css">
body{
height: 2000px;margin: 0px;padding: 0px;
}.bl{
height: 200px;
}
.menu{
height: 50px;
background: #001b6a;
width: 100%;
}.fx{
top: 0px;
position: fixed;
}.d-flex{
display: flex;
height: 100%;
}.menu a{
margin-right: 20px;
margin-bottom: auto;
margin-top: auto;
color: white;
font-family: sans-serif;
}.m-auto{
margin: auto;
}h1{
font-size: 83px;
font-family: sans-serif;
}
</style>
</head>
<body >
<div class="bl">
<div class="d-flex">
<h1 class="m-auto">Coding Rakitan</h1>
</div>
</div>
<div class="menu" id="menu">
<div class="d-flex">
<a>Menu 1</a>
<a>Menu 2</a>
<a>Menu 3</a>
<a>Menu 4</a>
</div>
</div>
<br>
<div class="d-flex">

<img class="m-auto" style="margin-top: 0px;" src="https://1.bp.blogspot.com/-EICGWCsAdt0/X14oFnUYi5I/AAAAAAAADuo/AsdycoKUhwQF9SMo-9Tp0XhcDE0YH-OlQCPcBGAYYCw/s2518/Screenshot_2020-09-13%2BCODING%2BRAKITAN%2BINSPIRASI%2BCODING%2BTERUPDATE%2Bandroid%2Bstudio%2Blaravel%2Bphp.png" style="width: 700px;">
</div>
</body>
<script type="text/javascript">
$(function(){

var menu = $('#menu');
pos = menu.offset();

$(window).scroll(function(){
if($(this).scrollTop() > pos.top+menu.height()){
menu.addClass('fx');
} else if($(this).scrollTop() <= pos.top ){
menu.removeClass('fx');
}
});

});
</script>
</html>
Kode yang perlu anda perhatikan ada pada :

<script type="text/javascript">
$(function(){

var menu = $('#menu');
pos = menu.offset();

$(window).scroll(function(){
if($(this).scrollTop() > pos.top+menu.height()){
menu.addClass('fx');
} else if($(this).scrollTop() <= pos.top ){
menu.removeClass('fx');
}
});

});
</script>
Dimana pada kode javascript di atas menginstruksikan ketika terjadi scroll kebawah dan nilai over dari scroll ini lebih besar dari over menu maka tambahkan class fx pada menu, yang mana menu fx ini akan membuat position menjadi fixed dan berada di atas atau top 0px. Sedangkan bila kebalikannya bila terjadi scroll ke atas dan nilainya lebih kecil atau sama dengan menu over maka hapus kembali class fx yang membuat menu kembali seperti semula.

Bagikan artikel ke:

Facebook Google+ Twitter

Add scroll-behavior: smooth to the element to enable smooth scrolling for the whole page (note: it is also possible to add it to a specific element/scroll container):


Browser Support

The numbers in the table specify the first browser version that fully supports the scroll-behavior property.

Propertyscroll-behavior61.079.036.014.048.0



Cross-browser Solution

For browsers that do not support the scroll-behavior property, you could use JavaScript or a JavaScript library, like jQuery, to create a solution that will work for all browsers:

Example


Try it Yourself »

Tip: Read more about the scroll-behavior property in our CSS Reference: CSS scroll-behavior Property.