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

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

  1. adisp007 Avatar

    Mantap………. Thanks………

    1. Dallas Brooks Avatar

      Solid content. Rhyme Game helped me beat writer’s block with its rhyming challenges.

  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

  18. Lewiskenna Avatar
    Lewiskenna

    I am glad I found this page because the information provided here is exactly what I was searching for. It is a well-written, universally applicable summary with excellent clarity.

    nuevo casino bono sin deposito

  19. GichardTak Avatar
    GichardTak

    I was really impressed by how clearly this information was presented. It is not always easy to find such a well-organized and neutral overview online, so I wanted to thank you for putting this together and making it so accessible to everyone reading.

    在线购买他达拉非片用于肛交XXX色情

  20. Sidney Turner Avatar

    dulu saya juga coba-coba efek kayak gini di sebuah proyek kecil, tapi pas nyoba di mobile scrolling jadi agak lag. Apakah ada trik tambahan biar animasi transisi dari absolute ke fixed nggak terlalu kasar?

  21. Earnestspami Avatar
    Earnestspami

    This is an incredibly useful and thoughtful piece of writing. I appreciate how you avoided unnecessary jargon, keeping the focus entirely on the most important details here.

    Watch sexual porno video xxx sex adults site

  22. Riley Adams Avatar

    Awesome write-up! I love the points you made. If you’re into casual games, you might also enjoy head-football – the best head football action on the web.

  23. bowie Avatar

    Simple and effective jQuery code to replicate that Facebook sticky sidebar.
    homedesignai.co

  24. OLaneFoext Avatar
    OLaneFoext

    The tone of this post feels positive and well-measured, helping create a positive atmosphere for discussion and meaningful interaction online.

    https://hubdeloeste.com

  25. Earnestspami Avatar
    Earnestspami

    The structure of this post makes the ideas clear and helps keep the overall discussion engaging for readers.

    https://openhousecumbria.com

  26. gesture synth Avatar

    The scroll-triggered fixed positioning trick is really neat, and the code is clean. I’ve used a similar approach for sticky headers, but I never thought about applying it to a floating widget. By the way, if you enjoy experimenting with interactive web effects, you might also like this free online musical instrument called gesture synth that uses hand tracking to control sound in the browser.

  27. Earnestspami Avatar
    Earnestspami

    I like how this post explains the topic in a clear way without making the discussion feel too heavy to understand for readers.

    https://ruitenburgrunmaasdijk.nl/

  28. Earnestspami Avatar
    Earnestspami

    Finding a post that is both detailed and completely unbiased is quite rare. Your methodical approach to outlining these details makes this a highly valuable resource for everyone.

    uk casinos not on gamstop,

  29. charlotte Avatar

    Artikel yang sangat bermanfaat. Teknik manipulasi class `menu-fixed` saat scroll ini memang cara yang efisien untuk meningkatkan UX agar navigasi tetap terjaga. Penjelasannya mudah diikuti, terutama bagian penggunaan `offset().top`. Sangat relevan untuk pengembangan tool interaktif seperti Body Visualizer yang juga mengutamakan visualisasi instan berdasarkan input dinamis pengguna.

  30. emailtoolbox Avatar

    Good stuff — exactly the kind of resource I look for. Your take on “Mengubah Element Position secara Dinamis dengan” especially resonated. I also rely on a batch download helper for the practical side: a batch download helper Cheers!

  31. Budi Santoso Avatar

    Artikel yang sangat bermanfaat dan informatif! Terima kasih telah berbagi. Bagi yang tertarik dengan pengeditan visual dan gaya busana berbasis AI, AI Fashion Changer adalah alat kreatif yang sangat layak untuk dicoba.

  32. Robin Avatar

    The article’s breakdown of using jQuery to toggle an element between absolute and fixed positioning once it crosses the upper margin is a practical approach for persistent navigation. Integrating that scroll-boundary check keeps the layout predictable, which matters when visitors simply need to watch fmovies online without losing context. It’s a straightforward reminder that minimal CSS adjustments can significantly improve interface stability.

  33. Dear Passengers Avatar

    The idea of balancing physics-based chaos while managing passenger demands sounds like a perfect recipe for hilarious co-op sessions. If your group enjoys games where planning inevitably falls apart, checking out the emerging “co-op flight attendant” genre is worthwhile. A title like Dear Passengers Game seems to capture that exact vibe with its risky cargo and service mechanics.

  34. ShaneFoext Avatar
    ShaneFoext

    This post serves as a wonderful example of how to deliver information properly. I truly value the unbiased approach you used here, as it ensures that the core message is communicated smoothly without relying on any unnecessary confusing terminology or distracting personal interpretations.

    casino argent reel en ligne

  35. Lewiskenna Avatar
    Lewiskenna

    The concise yet complete nature of this article makes it a fantastic resource, and I truly value the systematic approach you took to ensure every single point was covered in a logical and easy-to-understand manner for the readers.

    https://cybrexsport.com.ua/chy-dopomahaie-sylikahel-vid-zapotivannia-far.html

  36. Timsothytof Avatar
    Timsothytof

    The overall flow of this post works really well because each idea connects naturally and helps keep the reader interested throughout the discussion.

    instant withdrawal online casino

  37. Wailian Avatar

    Clear explanation, thanks. The .css() approach is fine for one-off tweaks, but for anything repeated I’ve found toggling a class and keeping the positioning in CSS much easier to maintain — especially when the same element needs different positions on mobile and desktop. Your point about caching the jQuery selector is spot on too; I’ve seen scripts slow down badly from re-querying the DOM inside loops.

  38. Reference2Video Avatar
    Reference2Video

    The transition from menu-absolute to menu-fixed is a clear, practical example of improving navigation during scrolling. It also makes me think of Reference2Video: the same idea of using reference media to preserve a visual element’s position or style could help create consistent UI-focused images and videos from prompts.

    reference to video generator

  39. 3d convert Avatar

    As someone who regularly converts 3D models for print, I appreciate tools that skip the cloud. This triathlon update is a solid reminder that keeping things local—whether it’s your game assets or a 3d converter3d convert—saves time and protects your work. No uploads means no waiting around.

  40. lisheng Avatar

    Great tutorial on dynamic jQuery positioning — very clear and practical! It reminded me of another creative web experiment I recently discovered: GestureSynth.me, a browser-based instrument that turns hand gestures into music using your webcam — all processed locally, no sign-up or install needed. If you’re into interactive web tech like jQuery or Web Audio, you’ll love exploring it too: Check out GestureSynth

  41. lisheng Avatar

    Great tutorial on dynamic jQuery positioning — super helpful for sticky menus! If you’re also working with images and need smart, no-signup editing (background removal, relighting, restoration, etc.), check out ImgLoop.ai — a free, privacy-first AI photo editor that runs entirely in-browser. No uploads leave your device until you explicitly request AI processing. Try it with 2 lifetime credits:
    https://imgloop.ai/

  42. Francis Avatar

    Great tutorial on dynamic jQuery positioning! This reminds me of another creative web experiment I recently discovered: GestureSynth.me — a fully browser-based instrument that turns hand gestures into music using your webcam, with zero installation or data upload. It’s built entirely with Web Audio and local hand tracking, and even includes a 4-track sequencer and WAV export. If you’re into interactive front-end experiments like this scroll-fixed menu, you’ll love exploring how it maps movement to sound in real time. Check it out here:
    GestureSynth.me

  43. Eric Avatar

    Great tutorial on dynamic element positioning with jQuery! This fixed/floating menu technique is still super useful for modern web projects. If you’re into interactive physics simulations and creative browser-based experiments, check out Sandboxels.cc — a free, open falling-sand simulator with 85+ materials, circuit building, fluid dynamics, and local save support. Perfect for devs and educators who love hands-on, visual coding exploration.

  44. Try On Up Avatar
    Try On Up

    Menarik melihat menu tetap menempel saat halaman digulir, dengan pergantian kelas berdasarkan posisi awal elemen. Pola dinamis seperti ini juga berguna untuk menjaga panel perbandingan outfit tetap mudah dijangkau saat meninjau beberapa pratinjau pakaian.

    AI clothes changer

  45. face rating Avatar

    I found the point about Baru-baru ini situs jejaring sosial Facebook menambah sebuah widget kecil di sebelah kanan atas pada halaman Home masing-masing user, yang berisi akti useful. A related Face-Rating may be useful as well.

Leave a Reply

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