robinadr

Tagged jQuery

I spent some time making my navigation responsive, after some plugins didn’t really work for me. It turns out it’s pretty straightforward to code your own responsive navigation using jQuery.

With most of the unrelated HTML and code for my grid system stripped out, this is what the code for the navigation itself looks like:

This is what it looks like, first on big screens and then on mobile screens:

Navigation on bigger screens

Navigation on mobile screens

I stole the idea for using <span>s for the navigation icon from Bootstrap. It’s pretty straightforward to style:

Then this is the JavaScript for all of this:

Not only does this code handle the toggling of the navigation for mobile browsers, it also watches the size of the window. If someone resizes their window down to the size needed to trigger the bar or back to full size, it will handle it gracefully.

Quick and easy.

You should use the Use Google Libraries plugin instead.

Update: I’ve modified the plugin to use a generic URL (.../1/jquery...) which loads the latest 1.x version. That way I don’t have to change the plugin everytime jQuery is updated.

The ZIP file has been updated, just overwrite your old googlejs.php file and everything will be gravy.

In an effort to save bandwidth, I made a quick plugin to modify WordPress to use Google’s Libraries API, which offers a Google-hosted jQuery. Here’s the code in question:

function radr_google_js( &$scripts ) {
    $scripts->remove( 'jquery' );

    $scripts->add(
        'jquery', // shortname
        'https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', // url
        false // dependancies (none)
    );
}

add_action( 'wp_default_scripts', 'radr_google_js' );

If you’re interested in using this on your own site, you’re welcome to download this quick plugin. Just follow standard procedure — unzip, copy to wp-content/plugins, and activate.

A while back I posted about automatically scrolling between internal links with some jQuery magic (which in turn was based on a tutorial by Learning jQuery) but one problem with it was that there was no way for the user to link to the anchor.

The problem was that because the function has to return false in order to keep the browser from jumping straight through to the anchor and skipping the animation. So I added another line to the code, taking after AJAX pseudo-permalinks, that adds the hash link (e.g. #anchor) back onto the URL in the address bar after a timeout matching the animation.

Here’s the line in question:

[sourcecode language="javascript"]setTimeout('window.location = window.location + "' + this.hash + '";', 1000);[/sourcecode]

Here’s the full code:

[sourcecode language="javascript"]$(function() {
    $('a[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) {
                var targetOffset = target.offset().top;
                $('html,body').animate({ scrollTop: targetOffset }, 1000);
                setTimeout('window.location = window.location + "' + this.hash + '";', 1000);
                return false;
            }
        }
    });
});[/sourcecode]

I came across an article on Learning jQuery about animating the scrolling between same-page links (i.e. anchors). Basically one block of code does this automatically, but I came across a problem that it was trying to do the same for anchors on other pages, which obviously wouldn’t work.

The line in question:

~~~ .prettyprint $(‘a[href*=#]’).click(function() {


This line selects all `a` elements with a hash (`#`) in its `href` attribute, but this also selects links to anchors on other pages. I changed it to this:

~~~ .prettyprint
$('a[href^=#]').click(function() {

So instead of matching a hash anywhere in the href attribute, this code only matches the hashes at the beginning. Other than that, the rest of the code is the same as in the article. You can see the effect here by clicking on the link in the footer to go back to the top of the page.