I’m stumped. I added a simple jquery function to the footer.php of my wordpress theme, but it doesn’t seem to do anything. I tried an even simpler hide(); function and that didn’t fire either. I can’t get any jquery to fire at all, but the jquery library is definitely loading into my theme (it’s based on twentyeleven). Here’s my code:
</footer><!-- #colophon -->
</div><!-- #page -->
<script>
jQuery(document).ready(function ($) {
$(".sub-menu").effect("highlight", {}, 3000);
});
</script>
<?php wp_footer(); ?>
</body>
</html>
I loaded the jquery ui core effects via my functions.php and see that it shows up in the site’s resources when I use the Chrome inspector, so the highlight(); function should work.
Here’s the page it should be working on
Why wouldn’t the jquery script be running?
Thanks!
Kenny
EDIT
Final code is the following (Sadly, I don’t know how to make the effect recursive through the <li> elements, but this does the job):
<script>
jQuery(document).ready(function ($) {
setTimeout(function() {
$('.sub-menu li:first-child').animate({ marginRight: '15px' }, 500);
$('.sub-menu li:first-child').animate({ marginRight: '0' }, 500);
setTimeout(function() {
$('.sub-menu li:nth-child(2)').animate({ marginRight: '15px' }, 500);
$('.sub-menu li:nth-child(2)').animate({ marginRight: '0' }, 500);
}, 400);
setTimeout(function() {
$('.sub-menu li:nth-child(3)').animate({ marginRight: '15px' }, 500);
$('.sub-menu li:nth-child(3)').animate({ marginRight: '0' }, 500);
}, 800);
setTimeout(function() {
$('.sub-menu li:nth-child(4)').animate({ marginRight: '15px' }, 500);
$('.sub-menu li:nth-child(4)').animate({ marginRight: '0' }, 500);
}, 1200);
}, 3000);
}(jQuery));
</script>
Your script is not running because the
$argument is not defined. The solution is to add(jQuery)after the closing curly braces:This technique is used when multiple libraries (potentially ones that also use
$) are present. See Using jQuery with Other Libraries for details.Now, it looks like you want to animate the
sub-menulist items one by one with a 400 ms delay. You can simplify your code by using.each()and.delay():The second part of the animation (setting
margin-rightto 0) is defined in thecompleteargument of.animate(), which eliminates one.animate()call. Thei * 400delay creates the cascading effect that you want.