Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

One Page WordPress Site scrollTo problem.. Help!

I've managed to follow along on Zac's video for the one page wordpress site on a different theme called (stork) which i've started to customise via a child theme, but i just cannot get the 'scroll to' jquery feature to work with the links.

Please take a look at my site to see if you can work it out.. i'm slowly trying to create a portfolio for myself and currently on the web design track, but have not yet reached JS.

garryc.me

i've added theme.js to the js directory and here is the code -

/**
* Custom Theme Styles
*/

( function( $ ) {

    $("nav a").click(function() {
        var link = $(this).attr('href');
    $('html, body').animate({
        scrollTop: $(link).offset().top
    }, 2000);
});

} )( jQuery );

i've added the link to this js file through WP_enqueue in functions.php

wp_enqueue_script( 'jgtstork-script-custom', get_template_directory_uri() . '/js/theme.js', array( 'jquery' ), '', true );

This can be seen on my site when clicking inspect element like Zac does in the video.. but i just cannot seem to figure it out its not scrolling slow to each link.

Where have I gone wrong?!

3 Answers

Chris Dziewa
Chris Dziewa
17,781 Points

One thing I noticed is that you have your jQuery in parentheses on the bottom instead of the top. The first line should be jQuery(function($)) {. Hopefully this fixes your problem.

Thanks Chris, but this still didn't work....

Sorted- added a scroll to plugin which was fairly straight forward

Chris Dziewa
Chris Dziewa
17,781 Points

It's good you fixed it with a plugin but it would be good practice to figure out what went wrong in the actual code. I believe actually that all that is wrong with your code besides the first change is that you have $(link).offset instead of link.offset. When you created the variable link, you already made it a jQuery object so you don't need the dollar sign and parentheses again.