Simulate position:fixed in jQuery
Asked Answered
T

4

12

I have a header that is larger than the width of the page so that I can't use position:fixed to set him on top of the page, because I absolutely need to be able to scroll horizontally. I don't think there is a CSS solution for this.

I made a sample of code to try to reproduce the effect of position:fixed, but there are undesired jumps. My code is as following :

$(window).scroll(function() {
            var y = $(window).scrollTop();
            $("#headertable").css('top', y+175);
});

Is there a way to make it really attached, like position:fixed? (Curiously, it is better displayed right now in IE than in FF, because it doesn't have this "jump" effect)

Please find an example here: http://jsbin.com/eyuya/7. The first table is with position:fixed, the other uses my code. That is the jumping effect I'm trying to avoid if there is a solution.

Edit:

Still haven't found a satisfying solution, I think I will use this in the end, because the site is meant to be used on IE and it doesn't seem like a miracle solution exists to attach a div to the viewport, and be able to scroll horizontally. I'm starting a bounty in case of somebody ran into this problem before and found a good solution.

Thanks for the people who already tried to answer this not as simple as it looks problem ;)

Tubing answered 7/7, 2010 at 7:55 Comment(2)
I don't understand what you are trying to do. Can you explain more?Christianna
I would like to have position:fixed but only vertical, that is to say I need to scroll horizontally which is not allowed by position :fixedTubing
M
8

You can wrap the element with a static positioned DIV to get scrollbars and then listen for the window scroll and position the fixed header according to the scrollLeft value:

var elem = $('#headertable');
var win  = $(window);
var wrap = $('<div>').css({
    width: elem.width(),
    height: elem.height()
});
elem.wrap(wrap);
win.scroll(function() {
    elem.css('left', win.scrollLeft()*-1);
});

Seems to work in IE/FF/Chrome:

http://jsbin.com/efuya3

Myrnamyrobalan answered 19/7, 2010 at 11:7 Comment(1)
I needed this solution to work with a header that's width was set to 100%, with a min-width set. With as is, issues would occur when the window was refreshed above the min-width then resized down as the wrap would be too large, causing a white gap to the right of the page when scrolled. To fix this, I edited your jsbin with my example: jsbin.com/efuya3/17 I hope this helps someone else who may stumble upon this gem. Thanks !Cholon
L
3

You can create a dummy visibililty:hidden element with the same width as that of the position:fixed element. Here is a sample:

<html>
<head>
  <script src="http://www.google.com/jsapi"></script>
  <script>
    google.load("jquery", "1.4.2");
    google.setOnLoadCallback(function() {
      $(document).ready(function(){
        var dummy = $('<div>dummy</div>').width( $('#header').width() + 'px').css('visibility','hidden');
        $('body').append(dummy);
      });
    });
    </script>
</head>
<body>
<div id="header" style="width:2000px;background:red;position:fixed;top:0;height:20px;">
    This is aheder with fixed position
</div>

<div style="height:1200px;background:green;">

</div>
</body>
</html>
Loreeloreen answered 7/7, 2010 at 8:30 Comment(3)
Nice this looks like exactly what I need, I'm testing it, and will accept if it's ok. Thanks so much, I've been looking all over the internet for a decent solution, but only the best website for programmers can provide the right and the best answers ;)Tubing
Well it's weird it doesn't seem to do the trick with FF : see jsbin.com/awawa3 Have you got an explaination? ThanksTubing
The link I provided works on FF 3.6 for you? It's weird, I also have this and if I scroll horizontally, it has the same behaviour as simple position:fixed . In IE7/8 it's even worse cause it has the same behaviour as position:absolute. Don't know what could be the problem.Tubing
C
1

http://fixedheadertable.com/demo/multipletablesdemo.html

try this one instead :)

Coffeecolored answered 8/7, 2010 at 4:8 Comment(1)
Hey, thanks for the advice but I ve already seen this one, ans it doesn't quite fit my expectations as the scrollbar is on the body, and I need to scroll with the right bar, because I already don't have a lot of space for displaying all the columns. I think I'll implement your previous solution or jerjer one, because it is a company software, so they won't change from Internet Explorer in a while unfortunately :sTubing
C
0

you could create a wrapper div and then specify a width on the div

eg width: 150%; or width; 2000px;

depending on your requirements

why do you need to scroll?

Coffeecolored answered 7/7, 2010 at 8:0 Comment(5)
This is my thead which is position :fixed. This is a table with 20 columns, I have the ability to hide some, but when I need to view the entire I need to scroll horizontally and see all the column titles. I'm testing your solution and I get back to you.Tubing
Unfortunately it doesn't work, setting a width to a position:fixed element doesn't allow horizontal scroll, I'll try to make an example to show exactly what I have now and what bothers me with my solution. Thanks for the answser anyway ;)Tubing
i may be wrong, but, you want the header to be fixed at the top of the page, but when you scroll left/right it should just scroll so you can see other columns? i may be wrong but try this jsbin.com/eyuya/4Coffeecolored
Thanks for your answer, well it seems like it only works with IE, I don't know why. If it was compatible with FF it would be perfect.Tubing
yes, unfortunatially firefox's javascript engine lags a bit on returning the scroll event, which is why it appears to jump.Coffeecolored

© 2022 - 2024 — McMap. All rights reserved.