after reading everywhere about the proper way to use jQuery and Wordpress: http://scribu.net/wordpress/optimal-script-loading.html http://www.ericmmartin.com/5-tips-for-using-jquery-with-wordpress/
I came up with this code:
First the functions.php
add_action('init', 'deregister_wp_jq');
add_action('init', 'register_my_jquery');
add_action('init', 'enqueue_my_jquery');
add_action('init', 'register_my_scripts');
add_action('wp_footer', 'print_my_scripts');
function deregister_wp_jq () {
wp_deregister_script('jquery');
}
function register_my_jquery ()
{
wp_register_script('my-jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", array(), '1.7.2', true);
wp_register_script('my-jquery-ui', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js", array('my-jquery'), '1.7.2', true);
}
function enqueue_my_jquery () {
wp_enqueue_scripts('my-jquery');
wp_enqueue_scripts('my-jquery-ui');
}
function register_my_scripts() {
wp_register_script('setNavigation', get_template_directory_uri() . '/js/jquery.setNavigation.js', array('my-jquery'), null, true);
wp_register_script('spasticNav', get_template_directory_uri() . '/js/jquery.spasticNav.mod.js', array('my-jquery', 'my-jquery-ui', 'setNavigation'), null, true);
}
function print_my_scripts() {
wp_print_scripts('spasticNav');
wp_print_scripts('setNavigation');
}
This is spasticNav, the plugin I want to use:
// JavaScript Document
(function($) {
$.fn.spasticNav = function(options) {
options = $.extend({
overlap : 20,
speed : 500,
reset : 1500,
color : '#b80606',
easing : 'easeOutExpo'
}, options);
return this.each(function() {
var nav = $(this), currentPageItem = $('#selected', nav), blob, reset;
$('<li id="blob"></li>').css({
width : currentPageItem.outerWidth(),
height : currentPageItem.outerHeight() + options.overlap,
left : currentPageItem.position().left,
top : currentPageItem.position().top - options.overlap / 2,
backgroundColor : options.color
}).appendTo(this);
blob = $('#blob', nav);
$('li:not(#blob)', nav).hover(function() {
// mouse over
clearTimeout(reset);
blob.animate({
left : $(this).position().left,
width : $(this).width()
}, {
duration : options.speed,
easing : options.easing,
queue : false
});
});
});
// end each
};
})(jQuery);
jQuery(function()
{
jQuery("#nav").spasticNav();
});
Then this is the jquery.setNavigation.js
function setNavigation() {
var path = window.location.pathname;
path = path.replace(/\/$/, "");
path = decodeURIComponent(path);
$(".nav a").each(function () {
var href = $(this).attr('href');
if (path.substring(0, href.length) === href) {
$(this).closest('li').addClass('active');
}
});
}
$(function () {
setNavigation();
});
I got a headache, backache, whatever-ache, spent hours and hours to fix this, but still, it does not work. Please, help me. I tried everything from different tutorials, etc., but nothing.
P.S.: I added the function to set the active state for
----------------------------------------- UPDATE -------------------------------
OK, so after other 12 hours or so passed searching on Google and debugging the code, finally I found the problem. It has to do with font-face as explained here: Lava Lamp navigation jQuery Firefox
This is the code that I am using now in functions.php
function my_scripts_method() { // Creates the my_scripts_method function
wp_deregister_script('jquery'); // Deregisters the built-in version of jQuery
wp_register_script('jquery', 'http' . ($_SERVER['SERVER_PORT'] == 443 ? 's' : '') . '://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', false, null, true); // Registers a CDN hosted version. If browsing on a secure connection, use HTTPS.
wp_enqueue_script('jquery'); // Activates the jQuery script
wp_register_script('jquery-ui', 'http' . ($_SERVER['SERVER_PORT'] == 443 ? 's' : '') . '://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js', false, null, true); // Registers a CDN hosted version. If browsing on a secure connection, use HTTPS.
wp_enqueue_script('jquery-ui'); // Activates the jQuery script
wp_register_script('spasticNav', get_template_directory_uri() . '/js/jquery.spasticNav.mod.js', false, null, true); // Registers your javascript file
wp_enqueue_script('spasticNav'); // Actives your javascript file
}
add_action('wp_enqueue_scripts', 'my_scripts_method'); // Tells WordPress to run the my_scripts_method function
And this is for the jquery.spasticNav.mod.js
// JavaScript Document
(function($) {
jQuery.fn.spasticNav = function(options) {
options = jQuery.extend({
overlap : 20,
speed : 500,
reset : 1500,
color : '#b80606',
easing : 'easeOutExpo'
}, options);
return this.each(function() {
var nav = jQuery(this), currentPageItem = jQuery('#selected', nav), blob, reset;
jQuery('<li id="blob"></li>').css({
width : currentPageItem.outerWidth(),
height : currentPageItem.outerHeight() + options.overlap,
left : currentPageItem.position().left,
top : currentPageItem.position().top - options.overlap / 2,
backgroundColor : options.color
}).appendTo(this);
blob = jQuery('#blob', nav);
jQuery('li:not(#blob)', nav).hover(function() {
// mouse over
clearTimeout(reset);
blob.animate({
left : jQuery(this).position().left,
width : jQuery(this).width()
}, {
duration : options.speed,
easing : options.easing,
queue : false
});
});
});
// end each
};
})(jQuery);
jQuery(function() {
jQuery.noConflict();
jQuery(document).ready(function() {
document.onreadystatechange = function () {
if (document.readyState == "complete") {
jQuery('#nav').spasticNav();}}});});
This is the error: Uncaught TypeError: Cannot read property 'left' of null
and it refers to these statements:
left : currentPageItem.position().left,
top : currentPageItem.position().top - options.overlap / 2
I hope you can help me now that i pinpointed the issue.
--------------------------------- UPDATE 2 - FIXED -----------------------------------
OK, it turns out that in the static version of the website, To feed something selected to spasticNav, I used:
<li> <a href="#acasa" id="selected">Acasa</a> </li>
WP provides already a script that changes automatically the status and it does so by adding the class .current_page_item Therefore, I modified this line accordingly:
var nav = jQuery(this), currentPageItem = jQuery('#selected', nav), blob, reset;
var nav = jQuery(this), currentPageItem = jQuery('.current_page_item', nav), blob, reset;