I am not sure on how to solve this problem in JavaScript, so I tought about "dynamic variables". If there is any better way to do it, please let me know!
Given is a function with one parameter (left or right):
var footerMenuLeftOpen = true;
var footerMenuRightOpen = true;
function footerMenu(menuName, className) {
if (menuName) {
$('.content-' + className).animate({
height: "+=28px",
}, {
duration: 100
});
return false;
} else {
$('.content-' + className).animate({
height: "-=28px",
}, {
duration: 100
});
return true;
}
}
$(".content-left").click(function() {
footerMenuLeftOpen = footerMenu(footerMenuLeftOpen, 'left');
});
$(".content-right").click(function () {
footerMenuRightOpen = footerMenu(footerMenuRightOpen, 'right');
});
.content-left {
width: 200px;
height: 100px;
background-color: red;
display: inline-block;
}
.content-right {
width: 200px;
height: 100px;
background-color: blue;
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content-left">
</div>
<div class="content-right">
</div>
Now I want to add code, that the change in the height (+/- 28px) depends on the height of the red or blue box. Therefore I want to check for the starting height of the boxes as soon the boxes are displayed.
For example:
if ($('.content-left').height() > 42) {
LeftHeight: "+=28px",
} else if ($('.footer-button-box.left').height() > 28) {
LeftHeight: "+=14px",
} else {
LeftHeight: "+=5px",
}
But how do I now change the height-value of the function "footerMenu" so that the height changes regarding to the determined values from the if-function?
I tought about something like
$('.content-' + className).animate({
height: className+Height,
}, {
duration: 100
});
... where className would be "left" plus the term "Height"; so leftHeight.
I guess there is a way better solution to make that work only one elegant function?
Thanks in advance!
EDIT: Since my question is hard to understand, in simple words:
I have variables "leftHeight" and "rightHeight". Now: how can I access these variables from a function where left/right is the value of the parameter?
Goal: height: leftHeight
Idea: height: className+"Height"
... so how to use the value of the parameter of the function as a part of a variable name?
ifis not a function and doesn't return anything. Use ternary operators, and place them in the location of the property value.