 |
|
 |
Thank you !! i did not use your code but the function scrollIntoView() was really hard to find it. thank u for posting it XD
|
|
|
|
 |
|
 |
I have a Modal Popup setup where I would really like to use this script, though when focus is set, which also happens if using #Anchor, the entire Frame inside the Popup is moved up, this creates problems as the Modal popups Navigation is at the top of the page and is hidden from sight.
e.g
/*This Disappears if the script is run*/
<asp:button id="btnNext" />
etc...
Body
etc...
Something way down the page to Focus
Is there a way around this problem, the top of form Navigation is important.
|
|
|
|
 |
|
 |
I needed to scroll to a specific position in the page when a button was pressed (ie. when i select a row for editing in a gridview, i want to scroll to the formview that does the actual editing)
So here is how i did it:
added in PageLoad
Page.RegisterClientScriptBlock("CtrlFocus",
@"<script>
function ScrollView(ClientId)
{
var el = document.getElementById(ClientId)
if (el != null)
{
el.scrollIntoView();
el.focus();
}
}
</script>");
added to the button properties
OnClientClick="javascript:ScrollView('editAnchor')"
and finaly added an anchor just before the formview:
OnClientClick="javascript:ScrollView('editAnchor')"
for this to work with the postback, i had to also set the MaintainScrollPositionOnPostback="true" of the page.
I hope this helps
|
|
|
|
 |
|
 |
Thanks so much. This just works great! But I could only get vertical scroll bar with this code. How do I get horizontal scroll bar? I'm sure this is a simple manipulation of the code, but if you could show a sample piece of code that does this, it would be great!
Thanks,
Sree.
|
|
|
|
 |
|
 |
This line does not work for me, it appears to be a csharp'ism that snuck into javascript code.
"window.onload += ScrollView;"
changed it to
"window.onload = ScrollView;"
Things worked a lot better for me then.
|
|
|
|
 |
|
 |
Thanks for spotting that. Have changed the code.
|
|
|
|
 |
|
 |
Hello,
you should use document.getElementById instead of document.all, because document.all does not exist in the official javascript standard. It is only implemented in >= MSIE4.0. With these browsers, it works fine in javascript-blocks too, but exactly it's a JScript command.
document.getElementById is the official supported method by DOM. All Browser I know, supports this.
Best regards
Stephan
|
|
|
|
 |
|
 |
Good point.
Have updated the code to use this. Thanks
Richard
|
|
|
|
 |
|
 |
because
-- only IE supports this
-- that's exactly the same thing what ASP.NET does after a PostBack when you activate "SmartNavigation" (among other things)
|
|
|
|
 |
|
 |
Sorry I should have stated this was actually IE and Firefox specific code. I find most people use one of these two.
I do realise that smart navigation will do this for a postback, this is for use in an initial page request situation where a request variable defines the point of focus on a page.
Regards
Richard
|
|
|
|
 |
|
 |
I experimented with .scrollIntoView() a while ago and found it (while being simple to use) a bit sloppy;
I had a JS-TreeView which had to reposition itself when a Node was opened. The .scrollIntoView() worked perfectly, but the scrollPosition was 2 pixels off - not much, but the offset was added at every click, so afer 4 clicks, the focus was 8 pixels off.
Now we calculate the exact X-Position of the anchor, and use window.scrollBy(x,y) to bring teh desired control into focus.
cheers,
Leo
|
|
|
|
 |
|
 |
Thanks Leo, thats useful to know. I will look into updating this sometime.
Regards
Richard
|
|
|
|
 |
|
 |
leopfeifenberger wrote:
Now we calculate the exact X-Position of the anchor,
I'd love to see an example of this.
|
|
|
|
 |
|
 |
Hi Ashaman (<-- Good to see SOMEBODY is still reading Robert Jordan )
it's something like this:
<SCRIPT LANGUAGE=javascript>
<!--
/*
getElementPos(ElementName)
info: Gets the Coordinates of an Element.
Can be any ObjectId (of DIVs, Anchors ect.)
params: Id of Emelent to get Coords
return: Coord Object
*/
function getElementPos(pElmName)
{
// find "Destination" Element
var elm = findObj(pElmName);
var coords = {x: 0, y: 0 };
// Tricky: Recursive Function to get
// Coords of Destination Element
// + sum of Coords of "parent" Elements:
// background: offsetLeft/offsetTop only
//gets the Offset to the Parent Object...
while (elm)
{
coords.x += elm.offsetLeft;
coords.y += elm.offsetTop;
elm = elm.offsetParent;
}
return coords;
}
/*
ScrollToElement(ElementName)
info: Scrolls to Element.
Can be any ObjectId (of DIVs, Anchors ect.)
params: Id of Emelent to Scroll To
return: void
*/
function scrollToElement(pElmName)
{
// Get the Position of the Element
var pos = getElementPos(pElmName);
// Just a hardcoded fix,
//is written by the COntrol based
//on ChildControls
if (pos.y>200) pos.y -= 20;
// Finally --> scroll into Position
window.scrollTo(pos.x,pos.y);
}
// MM DreamWeaver findObj() Function
function findObj(theObj, theDoc)
{
var p, i, foundObj;
if(!theDoc) theDoc = document;
if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
{
theDoc = parent.frames[theObj.substring(p+1)].document;
theObj = theObj.substring(0,p);
}
if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
for (i=0; !foundObj && i < theDoc.forms.length; i++)
foundObj = theDoc.forms[i][theObj];
for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
foundObj = findObj(theObj,theDoc.layers[i].document);
if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
return foundObj;
}
//-->
</SCRIPT>
I just wrote this from the top of my head (where is the Sysntax highlighting in this textbox ;PP ),
obviousely not tested, so there might be some syntax errors.
But it shoud be enough to get you on the right track.
Greets
Leo
|
|
|
|
 |