Mengubah Element Position secara Dinamis dengan jQuery

jQuery fixed-element behavior
Baru-baru ini situs jejaring sosial Facebook menambah sebuah widget kecil di sebelah kanan atas pada halaman Home masing-masing user, yang berisi aktifitas terbaru dari teman-teman kita. Uniknya, ketika saya menggulir halaman ke bawah, alih-alih menghilang dari layar monitor, widget tersebut malah menempel pada menu bar biru yang menjadi ciri khas Facebook.

Meskipun beberapa orang menganggap fitur tersebut tidak penting, tapi efek yang dihasilkannya sangat keren. Sebenarnya saya pernah menggunakan efek seperti itu pada beberapa proyek dengan bantuan jQuery. Di bawah ini adalah kode yang saya gunakan untuk menghasilkan efek tersebut.

$(document).ready(function() {

  var menu = $("#menu");
  var menuOriTop = menu.offset().top;
  var view = $(window);

  view.bind("scroll resize", function(){

    var menuViewTop = view.scrollTop();
    if ((menuViewTop > menuOriTop) && !menu.is(".menu-fixed")){

      menu.removeClass("menu-absolute").addClass("menu-fixed");
    }

    else if ((menuViewTop <= menuOriTop) && menu.is(".menu-fixed")){
 
      menu.removeClass("menu-fixed").addClass("menu-absolute");
    }
  });
});

Penjelasannya, kode jQuery di atas akan memeriksa apakah elemen menu telah digulir melewati batas atas mula-mula, jika sudah melewati, maka atribut position dari elemen menu akan diubah menjadi fix. Sebaliknya, jika elemen menu kurang dari batas atas mula-mula, maka atribut position akan diubah menjadi absolute.

Untuk lebih memahami, akan saya berikan kode HTML dan CSS sederhana. Semoga cukup untuk menambah koleksi potongan kode Anda 😉

<!DOCTYPE HTML>
<html>
<head>
<title>Float Menu</title>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%20type%3D%22text%2Fcss%22%3E%0A%20%20%23header%20%7B%20height%3A%20200px%3B%20width%3A800px%3B%20background-color%3A%20%233333FF%3B%20%7D%3Cbr%20%2F%3E%0A%20%20.sidebar%20%7B%20float%3A%20left%3B%20width%3A%20110px%3B%20border%3A%201px%20solid%20%23e6e6e6%3B%20%7D%3Cbr%20%2F%3E%0A%20%20.content%20%7B%20float%3A%20left%3B%20height%3A%202000px%3B%20width%3A%20690px%3B%20margin-right%3A10px%3B%20background-color%3A%2300FF00%20%7D%3Cbr%20%2F%3E%0A%20%20%23menu%20%7B%20width%3A100px%3B%20height%3A200px%3B%20background-color%3A%20%23F0F0F0%3B%20%7D%3Cbr%20%2F%3E%0A%20%20div.menu-absolute%20%7B%09position%3A%20absolute%3B%20top%3A%20210px%3B%20%7D%3Cbr%20%2F%3E%0A%20%20div.menu-fixed%20%7B%20solid%20%23E0E0E0%3B%09top%3A%2010px%3B%20position%3A%20fixed%3B%20%7D%3Cbr%20%2F%3E%0A%20%20.footer%20%7B%20clear%3A%20both%3B%20width%3A%20800px%3B%20background-color%3A%23FF3399%20%7D%3C%2Fp%3E%0A%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object mce-object-style" width="20" height="20" alt="&lt;style&gt;" />

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20src%3D%22http%3A%2F%2Fcode.jquery.com%2Fjquery-latest.js%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object mce-object-script" width="20" height="20" alt="&lt;script&gt;" />
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%3Cbr%20%2F%3E%0A%24(document).ready(function()%20%7B%3C%2Fp%3E%0A%3Cp%3E%20%20var%20menu%20%3D%20%24(%22%23menu%22)%3B%3Cbr%20%2F%3E%0A%20%20var%20menuOriTop%20%3D%20menu.offset().top%3B%3Cbr%20%2F%3E%0A%20%20var%20view%20%3D%20%24(window)%3B%3C%2Fp%3E%0A%3Cp%3E%20%20view.bind(%22scroll%20resize%22%2C%20function()%7B%3C%2Fp%3E%0A%3Cp%3E%20%20%20%20var%20menuViewTop%20%3D%20view.scrollTop()%3B%3Cbr%20%2F%3E%0A%20%20%20%20if%20((menuViewTop%20%3E%20menuOriTop)%20%26%26%20!menu.is(%22.menu-fixed%22))%7B%3C%2Fp%3E%0A%3Cp%3E%20%20%20%20%20%20menu.removeClass(%22menu-absolute%22).addClass(%22menu-fixed%22)%3B%3Cbr%20%2F%3E%0A%20%20%20%20%7D%3C%2Fp%3E%0A%3Cp%3E%20%20%20%20else%20if%20((menuViewTop%20%3C%3D%20menuOriTop)%20%26%26%20menu.is(%22.menu-fixed%22))%7B%0A%20%0A%20%20%20%20%20%20menu.removeClass(%22menu-fixed%22).addClass(%22menu-absolute%22)%3B%0A%20%20%20%20%7D%0A%20%20%7D)%3B%0A%7D)%3B%0A%20%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object mce-object-script" width="20" height="20" alt="&lt;script&gt;" />
</head>
<body>
<div id="header">Header</div>
<div class="content">content here</div>
<div class="sidebar">
<div id="menu" class="menu-absolute">Menu Float</div>

