Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type="text/javascript">
- $(document).ready(function(){
- //regExp for current domain (http://example.com)
- var domainRegExp = new RegExp(window.location.hostname, ‘’);
- //regExp for relative link to current domain (/page - relative to current domain)
- var relativeRegExp = new RegExp(‘^\/[^\/]’, ‘’);
- // Add smooth scrolling to all links
- $("a").on('click', function(event) {
- // Make sure this.hash has a value before overriding default behavior
- if (this.hash !== "") {
- //Prevent anchor click behavior
- if (!domainRegExp.test(this.href) && !relativeRegExp.test(this.href)) {
- return;
- }
- // Prevent default anchor click behavior
- event.preventDefault();
- // Store hash
- var hash = this.hash;
- // Using jQuery's animate() method to add smooth page scroll
- // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
- $('html, body').animate({
- scrollTop: $(hash).offset().top
- }, 800, function(){
- // Add hash (#) to URL when done scrolling (default click behavior)
- window.location.hash = hash;
- });
- } // End if
- });
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement