The problem is as follows. The site implements smooth anchor links via https://github.com/cferdinandi/smooth-scroll I am an amateur in javascript and jQuery, so could you please tell me how you can implement this not through hash or how to remove " "?
- oneAnd what prevents # in the links? - Vadizar
- imgur.com/a/gvhTq I just want it to be implemented without a grid, like on all sites. - Alexander Demchenko
|
2 answers
Made a minimalist example on jQuery. The link through # records the id of the block to which you want to go, however, no hashes are added to the URL. Works for any link on the page in which the id of the block to go.
// Page a Link Smooth Scrolling $("[href^='#']").click(function() { var idtop = $($(this).attr("href")).offset().top; $('html,body').animate( // Time animation {scrollTop: idtop}, 500); return false; }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <style> div{height:100vh} #block1{background: #000} #block1 a{color: #fff} </style> <div id="block1"> <a href="#block2">ΡΠΊΡΠΎΠ»Π» ΠΊ ΡΠ»Π΅Π΄ΡΡΡΠ΅ΠΌΡ Π±Π»ΠΎΠΊΡ</a> </div> <div id="block2"> <a href="#block1">ΡΠΊΡΠΎΠ»Π» ΠΊ ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠ΅ΠΌΡ Π±Π»ΠΎΠΊΡ</a> </div> - one
$("[href^='#']").click(function() {- it would be more correct$("body").on('click', "[href^='#']", function() {. - Qwertiy β¦ - Why? Argue. - Vadizar
- @Qwertiy read this article first and find out that .on is already deprecated: api.jquery.com/category/deprecated - Vadizar
- oneI do not see in that article that
onwas declared deprecated. And did not notice that you used jQuery 3. Why is it better? Because it works with dynamically added links. And your option selects only existing at the time of sampling. - Qwertiy β¦ - @Qwertiy campaign not read this article. Or are going to remove. Now I find and throw off. - Vadizar
|
This plugin is specially built in such a way as to handle the transition by clicks keeping them in history. If you just need a smooth transition, can it be easier to make it yourself? Here is a sample code, it is very simple:
$('body').on('click', '.smooth-scroll-class', function(){ //ΠΏΠΎ href ΠΈΡΠ΅ΠΌ ΡΠ»Π΅ΠΌΠ΅Π½Ρ Ρ Π½ΡΠΆΠ½ΡΠΌ id ΠΈ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅ΠΌ Π΅Π³ΠΎ ΠΏΠΎΠ·ΠΈΡΠΈΡ pos = $( $(this).attr('href') ).offset(); if (pos) { //ΡΠΊΡΠΎΠ»ΠΈΠΌ Π½Π° ΡΡΡ ΠΏΠΎΠ·ΠΈΡΠΈΡ $("html, body").animate({scrollTop: pos.top}, 600); //ΠΎΡΠΊΠ»ΡΡΠ°Π΅ΠΌ ΡΡΠ°Π½Π΄Π°ΡΡΠ½ΡΠΉ ΠΏΠ΅ΡΠ΅Ρ
ΠΎΠ΄ ΠΏΠΎ ΡΡΡΠ»ΠΊΠ΅ return false; } }) So you can make a link not to <a name='hashlink'> a to <.. id='hashlink'> Those do not need to create anchors, just write the id desired element.
You can also search for an anchor.
$('body').on('click', '.smooth-scroll-class', function(){ //ΠΏΠΎ href ΠΈΡΠ΅ΠΌ ΡΠΊΠΎΡΡ Ρ Π½ΡΠΆΠ½ΡΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ pos = $('a[name=' + $(this).attr('href').replace(/^#/,'') + ']').offset(); if (pos) { //ΡΠΊΡΠΎΠ»ΠΈΠΌ Π½Π° ΡΡΡ ΠΏΠΎΠ·ΠΈΡΠΈΡ $("html, body").animate({scrollTop: pos.top}, 600); //ΠΎΡΠΊΠ»ΡΡΠ°Π΅ΠΌ ΡΡΠ°Π½Π΄Π°ΡΡΠ½ΡΠΉ ΠΏΠ΅ΡΠ΅Ρ
ΠΎΠ΄ ΠΏΠΎ ΡΡΡΠ»ΠΊΠ΅ return false; } }) Live test:
$('body').on('click', '.smooth-scroll', function() { pos = $('a[name=' + $(this).attr('href').replace(/^#/,'') + ']').offset(); if (pos) { $("html, body").animate({ scrollTop: pos.top }, 600); return false; } }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#bottom" class='smooth-scroll'>ΡΠΊΡΠΎΠ»Π» Π²Π½ΠΈΠ·</a> <div style="height:2000px"></div> <a name='bottom'></a> |