Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCarlos Pals
814 PointsCan someone explain me this Jquery function?
var visible = false;
$("button").click(function() {
if (visible) {
$(".div").animate({
left: "-150px"
}, 300);
$("body").animate({
left:"0px"
}, 300);
} else {
$(".div").animate({
left: "0px"
}, 300);
$("body").animate({
left:"150px"
}, 300);
}
visible = !visible;
});
I don't understand the purpose of the var visible
2 Answers
Markus Ylisiurunen
15,034 PointsThere is a variable called visible
. And it's set to a boolean value of false
.
Then there is a function that will be executed when any <button>
of your page is clicked.
Inside of that function there is a if statement. This will be executed if the variable is set to value the true
. Inside of that if statement there are bunch of animation stuff going on. You can check what that method does on Here. After the if statement there is a line which changes the value of variable visible
. If visible
is currently false, after that line it will be true. If it's true it will change to false. So basically the if statement will be executed in every second click.
Hope this helps a little bit!
Chris Malcolm
2,909 Pointsthe button is a toggler for some kind of slideIn/slideOut element on the left. when button clicks, element slides in from the left (maybe a menu of 150px width). when clicked again, the menu slides back out of view (hence the negative positioning).
Tunde Adegoroye
20,597 PointsTunde Adegoroye
20,597 PointsIt's using true of false logic so if(visible) basically means if visible == true and the !visible means not visible which is the same as == false it's basically a true or false logic hope this helps :)