Skip to main content

What is the simplest jQuery way to have a "position:fixed" (always at top) div?


I'm relatively new to jQuery, but so far what I've seen I like. What I want is for a div (or any element) to be across the top of the page as if "position: fixed" worked in every browser.



I do not want something complicated. I do not want giant CSS hacks. I would prefer if just using jQuery (version 1.2.6) is good enough, but if I need jQuery-UI-core, then that's fine too.



I've tried $("#topBar").scrollFollow(); <-- but that goes slow... I want something to appear really fixed.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. Using this HTML:

    <div id="myElement" style="position: absolute">This stays at the top</div>


    This is the javascript you want to use. It attaches an event to the window's scroll and moves the element down as far as you've scrolled.

    $(window).scroll(function() {
    $('#myElement').css('top', $(this).scrollTop() + "px");
    });




    As pointed out in the comments below, it's not recommended to attach events to the scroll event - as the user scrolls, it fires A LOT, and can cause performance issues. Consider using it with Ben Alman's debounce/throttle plugin to reduce overhead.

    ReplyDelete
  2. For those browsers that do support "position: fixed" you can simply use javascript (jQuery) to change the position to "fixed" when scrolling. This eliminates the jumpiness when scrolling with the $(window).scroll(function()) solutions listed here.

    Ben Nadel demonstrates this in his tutorial:
    Creating A Sometimes-Fixed-Position Element With jQuery

    ReplyDelete
  3. Beautiful! You're solution was 99%... instead of "this.scrollY", I used "$(window).scrollTop()". What's even better is that this solution only requires the jQuery1.2.6 library (no additional libraries needed).

    The reason I wanted that version in particular is because that's what shipps with MVC currently.

    Here's the code:

    $(document).ready(function() {
    $("#topBar").css("position", "absolute");
    });

    $(window).scroll(function() {
    $("#topBar").css("top", $(window).scrollTop() + "px");
    });

    ReplyDelete
  4. For anyone still looking for an easy solution in IE 6. I created a plugin that handles the IE 6 position: fixed problem and is very easy to use: http://www.fixedie.com/

    I wrote it in an attempt to mimic the simplicity of belatedpng, where the only changes necessary are adding the script and invoking it.

    ReplyDelete
  5. In a project, my client would like a floating box in another div, so I use margin-top CSS property rather than top in order to my floating box stay in its parent.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...