views:

81

answers:

3

Hi!

I've a table that is very large. So I decided to put a horizontal scrollbar that is placed on the bottom of the table.
But I would like this scrollbar to be also on top on the table.

What I have in the template is this:

<div style="overflow:auto; width:100%; height:130%">
<table id="data" style="width:100%">

Is this possible to do in html? Or I have to try javascript or jquery?

Thank you!

A: 

As far as I'm aware this isn't possible with HTML and CSS.

MrMisterMan
well this is actually what I thought.. But I would like to known if anyone has done this and the best way to do this..
Pat
For HTML and CSS, there is no best way. It is simply not possible. You need to use other techniques, like javascript, to get this kind of functionality.
Justus Romijn
can you give some tips to start looking for?
Pat
@Pat I can't comment on @StanleyH's excellent answer for some reason so I'm responding here. The reason it says $ is not defined is probably because you haven't included the jQuery library in your document. That code needs this to function.
MrMisterMan
I've put at the beginning of the page <script type="text/javascript"></script>
Pat
A: 

Have you tried just making the <tbody> scroll?

CSS

#data { width: 99%; }
#data tbody { height: 500px; overflow: scroll; }

HTML

<table id="data">
 <thead> <!-- this part doesn't scroll with the tbody -->
  <tr>...</tr>
 </thead>
 <tbody>
 ...
 </tbody>
 <tfoot> <!-- this part doesn't scroll with the tbody -->
  <tr>...</tr>
 </tfoot>
</table>
fudgey
well I've more information on that page but only the table is too large.. So I would like to apply this only to the table
Pat
The CSS only applies to the table. What it does is makes the information inside the table scrollable, so the scroll bars inside the table are always visible. I just realized you also mean it's very wide... I'll edit my answer.
fudgey
well perhaps I didn't get myself clear. I already have the table scrollable. at the bottom. But I would like to have one bar at the top of the table and another at the bottom (that I already have). I think this basically does the same that I have, scroll bar at the bottom. if I'm not wrong :)
Pat
Yeah it wasn't clear if you wanted the scroll bar to move the table horizontally, vertically, or both.
fudgey
+3  A: 

Here's how to simulate a second horizontal scrollbar on top of an element:

Put a "dummy" div above the element that has horizontal scrolling, just high enough for a scrollbar. Then attach handlers of the "scroll" event for the dummy element and the real element, to get the other element in synch when either scrollbar is moved. The dummy element will look like a second horizontal scrollbar above the real element.

For a live example, see: http://jsfiddle.net/TBnqw/1/

Here's the code:

HTML:

<div class="wrapper1">
<div class="div1">
</div>
</div>
<div class="wrapper2">
<div class="div2">
aaaa bbbb cccc dddd aaaa bbbb cccc dddd aaaa bbbb cccc dddd aaaa bbbb cccc dddd aaaa bbbb cccc dddd aaaa bbbb cccc dddd aaaa bbbb cccc dddd
</div>
</div>

CSS:

.wrapper1, .wrapper2 {width: 300px; overflow-x: scroll; overflow-y:hidden;}
.wrapper1 {height: 20px; }
.wrapper2 {height: 200px; }
.div1 {width:1000px; height: 20px; }
.div2 {width:1000px; height: 200px; background-color: #88FF88; overflow: auto;}

JS:

$(function(){
$(".wrapper1").scroll(function(){
    $(".wrapper2")
        .scrollLeft($(".wrapper1").scrollLeft());
});
$(".wrapper2").scroll(function(){
    $(".wrapper1")
        .scrollLeft($(".wrapper2").scrollLeft());
});
});
StanleyH
really great answer! thanks a lot! :) so I tried to use your example, but the bar on top doesn't work, and when I tried to increase the width, the scrolling bar disappear. any idea why?
Pat
I get an error: $ is not defined
Pat
`$` is for jQuery. Just load it in the `<head>` of your page like this: `<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>`
fudgey
Did you get it to work? For more on jQuery, see http://api.jquery.com/scrollLeft/ (Of course, you can do it without jQuery by attaching onscroll handlers directly.) For graceful degradation for users with no JS, you can add the dummy div to the DOM by JS.
StanleyH
yes I got it to work, I had only add <script type="text/javascript" src="path"></script> to <head>. So everytime I add jquery I had to add the one that @fudgey add? Sorry, javascript and jquery are still kind of chinese to me
Pat