How to load a page in the middle of the page (instead of the top)?

I would like the page to open in a specific div halfway down, and not at the top ...

I have something like:

<div id="d1"> <div id="d2"> <div id="d3"> <div id="d4"> <div id="d5"> <div id="d6"> 

How can I open a page on # d4 and not on top? (Besides adding # d4 to the end of the url ...)

I suppose there must be some easy way to do this, but I can't figure out how to look for a solution! HTML, javascript? Any help is appreciated.

+1
source share
4 answers
 <script> function ScrollToElement(theElement){ var selectedPosX = 0; var selectedPosY = 0; while(theElement != null){ selectedPosX += theElement.offsetLeft; selectedPosY += theElement.offsetTop; theElement = theElement.offsetParent; } window.scrollTo(selectedPosX,selectedPosY); } </script> 

http://radio.javaranch.com/pascarello/2005/01/09/1105293729000.html

+1
source

You can use Javascript:

 location.replace('#d4'); 
0
source

Find the position of the div using this and then use the following javascript command:

 window.scroll(0, DIV_POS); // horizontal and vertical scroll targets 
0
source

EDIT: OOPS! did not read Except ... neglect!

Note to yourself, read the entire question before answering!

End edit

You can always use the HTML anchor tag.

 <a name="d1" /> <div id="d1"> <a name="d2" /> <div id="d2"> <a name="d3" /> <div id="d3"> <a name="d4" /> <div id="d4"> <a name="d5" /> <div id="d5"> <a name="d6" /> <div id="d6"> 

When you go to the page, you must specify the name of the binding in the URL: pagename.htm # d4

Be sure to close the div tags.

Good luck

Patrick

0
source

Source: https://habr.com/ru/post/1435630/


All Articles