Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Get Scrollbar Position

Reply
Thread Tools

Get Scrollbar Position

 
 
Jeff Johnson
Guest
Posts: n/a
 
      01-27-2004
Hi All,

I'm setting a <div> on a page on a lengthy page with a great deal of
text. I want the div to land where the user can see it. I need to
capture the vertical scrollbar position and use that integer to set
the 'Y' coordinate for the div.

Can this be done? If so, how?

If not, any other suggestions?

Thanks,

Jeff
 
Reply With Quote
 
 
 
 
DJ WIce
Guest
Posts: n/a
 
      01-27-2004
: I'm setting a <div> on a page on a lengthy page with a great deal of
: text. I want the div to land where the user can see it. I need to
: capture the vertical scrollbar position and use that integer to set
: the 'Y' coordinate for the div.

let x, y be the mouse position.
let zoom_factor be the % in MSIE that the body content is zoomed
(
document.body.style.setAttribute('zoom', zoom_factor+'%');
).
let 'contextmenu' be the ID of your DIV.

xOff = parseInt(document.getElementById('contenxtmenu').s tyle.width) -
(document.all?document.body.scrollLeft:window.page XOffset);
yOff = parseInt(document.getElementById('contenxtmenu').s tyle.height) -
(document.all?document.body.scrollTop:window.pageY Offset);
document.getElementById("contenxtmenu").style.top =
Math.min(parseInt(y/zoom_factor*100),document.body.clientHeight-yOff)+"px";;
document.getElementById("contenxtmenu").style.left =
Math.min(parseInt(x/zoom_factor*100),document.body.clientWidth-xOff)+"px";


Then your div will be placed next to the mouse, but always fully on screen.
If you don't zoom and don't mind the mouse; replace y/zoom_factor*100
and x/zoom_factor*100 by the prefered y and x location of your
<div> on your page.

Wouter


 
Reply With Quote
 
 
 
 
DU
Guest
Posts: n/a
 
      01-28-2004
DJ WIce wrote:

> : I'm setting a <div> on a page on a lengthy page with a great deal of
> : text. I want the div to land where the user can see it. I need to
> : capture the vertical scrollbar position and use that integer to set
> : the 'Y' coordinate for the div.
>
> let x, y be the mouse position.
> let zoom_factor be the % in MSIE that the body content is zoomed
> (
> document.body.style.setAttribute('zoom', zoom_factor+'%');


zoom is only supported by MSIE 5.5 and MSIE 6.

> ).
> let 'contextmenu' be the ID of your DIV.
>
> xOff = parseInt(document.getElementById('contenxtmenu').s tyle.width) -
> (document.all?document.body.scrollLeft:window.page XOffset);


As written, you will only support backward compatible rendering mode in
MSIE 6 where the root element is not the body. Your script might be
interesting but it has limited scope/support among current browsers.

DU

> yOff = parseInt(document.getElementById('contenxtmenu').s tyle.height) -
> (document.all?document.body.scrollTop:window.pageY Offset);
> document.getElementById("contenxtmenu").style.top =
> Math.min(parseInt(y/zoom_factor*100),document.body.clientHeight-yOff)+"px";;
> document.getElementById("contenxtmenu").style.left =
> Math.min(parseInt(x/zoom_factor*100),document.body.clientWidth-xOff)+"px";
>
>
> Then your div will be placed next to the mouse, but always fully on screen.
> If you don't zoom and don't mind the mouse; replace y/zoom_factor*100
> and x/zoom_factor*100 by the prefered y and x location of your
> <div> on your page.
>
> Wouter
>
>

 
Reply With Quote
 
DJ WIce
Guest
Posts: n/a
 
      01-28-2004
