I want to create a section similar to apples mac mini section (image below)
Essentially 4 buttons that hide / reveal text beneath.
https://www.apple.com/mac-mini/
(https://i.stack.imgur.com/OomgP.png)
Currently with the code below, when you click again on the same button it disappears, I don’t want this to happen. I’m new to javascript so not sure how to eliminate this / if possible.
Thanks
<button class="icon1">Button 1</button>
<button class="icon2">Button 2</button>
<button class="icon3">Button 3</button>
<button class="icon4">Button 4</button>
<p class="info info1">Toggle 1</p>
<p class="info info2">Toggle 2</p>
<p class="info info3">Toggle 3</p>
<p class="info info4">Toggle 4</p>
</div>
.info2, .info3, .info4 {
grid-area: scrn;
display: none;
}
/* decorative only */
body {
font-size: 4vmin;
text-align: center;
}
button {
font-size: 2.5vmin;
}
$(function() {
// select the button to click, this can be any element not just a button
$('.icon1').click(function() {
// select which info panel to show/hide.
$('.info1').toggle();
// hide any info panels that are not info1.
$('.info').not('.info1').hide();
});
});
$(function() {
$('.icon2').click(function() {
$('.info2').toggle();
$('.info').not('.info2').hide();
});
});
$(function() {
$('.icon3').click(function() {
$('.info3').toggle();
$('.info').not('.info3').hide();
});
});
$(function() {
$('.icon4').click(function() {
$('.info4').toggle();
$('.info').not('.info4').hide();
});
});
4
Answers
You probably want something like this for each button:
Instead of using
toggle()
, useshow()
instead. toggle means it will show or hide the element. If it’s currently being shown and you calltoggle()
it will be hidden and the other way around if it’s hidden.With a condition it work in the way i understood u need, but u can find other way with jquery to animate or maybe you can use a library like mo.js or gsap…
https://api.jquery.com/toggle/ (
is equivalent to:
}
)
This is the way toggle is meant to be used
By your description, you seem to be trying to create an accordion… Which basically is some title that is shown and the content is hidden.
There is a pretty clear example listed w3schools website.
There is also an option, if you are targetting HTML 5 capable browsers only without javascript that is illustratedhere
Hope this helps you… Best regards!