Forum Moderators: open

Message Too Old, No Replies

Page scrolls to top when making object visible

         

Beans

3:33 pm on May 29, 2009 (gmt 0)

10+ Year Member



I'm making a page where I want to click on a link and a popup box appears by the cursor with choices. I'm using the .style.top and .style.left properties to set the location of the hidden content. Then I make it visible with the .visibility property.
All this works fine, except that if the browser window has scrolled down the browser will always scroll to top left which makes it really frustrating for the user. I assumed at first it was when the visibility property was set that the browser window scrolled up but it's not. It's when the top/left coordinates are set that the scroll occurs. If anyone knows why this is happening I would be really grateful. I've got a code snippet that recreates this. You have to make sure that the browser window scrolls then click on the "Click to make visible" link.

<html>
<body>
<div id="invisidiv" style="position:absolute; visibility:hidden; top:0px; left:0px; z-index:10000;">
invisible content
</div>
<script language="javascript" type="text/javascript">
function makevisible(){
document.getElementById("invisidiv").style.top="200px";
document.getElementById("invisidiv").style.left="200px";
document.getElementById("invisidiv").style.visibility="visible";
}
for(index=1;index<10;index++) document.write("<p>&nbsp;<br />cause scroll<br />&nbsp;</p>");
</script>
<a href="#" onclick="makevisible()">Click to make visible</a>
</body>
</html>

Many thanks in advance

Vince

PCInk

3:40 pm on May 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The problem is the "#" in the href tag which causes the scrolling.

rocknbil

3:54 pm on May 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to return false from your function. This tells the browser to allow JS to manage the event and not perform the natural action of the link.

An added feature, pass the ID of the object to show/hide from your click. This allows you to apply it to any link and any div.


<script type="text/javascript">
function makevisible(obj){
if (document.getElementById(div)) {
document.getElementById(obj).style.top="200px";
document.getElementById(obj).style.left="200px";
document.getElementById(obj).style.visibility="visible";
}
return false;
}
for(index=1;index<10;index++) { document.write("<p>&nbsp;<br />cause scroll<br />&nbsp;</p>"); }
</script>
<a href="#" onclick="return makevisible(invisidiv)">Click to make visible</a>

Beans

4:15 pm on May 29, 2009 (gmt 0)

10+ Year Member



Thanks to both of you for the replies. I spent hours on this and couldn't see the wood for the trees but that's sorted it now.

Many thanks !

Vince

rocknbil

6:50 pm on May 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops on my part.

This
if (document.getElementById(div)) {

should be

if (document.getElementById(obj)) {