: As written, you will only support backward compatible rendering mode in
: MSIE 6 where the root element is not the body. Your script might be
: interesting but it has limited scope/support among current browsers.
Well, it's tested in Mozilla 1.3 and MSIE 5+ ...
And also om NN 7 and MSIE 6
(see http://www.djwice.com/contextmenu.html )

The zoom_factor value is default set to 100, so when the zoom function is
not implemented, it will not be changed, and the position values stay
(nearly) unchanged.

Wouter


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      01-28-2004
JRS: In article <bv6fdp$ebt$>, seen in
news:comp.lang.javascript, DJ WIce <> posted at
Tue, 27 Jan 2004 20:50:23 :-
>: I'm setting a <div> on a page on a lengthy page with a great deal of
>: text. I want the div to land where the user can see it. I need to
>: capture the vertical scrollbar position and use that integer to set
>: the 'Y' coordinate for the div.


Eschew non-standard quotemarks.


> xOff = parseInt(document.getElementById('contenxtmenu').s tyle.width) -
>(document.all?document.body.scrollLeft:window.pag eXOffset);
> yOff = parseInt(document.getElementById('contenxtmenu').s tyle.height) -
>(document.all?document.body.scrollTop:window.page YOffset);
> document.getElementById("contenxtmenu").style.top =
>Math.min(parseInt(y/zoom_factor*100),document.body.clientHeight-yOff)+"px";;
> document.getElementById("contenxtmenu").style.left =
>Math.min(parseInt(x/zoom_factor*100),document.body.clientWidth-xOff)+"px";



ISTM that the FAQ should have something on when parseInt should not be
used.

AFAICS, the first two superfluously call for conversion to Number (the
subtraction does that, if it is needed); and the second two should be
Math.floor.

AFAICS, I've only once found a case where parseInt is worthwhile, other
than where the base is a variable :

var T = parseInt(new Date(2e9).toLocaleString()) // ~ 1970-01-24
T = T<9 ? "US" : T>99 ? "ISO" : "UK" // US MDY, UK DMY, ISO YMD

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      01-28-2004
"Dr John Stockton" <> wrote in message
news:...
<snip>
>ISTM that the FAQ should have something on when parseInt
>should not be used.
>
>AFAICS, the first two superfluously call for conversion to
>Number (the subtraction does that, if it is needed); and the
>second two should be Math.floor.

<snip>

Done. The notes I wrote for section 4.21 (actually a more general
discussion of type-converting with JavaScript) currently include the
paragraph:-

|parseInt is occasionally used as a means of turning a
|floating point number into an integer. It is very ill suited
|to that task because if its argument is of numeric type it
|will first be converted into a string and then parsed as a
|number, very inefficient. This can produce very wrong
|results with numbers such as 2e-200, for which the nearest
|integer is zero, but with which parseInt returns 2. For
|rounding numbers to integers one of Math.round, Math.ceil
|and Math.floor are preferable, and for a desired result
|that can be expressed as a 32 bit signed integer the
|bitwise operation described below might also suite.

(There are additional notes on how the parsing is done, etc. in the
section on parseFloat)

The bitwise operation mentioned is OR zero. Which truncates floating
point numbers to integers but might be well suited to the situation in
the preceding posted code as the output is expected to be screen
co-ordinates so a result restricted to a 32 bit signed integer would be
expected anyway and it is quicker to execute than Math.floor.

Richard.


 
Reply With Quote
 
DJ WIce
Guest
Posts: n/a
 
      01-28-2004
: >: the 'Y' coordinate for the div.
: Eschew non-standard quotemarks.
Ah? It's better to use " in JavaScript?

: > xOff = parseInt(document.getElementById('contenxtmenu').s tyle.width) -
: >(document.all?document.body.scrollLeft:window.pag eXOffset);
: AFAICS, the first two superfluously call for conversion to Number (the
: subtraction does that, if it is needed);
I did not know that the substraction would eliminate the px behind the
width and convert it to a number. Thanks.

: > document.getElementById("contenxtmenu").style.left =
:
>Math.min(parseInt(x/zoom_factor*100),document.body.clientWidth-xOff)+"px";

: and the second two should be
: Math.floor.
Yeah, I was looking for trunc or something, but floor == trunc

Strangely I never saw Number before, parseInt is more common used so it
seems.
Is Number implemented later in the JavaScript interpreter? Or is it just not
the right function name why it's not that much used?
I mean wat Number? I first though it generate a random number.


Thanks,
Wouter


 
Reply With Quote
 
DJ WIce
Guest
Posts: n/a
 
      01-28-2004

: > xOff = parseInt(document.getElementById('contenxtmenu').s tyle.width) -
: >(document.all?document.body.scrollLeft:window.pag eXOffset);
: > yOff = parseInt(document.getElementById('contenxtmenu').s tyle.height) -
: >(document.all?document.body.scrollTop:window.page YOffset);

: AFAICS, the first two superfluously call for conversion to Number (the
: subtraction does that, if it is needed);

Sorry, I have to tell you it's not true for MSIE 6.028, XP:

When I remove parseInt in the above lines it assigns NaN to xOff and yOff.
When I replace parseInt in the above lines with Number the same happens.

Wouter


 
Reply With Quote
 
DU
Guest
Posts: n/a
 
      01-28-2004
DJ WIce wrote:

> : As written, you will only support backward compatible rendering mode in
> : MSIE 6 where the root element is not the body. Your script might be
> : interesting but it has limited scope/support among current browsers.
> Well, it's tested in Mozilla 1.3 and MSIE 5+ ...
> And also om NN 7 and MSIE 6
> (see http://www.djwice.com/contextmenu.html )
>
> The zoom_factor value is default set to 100, so when the zoom function is
> not implemented, it will not be changed, and the position values stay
> (nearly) unchanged.
>
> Wouter
>
>


I'm still convinced your script for zooming could be better. As coded,
it will not support MSIE 6 for windows in standards compliant rendering
mode. When you go:

xOff = parseInt(document.getElementById('contenxtmenu').s tyle.width) -
(document.all?document.body.scrollLeft:window.page XOffset);

then you are not off since the root element is not the body element but
the html element in standards compliant rendering mode for MSIE 6.

Anyway, I failed to see why you would or should need to know the zooming
factor of the document. The scrolled portion of the document can be
gotten reliably with cross-browser code without any reference to the
text zoom.

DU
 
Reply With Quote
 
DJ WIce
Guest
Posts: n/a
 
      01-28-2004
: I'm still convinced your script for zooming could be better. As coded,
: it will not support MSIE 6 for windows in standards compliant rendering
: mode. When you go:

Ah, sorry I did not understand you.
I tought you said the code does not work.
I see you mean that the code for MSIE 6 can be optimized so it will be
interpreted faster, am I right?

So document.scrollLeft when it's MSIE6 and else document.body.scrollLeft
when it's lower?
Sorry I'm very new to differant types of rendering;
"standards compiliant rendereing" as oposite of .. rendering ?

: Anyway, I failed to see why you would or should need to know the zooming
: factor of the document. The scrolled portion of the document can be
: gotten reliably with cross-browser code without any reference to the
: text zoom.
Because I want to place the menu under the mouse cursor position (x, y in my
code).
The mouse cursor position in MSIE 6 is returned as value of the 100% mode,
even if it's in 150% zoomed mode.
So one needs to correct that to let the menu show up close to the cursor if
it's on the righthand side of the document.
(the error on the lefthand side is not very big of cause).

Wouter


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to keep position of scrollbar Green ASP .Net 2 07-17-2004 03:36 PM
how to remember asp:listbox's scrollbar position Daniel ASP .Net 1 07-16-2004 02:10 AM
ScrollBar? Does it exist just WEB ScrollBar Control? Alex ASP .Net Web Controls 1 04-04-2004 12:44 AM
How to clear the scrollbar position programatically, take 2 Carl Mercier ASP .Net 1 03-02-2004 06:25 AM
Re: how datagrid scrollbar can keep same position after postback Steve C. Orr, MCSD ASP .Net 0 08-01-2003 06:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57