• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Javascript ScrollTo problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm displaying html and have divs throughout my display. I have javascript that hides or displays certain divs --- I can't get the window to scroll to the div that is just displayed. My HTML code is generated via XSLT, but below is an HTML snippet that demos the problem I'm having.

Here is a test version of my problem - hacked together.
Display this in an HTML page.
When you click on a 'more' or 'less' link, sections expand or contract. I'd like the window to scroll to the top of the section that was just expanded or contracted - what is happening is that the window scrolls to the top of the page - despite my attempts to get scrolling to work.

Anybody help me? It's one of those that I've spent far too long trying to fix myself!

Here is the code: onKlick obviously needs to be replaced:
===========================================

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<script language="javascript" type="text/javascript">

function displayDiv(divId, moreAnchorId, lessAnchorId, orderDotId)
{
document.getElementById(divId).style.display = "inline";
document.getElementById(moreAnchorId).style.display = "none";
document.getElementById(lessAnchorId).style.display = "inline";
document.getElementById(orderDotId).style.visibility = "hidden";
ScrollBackInPlace(moreAnchorId);

}
function hideDiv(divId, moreAnchorId, lessAnchorId, orderDotId)
{
document.getElementById(divId).style.display = "none";
document.getElementById(moreAnchorId).style.display = "block";
document.getElementById(lessAnchorId).style.display = "none";
document.getElementById(orderDotId).style.visibility = "visible";
ScrollBackInPlace(divId);
}
function ScrollBackInPlace(divId)
{
var theElement = document.getElementById(divId);
var elemPosX = theElement.offsetLeft;
var elemPosY = theElement.offsetTop;
theElement = theElement.offsetParent;
while(theElement != null){
elemPosX += theElement.offsetLeft
elemPosY += theElement.offsetTop;
theElement = theElement.offsetParent;
}
window.scrollTo(elemPosX ,elemPosY);
}
</script>
</head>
<body bgcolor="white">
<form name="Form1" method="post" id="Form1">
<input type="hidden" name="hidEventTarget" value="">
<input type="hidden" name="hidEventArgument" value="">
<input type="hidden" name="hidEFile" value="">
<input type="hidden" name="hidContent" value="">

<div id="pnOthNt" class="divContent">
<div >Contacts</div>
<TABLE cellSpacing="0" cellPadding="1" width="100%" border="1">
<TR>
<TH align="left" width="8%">Location</TH>
<TH align="left" width="20%">Document</TH>
<TH align="left" width="72%">Notes</TH>
</TR>
<TR>
<TD valign="top">030</TD>
<TD valign="top">Contact#1<br>Mar 04 2004<br>SOME DUDE</TD>
<TD nowrap>Contacts:<span id="orderDotContact1">...</span><br>
<table align="right" border="0">
<tr>
<td align="right"><a href="#" name="moreContact1" id="moreContact1"
onKlick="javascript isplayDiv('myDivContact1',
'moreContact1', 'lessContact1',
'orderDotContact1')">
more</a>
</td>
</tr>
</table>
<div style="display:none;" id="myDivContact1">
Line1</br>Line2</br>Line3</br>Line4</br>Line5</br>Line6</br>Line7</br>Line8</br>Line9</br>Line10</br>
Line11</br>Line12</br>Line13</br>Line14</br>Line15</br>Line16</br>Line17</br>Line18</br>Line19</br>Line20</br>
Line21</br>Line22</br>Line23</br>Line24</br>Line25</br>Line26</br>Line27</br>Line28</br>Line29</br>Line30</br>
<table align="right" border="0">
<tr>
<td align="right">
<a href="#" style="display:none;" name="lessContact1" id="lessContact1"
onKlick="javascript:hideDiv('myDivContact1',
'moreContact1', 'lessContact1',
'orderDotContact1')">less</a>
</td>
</tr>
</table>
</div>
</TD>
</TR>
<TR>
<TD valign="top">030</TD>
<TD valign="top">Contact#2<br>Mar 04 2004<br>SOME DUDE</TD>
<TD nowrap>Contacts:<span id="orderDotContact2">...</span><br>
<table align="right" border="0">
<tr>
<td align="right"><a href="#" name="moreContact2" id="moreContact2"
onKlick="javascript isplayDiv('myDivContact2',
'moreContact2', 'lessContact2',
'orderDotContact2')">
more</a>
</td>
</tr>
</table>
<div style="display:none;" id="myDivContact2">
Line1</br>Line2</br>Line3</br>Line4</br>Line5</br>Line6</br>Line7</br>Line8</br>Line9</br>Line10</br>
Line11</br>Line12</br>Line13</br>Line14</br>Line15</br>Line16</br>Line17</br>Line18</br>Line19</br>Line20</br>
Line21</br>Line22</br>Line23</br>Line24</br>Line25</br>Line26</br>Line27</br>Line28</br>Line29</br>Line30</br>
<table align="right" border="0" ID="Table2">
<tr>
<td align="right">
<a href="#" style="display:none;" name="lessContact2" id="lessContact2"
onKlick="javascript:hideDiv('myDivContact2',
'moreContact2', 'lessContact2',
'orderDotContact2')">
less</a>
</td>
</tr>
</table>
</div>
</TD>
</TR>
<TR>
<TD valign="top">030</TD>
<TD valign="top">Contact#3<br>Mar 04 2004<br>SOME DUDE</TD>
<TD nowrap>Contacts:<span id="orderDotContact3">...</span><br>
<table align="right" border="0" >
<tr>
<td align="right"><a href="#" name="moreContact3" id="moreContact3"
onKlick="javascript isplayDiv('myDivContact3',
'moreContact3', 'lessContact3',
'orderDotContact3')">
more</a>
</td>
</tr>
</table>
<div style="display:none;" id="myDivContact3">
Line1</br>Line2</br>Line3</br>Line4</br>Line5</br>Line6</br>Line7</br>Line8</br>Line9</br>Line10</br>
Line11</br>Line12</br>Line13</br>Line14</br>Line15</br>Line16</br>Line17</br>Line18</br>Line19</br>Line20</br>
Line21</br>Line22</br>Line23</br>Line24</br>Line25</br>Line26</br>Line27</br>Line28</br>Line29</br>Line30</br>
<table align="right" border="0" >
<tr>
<td align="right">
<a href="#" style="display:none;" name="lessContact3" id="lessContact3" onKlick="javascript:hideDiv('myDivContact3',
'moreContact3', 'lessContact3',
'orderDotContact3')">less</a>
</td>
</tr>
</table>
</div>
</TD>
</TR>
</TABLE>
<br>




</form>
</div>

</body>
</html>
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure why, but if you put a delay before you do scrolling, it seems to be working. Use the wfollowing instead of your scrollTo call
 
Marc Nielsen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks... that did the trick. Never would have tried that! Thanks so much.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic