How to go to a specific anchor when entering a page (does not work)

First of all, this is my current state of the game: thsbrk.de .

Black boxes should be, for example, about the section. I want to achieve this if you enter my page (thsbrk.de), you will immediately go to my link section (anchor '#references'). Then, if you click on the link, you will scroll to the section. I already tried to make it work, but it is not. It seems that the anchor is not working.

It would be great if someone could look at my code and offer me a solution :)

(Scrolling is not yet implemented, I only ask the binding problem)

EDIT: Here I have an example of how it should work: Example

+4
source share
3 answers

Give a script tag like this in head.Let is also the first script.

<script> location.href="http://thsbrk.de/#references" </script> 

From your code, you did the same. But try reordering script tags that may work.

+1
source

Plain JS:

 window.onload=function() { var anchorHash = 'references'; document.getElementsByName(anchorHash)[0].scrollIntoView(); } 

Here is a jQuery example from 2009 - there may be newer ways

How to scroll a table row in a view (element.scrollintoView) using jQuery?

In your case, this may work.

 $(document).ready(function() { var anchorHash = 'references'; var pos = $('#'+anchorHash).position(); window.scrollTo(0,pos.top); }); 
+1
source

Try this and tell me the result:

 $(document).ready(function() { window.location.href = '#references'; }); 

and then change your anchor tag as follows:

 <a name="references">Here</a> 
0
source

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


All Articles