</div>
<div class="footer">footer</div>

</body>
</html>

Comments

19 responses to “Mengubah Element Position secara Dinamis dengan jQuery”

  1. adisp007 Avatar

    Mantap………. Thanks………

  2. Sriyono Semarang Avatar

    nggak diaplikasikan disini juga sekalian nehh 🙂

    1. Sibudi Avatar

      Wah telat banget jawabnya ya 😀
      Tapi sudah lama teknik ini saya aplikasikan di menu sebelah kiri website ini 🙂

  3. […] yang didapat dengan membuat sebuah navigasi tab dengan jQuery adalah halaman tidak perlu dimuat ulang hanya untuk berganti tab yang aktif. Kenapa hal itu bisa […]

  4. kuceng21 Avatar

    Manntab gan, ini yang ane cari, mirip yang di toko bagun nih, tinggal modif dikit jadi dah,,,,

  5. kolonel Avatar
    kolonel

    Sedikit saran ne master…
    kalau bisa sediakan link untuk demonya…
    jadi pengunjung bisa melihat hasilnya bagaimana sebelum dia mengaplikasikannya.
    thanks

    1. Sibudi Avatar

      Terima kasih atas sarannya.
      Tapi karena source code sudah saya sertakan seutuhnya, jadi bisa langsung dicoba sendiri.

  6. OLaneFoext Avatar
    OLaneFoext

    Hi there Dear, are you truly visiting this site regularly, if so afterward you will without doubt obtain good knowledge.
    https://share.google/Skowg82xl3iohlVPW

  7. GichardTak Avatar
    GichardTak

    It’s remarkable in support of me to have a site, which is good in support of my experience. thanks admin
    https://share.google/r4f9uo9t2W4hTqb7d

  8. GichardTak Avatar
    GichardTak

    Right here is the right blog for anybody who wants to understand this topic. You understand so much its almost hard to argue with you (not that I personally would want to…HaHa). You certainly put a brand new spin on a topic which has been written about for a long time. Excellent stuff, just great!
    https://share.google/3GmIn5buJVz1PlwDJ

  9. Juegaenlinea Avatar

    https://juegaenlinea.cat/
    Juegaenlinea se establece en 2026 como una opcion destacada en el mercado colombiano, entregando un entorno seguro y integral para juegos de casino en linea y apuestas en deportes bajo licencia otorgada por la Junta de Control de Juegos de Curazao.
    Creada principalmente dirigida a el audiencia de Latinoamerica, esta alternativa funciona en pesos de Colombia y presenta una extensa oferta de entretenimiento avalada por mas de 35 estudios de software certificados.

  10. CodereMexico Avatar

    https://codere.cat/
    Actualmente, Codere Mexico es un establecimiento digital de apuestas que opera con pleno cumplimiento normativo contando con el permiso de la SEGOB, ofreciendo a los jugadores mexicanos un entorno de juego seguro y fiable donde cada ingreso y retiro de fondos se operan totalmente en MXN, la moneda local mediante el sistema SPEI .

  11. GichardTak Avatar
    GichardTak

    I am leaving a comment just to say how much I appreciated the direct, no-nonsense approach you used to tackle this topic, as it is much better than the overly complicated explanations I usually find on other websites.

    no deposit bonus

  12. StephenLed Avatar
    StephenLed

    The overall tone of this post feels calm and useful at the same time, which helps create a comfortable and interesting reading experience for different audiences.

    купить Empower RF

  13. StephenLed Avatar
    StephenLed

    What stands out to me here is the way the discussion flows so clearly from one point to another, making the post feel interesting, clearly arranged, and comfortable to read from beginning to end.

    купить inmode Diolaze

  14. Dichaelvof Avatar
    Dichaelvof

    This post combines a natural tone with clear structure, making the content pleasant and simple to follow.

    Virsavia косметология

  15. GichardTak Avatar
    GichardTak

    The overall flow of this post feels very natural and nicely structured, because each idea connects naturally with the next one and helps keep the reader engaged without making the discussion feel too heavy or repetitive.

    site de paris sportif hors arjel

  16. Dichaelvof Avatar
    Dichaelvof

    The logical progression of your thoughts throughout this piece really helps in understanding the core message, and I find this level of clarity to be incredibly valuable for anyone reading about this topic online.

    casino en ligne retrait immediat

  17. FobertRaw Avatar
    FobertRaw

    What stands out here is how clearly the discussion flows from one point to another, because the post stays interesting and easy to follow without feeling forced for readers to follow closely.

    meilleur site paris sportif hors arjel

Leave a Reply

Your email address will not be published. Required fields are marked *