Elemen-DIV Diam setelah discroll atau Menu Melayang
Banyak situs menerapkan menu sticky, termasuk 'Header' situs yang sedang Anda baca ini. Saya sendiri tidak menemukan padanan kata sticky ini dalam bahasa Indonesia. Saya menyamakan sticky ini dengan melengket atau melekat. Menu melengket diterapkan agar memudahkan pengunjung mengeksplor situs tanpa harus bolak-balik menggulung (scroll) laman. Menu akan tetap lengket meskipun laman digulung jauh ke bawah.
 
Bagaimana membuatnya? Salin jQuery berikut dan letakkan di atas tag </body>.
Sesuaikan nama elemen yang akan dibuat sticky. Dalam contoh ini elemen yang saya sticky adalah 'menu'.

<script type='text/javascript'>
//<![CDATA[
$(document).ready(function() {
    // Menentukan elemen yang dijadikan sticky yaitu .menu
    var stickyNavTop = $('.menu').offset().top; 
    var stickyNav = function(){
        var scrollTop = $(window).scrollTop();  
        // Kondisi jika discroll maka menu akan selalu diatas, dan sebaliknya        
        if (scrollTop > stickyNavTop) { 
            $('.menu').css({ 'position': 'fixed', 'top':0, 'z-index':9999 });
        } else {
            $('.menu').css({ 'position': 'relative' });
        }
    };
    // Jalankan fungsi
    stickyNav();
    $(window).scroll(function() {
        stickyNav();
    });
});
//]]>
</script>

Bagaimana jika saya ingin menu itu muncul hanya setelah discroll? Jika tidak discroll menu tersebut tersembunyi.

Memunculkan/menampilkan Menu setelah discroll

Gunakan jQuery berikut untuk memunculkan/menampilkan menu hanya setelah halaman discroll.

<script type='text/javascript'>
//<![CDATA[
$(document).ready(function() {
    // Menentukan elemen yang dijadikan sticky yaitu .menu
    var stickyNavTop = $('.menu').offset().top; 
    var stickyNav = function(){
        var scrollTop = $(window).scrollTop();  
        // Kondisi jika discroll maka menu akan selalu diatas, dan sebaliknya        
        if (scrollTop > stickyNavTop) { 
            $('.menu').css({ 'position': 'fixed', 'top':0, 'z-index':9999 });
        } else {
            $('.menu').css({ 'position': 'absolute', 'top':-9999, 'left':-9999 });
        }
    };
    // Jalankan fungsi
    stickyNav();
    $(window).scroll(function() {
        stickyNav();
    });
});
//]]>
</script>

 
Selamat belajar :)

Post a Comment

Author Name

{picture#https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjf6jBcOMlHfM5ckfOgfHgF8S3bcBlF1LLqvB5pLRwZFNY60xj94s7LKuyBAjIj9ETnBiRbAAM0lUTJq6WH6R2VGBblJW6g3YdZ1YKPwXudeTCgq8x6WXC0ojTIaR3FzDET1F-66YeCgdRU/s1600/Turiswan.png} " Jangan berharap sesuatu yang besar dengan tiba-tiba, mulailah dari yang terkecil dan gapailah semua harapan dengan sungguh-sungguh disertai kerjakeras dan do'a. " {facebook#http://www.facebook.com/turiswan22gtr} {twitter#http://twitter.com/turiswan2298} {google#https://plus.google.com/u/0/106442727760269580008/posts} {pinterest#http://www.pinterest.com/turiswan} {youtube#http://www.youtube.com/channel/UCJL-dLtQyZ_uQx7B3-vThjA?sub_confirmation=1} {instagram#http://instagram.com/turiswan2298}

Sevida Premium Responsive Magazine Blogger Template

Sevida Premium Responsive Magazine Blogger Template Sevida is the premium responsive blog and magazine template for Blogger fans. This template shows you how cool and flexible Blogspot magazine template can be.

Maxxiz - Responsive Magazine/News Blogger Template

Maxxiz - Responsive Magazine/News Blogger Template Maxxiz a blogger theme with responsive layout. Its High User Friendly Blogger Template. Its design simple and clean and perfect for magazine or portofolio websites. With an attractive color combination and impressed professionals and has many features.

Adamz Premium Responsive Blogger Template

Adamz Premium Responsive Blogger Template Adamz a blogger theme with responsive layout. This theme has a design that is perfect for magazine websites. With an attractive color combination and impressed professionals and has many features.

Ijonkz - Responsive Magazine/News Blogger Template

Ijonkz - Responsive Magazine/News Blogger Template Ijonkz a blogger theme with responsive layout and two style Boxed or Full width. Its High User Friendly Blogger Template. Its design simple and clean and perfect for News, Magazine or portofolio websites. With an attractive color combination and impressed professionals and has many features.

Rifqiy - Responsive Magazine/News Blogger Template

Rifqiy - Responsive Magazine/News Blogger Template Rifqiy a blogger theme with responsive layout. This theme has a design that is perfect for magazine or portofolio websites. With an attractive color combination and impressed professionals and has many features.

Syahira Premium Responsive Blogger Template

Syahira Premium Responsive Blogger Template Syahira a blogger theme with responsive layout. Its High User Friendly Blogger Template. Its design simple and clean and perfect for magazine or portofolio websites. With an attractive color combination and impressed professionals and has many features.

Contact Form

Name

Email *

Message *

Powered by Blogger.