Need to check a few anchors and perform actions, how to do it?

My code does not work for some reason:

if (window.location.hash == '#dynamic') && (window.location.hash == '#testing') { $('html, body').animate({ scrollTop: $('#tabs').offset().top }, 1000); } 

  • My code does not work for some reason - because && is a logical "AND", and a logical "OR" is || - Grundy
  • Exactly, thanks!) - Alexander

2 answers 2

The condition is not correct

 if ((window.location.hash == '#dynamic') || (window.location.hash == '#testing')) { $('html, body').animate({ scrollTop: $('#tabs').offset().top }, 1000); } 
  • and how does your condition differ from the condition in the question? - Grundy
  • @Grundy, oh, wanted || to put - Yuri

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>