JS script loads through wp_enqueue but does not work.
-
Here’s my problem (I posted this under plugins, so am reposting here because it wasn’t the right place!) (And I can’t seem to edit/move/delete my previous post anymore.)
I have a js script that I want to load to the page. If I hard-code it onto the <head> section of header.php (including loading an external minified jquery, http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js ) it works just fine.
However, if I remove the link to the external jquery file, it stops working.
If I try to load the script through wp_enqueue (functions.php), it loads just fine (I can see it loaded in the <head> section, and if I click on the link it opens the file, so it links to the correct file), but it still doesn’t work.
What could be the problem?
Here is the original script:
$(function() { $(window).on('scroll', function() { $('#par_background').css('margin-top', $(window).scrollTop() * -.3); }); }); $(document).ready(function () { $(window).bind('scroll', function () { var distance = 50; if ($(window).scrollTop() > distance) { $('nav').addClass('scrolled'); } else { $('nav').removeClass('scrolled'); } }); });And here’s the modified content of test.js file, formatted to load with wp_enqueue (but I am not confident at all if this is the correct formatting, not an expert with JS):
jQuery(function() { $(window).on('scroll', function() { $('#par_background').css('margin-top', $(window).scrollTop() * -.3); }); }); jQuery(function () { $(window).bind('scroll', function () { var distance = 50; if ($(window).scrollTop() > distance) { $('nav').addClass('scrolled'); } else { $('nav').removeClass('scrolled'); } }); });Any help would be appreciated!
The topic ‘JS script loads through wp_enqueue but does not work.’ is closed to new replies.