There is an html-code in which there are several anchor links, at the top there is a fixed menu with fixed heights. You need to make a js-code that, when using one of the anchor links, will transfer not to the anchor itself, but 140 pixels lower ...
- as I understand it is necessary to first create an array var arrYakor = [], and how to fit all id into it? - Ivan
- maybe so? var arrYakor = [# -homepage, #services, #contact] - Ivan
- show your HTML menu code - Arsen
- I understood correctly you need something like that css-tricks.com/snippets/jquery/smooth-scrolling - Ihor Tkachuk
- From the second time I understood the question. Js is not needed here. - Qwertiy β¦
|
3 answers
Made for clarity: this code makes a smooth scroll to the anchor, but stops 100 pixels higher.
$(document).ready(function() { $('a[href^="#"]').bind('click',function (e) { e.preventDefault(); var target = this.hash, $target = $(target); $('html, body').stop().animate({ 'scrollTop': $target.offset().top - 100 }, 900, 'swing', function () { window.location.hash = target; }); }); }); .one { height: 1000px; background: tomato; } .two { height: 1000px; background: silver; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <nav> <ul> <li><a href="#one">1</a></li> <li><a href="#two">2</a></li> </ul> </nav> <div class="one" id="one">1</div> <div class="two" id="two">2</div> |
This can be done without scripts:
body { margin: 0; } nav { position: fixed; height: 2rem; line-height: 2rem; top: 0; width: 100%; background: silver; } section { padding-top: 2rem; margin-bottom: -2rem; } section:last-child { margin-bottom: 0; } <nav> <a href="#s1">Section 1</a> <a href="#s2">Section 2</a> <a href="#s3">Section 3</a> <a href="#s4">Section 4</a> <a href="#s5">Section 5</a> </nav> <section id="s1"> <h1>Section 1</h1> <p>ΠΡΡΡ html-ΠΊΠΎΠ΄, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠΊΠΎΡΠ½ΡΡ
ΡΡΡΠ»ΠΎΠΊ, Π²Π²Π΅ΡΡ
Ρ ΡΠΈΠΊΡΠΈΡΠΎΠ²Π°Π½Π½Π°Ρ ΠΌΠ΅Π½ΡΡΠΊΠ° Ρ ΡΠΈΠΊΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠΉ Π²ΡΡΠΎΡΠΎΠΉ. ΠΡΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ js-ΠΊΠΎΠ΄, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ· ΡΠΊΠΎΡΠ½ΡΡ
ΡΡΡΠ»ΠΎΠΊ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π½ΠΎΡΠΈΡΡ Π½Π΅ Π½Π° ΡΠ°ΠΌ ΡΠΊΠΎΡΡ, Π° Π½Π° 140 ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ Π½ΠΈΠΆΠ΅...</p> </section> <section id="s2"> <h1>Section 2</h1> <p>ΠΡΡΡ html-ΠΊΠΎΠ΄, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠΊΠΎΡΠ½ΡΡ
ΡΡΡΠ»ΠΎΠΊ, Π²Π²Π΅ΡΡ
Ρ ΡΠΈΠΊΡΠΈΡΠΎΠ²Π°Π½Π½Π°Ρ ΠΌΠ΅Π½ΡΡΠΊΠ° Ρ ΡΠΈΠΊΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠΉ Π²ΡΡΠΎΡΠΎΠΉ. ΠΡΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ js-ΠΊΠΎΠ΄, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ· ΡΠΊΠΎΡΠ½ΡΡ
ΡΡΡΠ»ΠΎΠΊ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π½ΠΎΡΠΈΡΡ Π½Π΅ Π½Π° ΡΠ°ΠΌ ΡΠΊΠΎΡΡ, Π° Π½Π° 140 ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ Π½ΠΈΠΆΠ΅...</p> </section> <section id="s3"> <h1>Section 3</h1> <p>ΠΡΡΡ html-ΠΊΠΎΠ΄, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠΊΠΎΡΠ½ΡΡ
ΡΡΡΠ»ΠΎΠΊ, Π²Π²Π΅ΡΡ
Ρ ΡΠΈΠΊΡΠΈΡΠΎΠ²Π°Π½Π½Π°Ρ ΠΌΠ΅Π½ΡΡΠΊΠ° Ρ ΡΠΈΠΊΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠΉ Π²ΡΡΠΎΡΠΎΠΉ. ΠΡΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ js-ΠΊΠΎΠ΄, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ· ΡΠΊΠΎΡΠ½ΡΡ
ΡΡΡΠ»ΠΎΠΊ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π½ΠΎΡΠΈΡΡ Π½Π΅ Π½Π° ΡΠ°ΠΌ ΡΠΊΠΎΡΡ, Π° Π½Π° 140 ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ Π½ΠΈΠΆΠ΅...</p> </section> <section id="s4"> <h1>Section 4</h1> <p>ΠΡΡΡ html-ΠΊΠΎΠ΄, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠΊΠΎΡΠ½ΡΡ
ΡΡΡΠ»ΠΎΠΊ, Π²Π²Π΅ΡΡ
Ρ ΡΠΈΠΊΡΠΈΡΠΎΠ²Π°Π½Π½Π°Ρ ΠΌΠ΅Π½ΡΡΠΊΠ° Ρ ΡΠΈΠΊΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠΉ Π²ΡΡΠΎΡΠΎΠΉ. ΠΡΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ js-ΠΊΠΎΠ΄, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ· ΡΠΊΠΎΡΠ½ΡΡ
ΡΡΡΠ»ΠΎΠΊ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π½ΠΎΡΠΈΡΡ Π½Π΅ Π½Π° ΡΠ°ΠΌ ΡΠΊΠΎΡΡ, Π° Π½Π° 140 ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ Π½ΠΈΠΆΠ΅...</p> </section> <section id="s5"> <h1>Section 5</h1> <p>ΠΡΡΡ html-ΠΊΠΎΠ΄, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠΊΠΎΡΠ½ΡΡ
ΡΡΡΠ»ΠΎΠΊ, Π²Π²Π΅ΡΡ
Ρ ΡΠΈΠΊΡΠΈΡΠΎΠ²Π°Π½Π½Π°Ρ ΠΌΠ΅Π½ΡΡΠΊΠ° Ρ ΡΠΈΠΊΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠΉ Π²ΡΡΠΎΡΠΎΠΉ. ΠΡΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ js-ΠΊΠΎΠ΄, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ· ΡΠΊΠΎΡΠ½ΡΡ
ΡΡΡΠ»ΠΎΠΊ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π½ΠΎΡΠΈΡΡ Π½Π΅ Π½Π° ΡΠ°ΠΌ ΡΠΊΠΎΡΡ, Π° Π½Π° 140 ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ Π½ΠΈΠΆΠ΅...</p> </section> - an interesting option, but what if
sectionhas abackground- will it flow onto the upper block? - lexxl - @lexxl, I suggest giving an example, asking a separate question with it and giving a link. Because, firstly, there will not be, secondly, not on, but under, what will appear only with a transparent background of the title, and thirdly, how do you imagine the scroll not to affect it? And in general, colors section, but not the parent, strange. And finally, probably, everything is solved by an additional container. - Qwertiy β¦
- if elementary through the debugger to add a color background of each
sectionin your code, then it will be seen that the last line of the section will be covered with the background of the section following / under it. so, firstly, secondly, not "under", but "on", thirdly, it finally can not understand what you are talking about) but painting the section is just the same rules, because it is a container for a separate block with info, in which its title and content, and then maybe your background image / color - lexxl - @lexxl, still need an example. The problem does not reproduce. i.stack.imgur.com/x3Ovo.png - Qwertiy β¦
- imgur.com/a/5CUjM added two different backgrounds and a border - lexxl
|
I think this example will help you (you can customize your menu structure)
$(document).ready(function(){ $('a').on('click',function(e){ e.preventDefault(); var anch = $(this).attr('href').split('#'); var items = $('#'+anch[1]).position().top + 140; var body = $("html, body"); body.stop().animate({scrollTop:items+'px'}, '800', 'swing'); console.log(items); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li><a href="#menu1">ΠΠ΅Π½Ρ 1</a></li> <li><a href="#menu2">ΠΠ΅Π½Ρ 2</a></li> <li><a href="#menu3">ΠΠ΅Π½Ρ 3</a></li> </ul> <div> <p id="menu1">ΠΠΎΠ½ΡΠ΅Π½Ρ</p> <p id="menu2">ΠΠΎΠ½ΡΠ΅Π½Ρ</p> <p id="menu3">ΠΠΎΠ½ΡΠ΅Π½Ρ</p> </div> - Yes, anchor links work fine, but all the others do not work at all, do not go to the right pages, gave the class to all anchor links .anchor and wrote it does not work: (($ ('a.anchor'). on ('click', function (e) - Ivan
- one@Ivan Can you give me a Lexxl answer, marked as the correct answer? after installing a specific class $ ('a.anchor') other links do not work? you may need to clear the old browser cache. - Arsen
- cleaning helped thank you) - Ivan
- always please) - Arsen
|