This is a basic version of my function (first time I am doing this, so I’m sorry if I don’t explain it well)
var pages = {
init:function(){
},
home:function(){
articleid = this.closest("article").attr('id');
skillsbtn = $(this).hasClass("skillsbt");
home = $('#home');
homeheight = '-' + home.height();
if (skillsbtn && articleid == "home"){
home.animate({"marginTop" : homeheight},800);
}
},
work:function(){
//same variables are requred here: skillsbtn = $(this).hasClass("skillsbt");...
},
skills:function(){
//same variables are requred here: skillsbtn = $(this).hasClass("skillsbt");...
},
contact:function(){
},
}
So the idea is skillsbtn = $(this) refer to whatever $(this) is at home:function. I have tried to duplicate the variables from home:function to work:function and it wokeded fine. But can I put the variable I need in the init:function but be able to call them within the home:function and by doing so, be able to change what ever $(this) is, to mean this->home, or this->work
I have tried to access it like pages.init.skillsbtn.call(this) but it dies not work.
Hope you can help
– Thanks
You use global variables all over (where is the
varbefore each of the leading four assignments inhomefunction)?You want to share state and you do not use objects for this? That’s what they are for.
What is pages? If it is some kind of ‘repository’ or ‘namespace’, maybe use UpperCase name to distinguish it is not the plain instance.
Since
thisin jQuery events is bound to the source of the event, you cannot usethisplainly, but you can do this:If you are really sure you will only have one pages object, you can use pages.xxx to share variables between function calls and your original code. But that is not the right thing to do unless you are really, really sure.