I have searched for this but my javascript knowledge is very basic so I might have missed the answer.
I have an image gallery that I want visible by default on desktop. When viewed on mobile, I want that gallery hidden by default, but still visible if a button is clicked.
I have created the mobile part, using some code I had for mobile navigation. That part works fine... but obviously since my button and image-gallery div are set to "display none" by default it's not going to show up on desktop. But I don't know how to make both things happen with the same content.
How could I make it visible on desktop, and an optional toggle on mobile? Thanks!
HTML:
<div class="container">
<button onclick="toggleGallery()">Click to view images</button>
<p>I want the hidden image-gallery div to show up here despite being display none.</p>
<div id="image-gallery" style="display:none;">
<ul>
<li>Image 1</li>
<li>Image 2</li>
<li>Image 3</li>
</ul>
</div>
</div><!--container-->
CSS:
#image-gallery {
display: block;
width: 600px;
border: 1px solid black;
}
#image-gallery li {
width: 150px;
height: 150px;
color: white;
text-align: center;
margin: 10px;
background-color: gray;
display: inline-block;
}
button {
display: none;
}
@media (max-width: 600px) {
#image-gallery {
display: block;
width: 100%;
}
button {
display: block;
}
#image-gallery li {
border: 1px solid red;
}
}
JS:
function toggleGallery() {
var x = document.getElementById('image-gallery');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}