Example analysis of anchor point scoll effect is realized by jQuery

  • 2020-05-12 02:15:50
  • OfStack

This article illustrates an example of how jQuery implements the scoll effect of anchor points. Share with you for your reference. The specific implementation method is as follows:

$('a[href*=#]').click(function() {
   if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
   && location.hostname == this.hostname) {
     var $target = $(this.hash);
     $target = $target.length && $target
     || $('[name=' + this.hash.slice(1) +']');
     if ($target.length) {
       var targetOffset = $target.offset().top;
       var obj = document.documentElement;
     if (jQuery.browser.safari)
           obj = document.body
    if (jQuery.browser.msie)
           obj = 'html';
      $(obj).animate({scrollTop: targetOffset}, 1000);
       return false;
     }
   }
});

I hope this article is helpful for you to design jQuery program.


Related articles: