Completely stuck in uploading jQuery to a WordPress site

this may seem like a simple mistake, but I'm stuck on it.

I start the installation of WordPress using a script that I found that hides the scroll-based header. They script worked perfectly on the page itself, enclosed in tags, until I started loading it into a WordPress theme as a separate file.

wp_enqueue_script( 'greycanary-effects', get_template_directory_uri() . '/js/effects.js', array(), '20120206', true );

And the script ...

( function() {
    var didScroll;
    var lastScrollTop = 0;
    var delta = 5;
    var navbarHeight = $('.main-navigation').outerHeight();

    jQuery(window).scroll(function(event){
        didScroll = true;
    });

    setInterval(function() {
        if (didScroll) {
            hasScrolled();
            didScroll = false;
        }
    }, 250);

    function hasScrolled() {
        var st = $(this).scrollTop();

        // Make sure they scroll more than delta
        if(Math.abs(lastScrollTop - st) <= delta)
            return;

        // If they scrolled down and are past the navbar, add class .nav-up.
        // This is necessary so you never see what is "behind" the navbar.
        if (st > lastScrollTop && st > navbarHeight){
            // Scroll Down
            $('.main-navigation').removeClass('nav-down').addClass('nav-up');
        } else {
            // Scroll Up
            if(st + $(window).height() < $(document).height()) {
                $('.main-navigation').removeClass('nav-up').addClass('nav-down');
            }
        }

        lastScrollTop = st;
    };
});
+4
source share
4 answers

You will need to replace all your links with "jQuery"; or you need to create a variable called "$":

eg. put this at the beginning of your function () {

(function(){
    $ = jQuery;
    ....

OR

( function() {
    var didScroll;
    var lastScrollTop = 0;
    var delta = 5;
    var navbarHeight = jQuery('.main-navigation').outerHeight();

    jQuery(window).scroll(function(event){
        didScroll = true;
    });

    setInterval(function() {
        if (didScroll) {
            hasScrolled();
            didScroll = false;
        }
    }, 250);

    function hasScrolled() {
        var st = jQuery(this).scrollTop();

        // Make sure they scroll more than delta
        if(Math.abs(lastScrollTop - st) <= delta)
            return;

        // If they scrolled down and are past the navbar, add class .nav-up.
        // This is necessary so you never see what is "behind" the navbar.
        if (st > lastScrollTop && st > navbarHeight){
            // Scroll Down
            jQuery('.main-navigation').removeClass('nav-down').addClass('nav-up');
        } else {
            // Scroll Up
            if(st + jQuery(window).height() < jQuery(document).height()) {
                jQuery('.main-navigation').removeClass('nav-up').addClass('nav-down');
            }
        }

        lastScrollTop = st;
    };
});
+2
source

wordpress jquery $, javascript. jQuery. , jQuery,

+1

, :

wp_enqueue_script( 'greycanary-effects', get_template_directory_uri() . '/js/effects.js', array( 'jquery' ), '20120206', true );

array( 'jquery' ) wordpress, script jquery .

wp_enqueue_script ,

, $,

( function( $ ) {
    // Use $ here instead of jQuery if you want
    ...
} )( jQuery );
+1

,

$to jQuery. script .

0

All Articles