Not sure if enqueuing js scripts properly

When I put this code right above the closing of my body tag it works, but when I try to enqueue it, it breaks. I’m thinking maybe jQuery isn’t getting called? The snippet is from here

FUNCTIONS.PHP

Read More
add_action( 'wp_enqueue_scripts', 'scroll_port' );

function scroll_port() {
    wp_register_script( 'scrolling', get_template_directory_uri() . '/js/scroll.js', array('jquery'), '', true );

    wp_enqueue_script('scrolling');
}

SCROLL.JS

$('a[href*=#]:not([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) {
         $('html,body').animate({
             scrollTop: target.offset().top
        }, 1000);
        return false;
    }
}
});`

Related posts

1 comment

  1. On your SCROLL.JS file, add the line before anything else,

    (function($){
    

    And after everything else

    })(jQuery);
    

    This block will load you code after jQuery is loaded, and will avoid the errors.

Comments are closed.