Reputation: 147
I try to make image gallery with openable image, but the problem is when a image is opened and monitor or browser window height is small, the top side of image is hidden. Any option with overflow property does not work for me. My current code to example is below. Any suggestions how to fix or what I do wrong ?
function openModal() {
document.getElementById('myModal').style.display = "flex";
document.body.style.overflow = "hidden";
}
function closeModal() {
document.getElementById('myModal').style.display = "none";
document.body.style.overflow = "visible";
}
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex-1].style.display = "flex";
var modal = document.getElementById("modalContent");
var divImg = slides[slideIndex-1];
var img = divImg.getElementsByTagName('img')[0];
var imgWidth = img.width;
var imgHeight = img.height;
modal.style.width = imgWidth + "px";
modal.style.height = imgHeight + "px";
document.onkeydown = function(x) {
if (document.getElementById('myModal').style.display == "flex") {
x = x || window.event;
if (x.keyCode == '37') {
plusSlides(-1)
} else if (x.keyCode == '39') {
plusSlides(1)
}
}
}
}
/* Center gallery */
.row {
margin: 0 auto 30px;
text-align: center;
}
/* Padding in gallery */
.row > .column {
padding: 16px 8px;
}
.column {
width: 320px;
height: 320px;
display: inline-block;
overflow: hidden;
}
/* The Modal (background) */
.modal {
display: none;
position: fixed;
z-index: 4;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.9);
}
/* Modal Content */
.modal-content {
position: relative;
margin: 0 auto ;
padding: 0;
align-self: center;
}
/* The Close Button */
.close {
color: white;
position: absolute;
top: 10px;
right: 25px;
font-size: 35px;
font-weight: bold;
}
/* Close hover */
.close:hover,
.close:focus {
color: #999;
text-decoration: none;
cursor: pointer;
}
/* Slide iamge */
.mySlides {
display: none;
justify-content: center;
}
/* Pointer cursor */
.cursor {
cursor: pointer
}
/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
color: white;
font-weight: bold;
font-size: 20px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-user-select: none;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* Removing white line on bottom of image in gallery */
.mySlides img {
width: auto !important;
max-height: 720px;
max-width: 720px;
}
/* Shadow and opacity animation */
img.hover-shadow {
transition: 0.4s;
}
/* Shadow and opacity of gallery image */
.hover-shadow:hover {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
opacity: 0.8;
}
<div class="row">
<div class="column">
<img src="https://www.smashingmagazine.com/wp-content/uploads/2015/06/10-dithering-opt.jpg" style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="https://amazingslider.com/wp-content/uploads/2012/12/dandelion.jpg" style="width:100%" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="http://www.qygjxz.com/data/out/114/5838619-image.png" style="width:100%" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="https://camo.mybb.com/e01de90be6012adc1b1701dba899491a9348ae79/687474703a2f2f7777772e6a71756572797363726970742e6e65742f696d616765732f53696d706c6573742d526573706f6e736976652d6a51756572792d496d6167652d4c69676874626f782d506c7567696e2d73696d706c652d6c69676874626f782e6a7067" style="width:100%" onclick="openModal();currentSlide(4)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="https://www.w3schools.com/css/img_lights.jpg" style="width:100%" onclick="openModal();currentSlide(5)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="http://www.jqueryscript.net/images/jQuery-Plugin-For-Fullscreen-Image-Viewer-Chroma-Gallery.jpg" style="width:100%" onclick="openModal();currentSlide(6)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="https://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg" style="width:100%" onclick="openModal();currentSlide(7)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="https://www.w3schools.com/w3css/img_avatar3.png" style="width:100%" onclick="openModal();currentSlide(8)" class="hover-shadow cursor">
</div>
</div>
<!--Modal image slide show-->
<div id="myModal" class="modal">
<span class="close cursor" onclick="closeModal()">×</span>
<div class="modal-content" id="modalContent">
<div class="mySlides">
<div class="numbertext">1 / 8</div>
<img src="https://www.smashingmagazine.com/wp-content/uploads/2015/06/10-dithering-opt.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">2 / 8</div>
<img src="https://amazingslider.com/wp-content/uploads/2012/12/dandelion.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">3 / 8</div>
<img src="http://www.qygjxz.com/data/out/114/5838619-image.png" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">4 / 8</div>
<img src="https://camo.mybb.com/e01de90be6012adc1b1701dba899491a9348ae79/687474703a2f2f7777772e6a71756572797363726970742e6e65742f696d616765732f53696d706c6573742d526573706f6e736976652d6a51756572792d496d6167652d4c69676874626f782d506c7567696e2d73696d706c652d6c69676874626f782e6a7067" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">5 / 8</div>
<img src="https://www.w3schools.com/css/img_lights.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">6 / 8</div>
<img src="http://www.jqueryscript.net/images/jQuery-Plugin-For-Fullscreen-Image-Viewer-Chroma-Gallery.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">7 / 8</div>
<img src="https://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">8 / 8</div>
<img src="https://www.w3schools.com/w3css/img_avatar3.png" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
</div>
</div>
Upvotes: 1
Views: 1841
Reputation: 105
Well from the snippet i would say that the Overflow: hidden;
property in .column
might cause some trouble try deleting it or set it to Overflow: scroll;
Edit: The Overflow: ;
has to be either Overflow-x: ;
or Overflow-y: ;
depending on the axis.
Edit: document.body.style.overflow = "hidden";
in your openmodal function Might also need the right axis (I'm bad at functions but it's worth a shot ^^)
Upvotes: 0
Reputation: 9873
One line fix: remove width: auto!important
from the .mySlides img
css.
Then the image will scale to the screen width when the modal is open.
EDIT: Change display: flex
to display: block
on your modal. This solves your height problem on smaller height screens. But now your content is not vertically centered. You are trying to use flexbox in the midst of other things, and it is not meant for that. This is why you are getting strange behavior.
To mitigate this problem, use a media query or javascript to explicitly set the display: flex
to display: block
on smaller screen heights, because vertically aligning doesn't make sense there anyways since the image will always be greater than the screen height.
This gives you the best of both worlds:
margin-top
or something to add some space between top of screen and image.Upvotes: 2