I have a jQuery function using a series of "if" and "else" statements. I am wondering if there is a more elegant way I can code this. I am thinking I could create and array with my "hashtag names" and then loop it through a function. However I am not quiet sure how to go about doing that. Here is my code...
setTimeout(function() {
var i = 0;
if(window.location.hash == "#" + "somename"){
var i = 0;
return scroll(i);
}
else if(window.location.hash == "#" + "somethingelse"){
var i = 1;
return scroll(i);
}
else if(window.location.hash == "#" + "someotherword"){
var i = 2;
return scroll(i);
}
else if (window.location.hash == "#" + "sometext"){
var i = 3;
return scroll(i);
}
}, 500 );
EDIT: Some more code
function scroll(i){
var title = $('.title').eq(i);
var top = $(title).offset().top - 50 ;
$('html,body').animate({ scrollTop: top }, 'slow');
}
Basically what I am doing is checking to see what hashtag is in the URL, and then scrolling to the part of the page that is related to that hashtag. This method works, but I would like to optimize the "if/else" part of my code if possible.
Any help is appreciated. An array with a loop may not be the best approach. I am open to any ideas. Thanks.