Issue:
I just started using javascript/jQuery with little experience. I had working code, but after a little bit of restructuring, everything died.
Original Code:
// Floating NavBar - Side
var names = ['#floatMenu','#header'];
for (var i = 0; i < names.length; i++){
floatObj(names[i])
}
function floatObj(name)
{
var menuYloc = null;
$(document).ready(function(){
menuYloc = parseInt($(name).css('top').substring(0,$(name).css('top').indexOf('px')));
$(window).scroll(function(){
var offset = menuYloc + $(document).scrollTop() + 'px';
$(name).animate({top:offset},{duration:0,queue:false});
});
});
};
Re-structured Code
$(document).ready(function(){
floatObj();
});
function floatObj()
{
var names = ['#floatMenu','#header'];
var menuYloc = null;
for (var i = 0; i < names.length; i++){
menuYloc = parseInt($(names[i]).css('top').substring(0,$(names[i]).css('top').indexOf('px')));
$(window).scroll(function(){
var offset = menuYloc + $(document).scrollTop() + 'px';
$(names[i]).animate({top:offset},{duration:0,queue:false});
});
};
};
Question:
I was wondering if someone could point out why restructuring the code this way doesn't work? I was also wondering if there was a method of debugging javascript without any additional add-ons? (It would also be helpful if glaring mistakes are pointed out.)
Reason why.
The reason I want to re-structure my code this way is because I have some other functions I would like to run at load up. I figured I could throw all the functions into the $(document).ready(function(){}) bit. If there's actually a proper way of doing this, please enlighten me ><.
Any help would be greatly appreciated! Thanks.