Good day! There is a tabbed script. Help, please, make sure that when you click on a tab, the page automatically scrolls up, where the top of the page will be the top point of the tab of the .content block. Thanks for the help!

$(document).ready(function(){ $('.tab').on('click', function() { var $me = $(this); var id = $me.data("id"); $('.content').hide(); $('.content[data-id = ' + id + '-content]').show(); $('.tab.tab-sel').removeClass('tab-sel'); $('.tab[data-id=' + id + ']').addClass('tab-sel'); }); }); 
  • Attach the HTML markup to the question - user200141
  • Oleg, I welcome you! This is for yesterday’s question ( ru.stackoverflow.com/questions/505172/… ) I used your script, started doing it, everything is fine, but my page turns out to be too complex and voluminous, and when I go through the tabs it’s not very aesthetically pleasing that looks page jumps. Therefore, I see a solution so that when you click the page scroll to the top of the tab. - LADYX
  • Greetings. I realized that yesterday's question. It's just not very clear what your jumps are, because when you switch a tab, the user will scroll to the top and so on to push the tab. - user200141
  • one
    And so - I can prompt that in jQuery var offset = $('<Π­Π»Π΅ΠΌΠ΅Π½Ρ‚>').offset().top; - find out the indent from the top of the screen. $("body,html").animate({scrollTop: offset}, 500); - smoothly scroll to this place. 500 is the time in milliseconds for the animation - user200141
  • one
    In this case, IMHO makes sense to animate only a click on the lower tabs. Let the lower tabs give the class .bottom . The code does not change much, now I will write. - user200141

1 answer 1

Try this

 $(document).ready(function(){ $('.tab').on('click', function() { var $me = $(this); var id = $me.data("id"); var $tab = $('.tab[data-id=' + id + ']'); var $tabBody = $('.content[data-id = ' + id + '-content]'); $('.tab.tab-sel').removeClass('tab-sel'); $tab.addClass('tab-sel'); $('.content').fadeOut(500); $tabBody.fadeIn(500); //Если ΠΊΠ»ΠΈΠΊ ΠΏΠΎ Π½ΠΈΠΆΠ½Π΅ΠΉ Π²ΠΊΠ»Π°Π΄ΠΊΠ΅(НапримСр класс .bottom) if($me.is('.bottom')) { var offset = $tabBody.offset().top; $("body,html").animate({scrollTop: offset}, 500); } }); }); 

Without animation, if ugly it turns out

 $(document).ready(function(){ $('.tab').on('click', function() { var $me = $(this); var id = $me.data("id"); var $tab = $('.tab[data-id=' + id + ']'); var $tabBody = $('.content[data-id = ' + id + '-content]'); $('.tab.tab-sel').removeClass('tab-sel'); $tab.addClass('tab-sel'); $('.content').hide(); $tabBody.show(); //Если ΠΊΠ»ΠΈΠΊ ΠΏΠΎ Π½ΠΈΠΆΠ½Π΅ΠΉ Π²ΠΊΠ»Π°Π΄ΠΊΠ΅(НапримСр класс .bottom) if($me.is('.bottom')) { var offset = $tabBody.offset().top; $("body,html").scrollTop(offset); } }); }); 
  • I understood the essence, edit it a little, and everything works out. Thank you so much for the help !!! Good luck! - LADYX
  • one
    @LADYX: var offset is just the number of pixels from the top of the page to the element. This number and with it you can do any mathematical operation. In your case, you need to compensate for these 60px , raising the page above. Ie taking them away from the calculated indent. $("body,html").scrollTop(offset - 60); - user200141
  • one
    @LADYX: The number 60 itself is straightforward not to insert into the code, otherwise the so-called "magic number" will turn out. For clarity, I wrote this to you. It is better to put in a separate variable of type var topMenuHeight = 60; and substitute it already, or even measure the height of this fixed block "on the fly". - user200141
  • one
    @LADYX: If you leave offset - 60 , then after a while you can forget what 60 is and why it is here. Congratulations, you got a "magic number" 60 in the code, which is a mystery to you. Writing offset - topMenuHeight unequivocally tells the reader that they subtracted the height of the top menu from the indent. The abundance of "magic numbers" in the code will make it harder to read, or simply make it impossible to read it. - user200141
  • one
    @LADYX: Thank you, very nice to hear. Good luck to you in comprehending our difficult task too =) - user200141