开发者

jQuery dynamic var change and dynamic if else checking

I want to create a var which changes it's value on click and then I want to check the value of the var using if and preform开发者_StackOverflow a series of animations. If the value of the var isn't lets say projects I use else to reverse the animations.

var url = "work";

if (url == "home") {
$("#homeBox").fadeIn(200)
}

$("#homeTab").click(function(){
var url = "home"
})

I will use this for a one-page website so the var values are basically the urls.

Thanks


If I understood what you need, this would solve it:

var url = "work";

function animate(){
  if (url == "home") {
    $("#homeBox").fadeIn(200);
  }
}
animate();

$("#homeTab").click(function(){
  url = "home";  //I removed 'var', because it would create a local variable.
  animate(); //Perform the animation/deanimation
})

Hope this helps. Cheers


I think you want to place your if check inside the click handler. Something like this:

var url = "work";

$("#homeTab").click(function(){
   var url = "home";

   if (url == "home") {
      $("#homeBox").fadeIn(200);
   }
   else {
      $("#homeBox").fadeOut(200);
   }

});

Also, don't forget those semicolons at the end of every line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