On page 77 of jQuery: Novice to Ninja they have the following excerpt;
var $window = $(window).
$navigation = $(“#navigation”);
…
What I cannot for the life of me is figure out why it ONLY works if I have $window instead of window.
// This would not work
var window = $(window),
navigation = $(‘#navigation’);
window.scroll(function() {
navigation.css(‘top’, $(document).scrollTop());
});
// This will work
var $window = $(window),
navigation = $(‘#navigation’);
$window.scroll(function() {
navigation.css(‘top’, $(document).scrollTop());
});
Can someone help me understand why? I’ve read where preceding a var with the $ is common practice if the variable contains a jQuery object, but why will the window object only work if I precede the variable with the $?
I have a JSFiddle here: http://jsfiddle.net/ryentzer/3hAvm/
Scratching head,
Rick