jquery.smooth-scroll.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. $(document).ready(function() {
  2. function filterPath(string) {
  3. return string
  4. .replace(/^\//,'')
  5. .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
  6. .replace(/\/$/,'');
  7. }
  8. var locationPath = filterPath(location.pathname);
  9. $('a[href*=#]').each(function() {
  10. var thisPath = filterPath(this.pathname) || locationPath;
  11. if ( locationPath == thisPath
  12. && (location.hostname == this.hostname || !this.hostname)
  13. && this.hash.replace(/#/,'') ) {
  14. var $target = $(this.hash), target = this.hash;
  15. if (target) {
  16. $(this).click(function(event) {
  17. if (!$(this.hash).offset()) {
  18. return;
  19. }
  20. event.preventDefault();
  21. position = $(this.hash).offset().top;
  22. $('html,body').animate({scrollTop: position}, 400, function() {
  23. location.hash = target;
  24. });
  25. });
  26. }
  27. }
  28. });
  29. });