views:

351

answers:

3

I have a web page that displays a long line graph inside a div with overflow-x: scroll. This works well as a web page allowing the use to scroll back and forward through the graph.

However, when printing the page the scroll position is reset to zero. Is there a way to overcome this?

+2  A: 

I think you're going to have to specify an alternate CSS for printing where you somehow need to remove the overflow:

<link rel="stylesheet" type="text/css” href="sheet.css" media="print" />

However, maybe there is an approach with JavaScript or even Flash? If I understand correctly, you only want to have a part of the graph printed (the one "selected" by the user?) and not the full one? I'm pretty sure that's not possible with plain HTML/CSS, but I strongly believe that Flash or maybe JavaScript/AJAX (to only load a part of the image at a time) can solve it.

Michael Stum
A: 

A simple approach would be to have some javascript which posts back to your page with the user's selected scroll position on a link saying something like 'setup for printing'. Then the server side returns a page with the graph relatively positioned at the scroll position with overflow:hidden to clip the graph appropriately.

Of course this would not work for users with javascript disabled - if you want to support this you would need the user to specify the scroll position in something like a text input element and submit button which you hid with javascript when enabled.

domgblackwell
+2  A: 

You can't do this in plain CSS -- you will have to reimplement the scrolling using your Javascript UI library of choice to get what you want.

The user state of the scrollbar isn't used when printing (think about it, if you're scrolled 3 screens down a page and hit "print" does it make sense for the browser to only print the part of the document that's in your window at the time?). However, if you use JS, which actually manipulates the DOM (i.e. sets the x-position offset to -293 if the person has scrolled right 293 pixels, just like style="left: -293px; overflow: hidden;" in CSS), then it will show up as such in printed documents.

My suggestion is, unless the graphs are very wide, just skip all of this nonsense and use a printer stylesheet with width: 100% for the graph's <div> so the graph just shrinks to page width.

joelhardi