Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript issue with scrollTo() in Chrome
    text
    copied!<p>I try to create a web page with a fixed navigation bar at the top that covers the content underneath. When loading the page with an anchor in the url the normal behaviour is that the page scrolls the anchor to the top of the window. But then that content is hidden under the navigation bar. So I try to solve this problem with JavaScript scrollTo(). My solution works fine with Firefox and Opera but not in Chrome. Please try the example. Any ideas how to fix this issue in Chrome? Thank you.</p> <p>test.htm:</p> <pre><code>&lt;!DOCTYPE HTML&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Test&lt;/title&gt; &lt;meta charset='UTF-8'&gt; &lt;style type='text/css'&gt; #navi { position:fixed; left:0; top:0; width:100%; height:100px; background-color:yellow; } #spacer { background-color:cyan; height:100px; } #spacer2 { height:1000px; } .style1 { background-color:green; height:200px; } &lt;/style&gt; &lt;script type='text/javascript'&gt; /* &lt;![CDATA[ */ function scrollAnchor() { // doesn't work in Chrome var y = document.getElementById(window.location.hash.substr(1)).offsetTop - 110; window.scrollTo(0, y); //alert(y); } /* ]]&gt; */ &lt;/script&gt; &lt;/head&gt; &lt;body id='top' onload='scrollAnchor();'&gt; &lt;div id='navi'&gt; &lt;a href='./test2.htm'&gt;Menu&lt;/a&gt; &lt;/div&gt; &lt;div id='main'&gt; &lt;div id='spacer'&gt;&lt;/div&gt; &lt;h3 id='1'&gt;Heading 1&lt;/h3&gt;&lt;p class='style1'&gt;&lt;/p&gt; &lt;h3 id='2'&gt;Heading 2&lt;/h3&gt;&lt;p class='style1'&gt;&lt;/p&gt; &lt;h3 id='3'&gt;Heading 3&lt;/h3&gt;&lt;p class='style1'&gt;&lt;/p&gt; &lt;h3 id='4'&gt;Heading 4&lt;/h3&gt;&lt;p class='style1'&gt;&lt;/p&gt; &lt;h3 id='5'&gt;Heading 5&lt;/h3&gt;&lt;p class='style1'&gt;&lt;/p&gt; &lt;h3 id='6'&gt;Heading 6&lt;/h3&gt;&lt;p class='style1'&gt;&lt;/p&gt; &lt;div id='spacer2'&gt;&lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>test2.htm:</p> <pre><code>&lt;!DOCTYPE HTML&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Test&lt;/title&gt; &lt;meta charset='UTF-8'&gt; &lt;/head&gt; &lt;body&gt; &lt;a href='test.htm#1'&gt;Heading 1&lt;/a&gt; &lt;a href='test.htm#2'&gt;Heading 2&lt;/a&gt; &lt;a href='test.htm#3'&gt;Heading 3&lt;/a&gt; &lt;a href='test.htm#4'&gt;Heading 4&lt;/a&gt; &lt;a href='test.htm#5'&gt;Heading 5&lt;/a&gt; &lt;a href='test.htm#6'&gt;Heading 6&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload