Last active
April 27, 2016 16:12
-
-
Save joshrogersdesign/8be917787017325ff7950290f9612a34 to your computer and use it in GitHub Desktop.
Add SmoothScroll.js to specified WordPress page based on conditional tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ENQUEUE JQUERY SMOOTH SCROLL | |
function smoothscrollfaqs() { | |
if( is_page('faqs')) { | |
wp_enqueue_script( 'scrollToAnchor', get_stylesheet_directory_uri() . '/js/smoothscroll.js', array( 'jquery' ), '', true ); | |
} | |
} | |
add_action( 'wp_footer', 'smoothscrollfaqs' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(function() { | |
// scroll handler | |
var scrollToAnchor = function( id ) { | |
// grab the element to scroll to based on the name | |
var elem = jQuery("a[name='"+ id +"']"); | |
// if that didn't work, look for an element with our ID | |
if ( typeof( elem.offset() ) === "undefined" ) { | |
elem = jQuery("#"+id); | |
} | |
// if the destination element exists | |
if ( typeof( elem.offset() ) !== "undefined" ) { | |
// do the scroll | |
jQuery('html, body').animate({ | |
scrollTop: elem.offset().top | |
}, 1000 ); | |
} | |
}; | |
// bind to click event | |
jQuery("a").click(function( event ) { | |
// only do this if it's an anchor link | |
if ( jQuery(this).attr("href").match("#") ) { | |
// cancel default event propagation | |
event.preventDefault(); | |
// scroll to the location | |
var href = jQuery(this).attr('href').replace('#', '') | |
scrollToAnchor( href ); | |
} | |
}); | |
}); | |
//updated by James Pederson |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Greetings
I was working with a client recently who wanted to implement a "Smooth Scrolling" function for anchor tags on their FAQs page. I saw a lot of material on how to add a "back to top" link/button that would work universally across the site, but I had a hard time finding anything for implementing smooth scrolling for a single page. So I "borrowed" other great developers ideas and put something together based on my knowledge of Wordpress, PHP, jQuery etc.
I am no jQuery expert - this code works great on WordPress 4.5 for me, but with updates being released all the time it may need updating/tweaking. Feel free to add, edit, request anything that seems out of place. I am happy to hear your critiques and improve this.
INSTRUCTIONS:
-Check the console for errors - but you should be ready to start seeing some smooth scrolling on your anchor tags
CREDITS:
The enqueuing was adapted from wpbeginner
The smoothscroll.js was adapted in part from Brad S. Knutson