I have a div
called img-with-overlay
that contains a static-positioned image and an absolute-positioned overlay. When img-with-overlay
is hovered over, the overlay goes from 0
opacity to 1
. An unintended side-effect of this is that the vertical scrollbar disappears when the overlay is opaque, in Chrome and Chromium-based browsers. It does not occur in Firefox.
Why is the scrollbar there, you ask? You can see that there is a container
with a very specific width
and height
. If these dimensions are altered by even half a pixel, the unintended side-effect will not occur. I'm using this container to represent the body
of my webpage, although I have shrunk it for the purposes of fitting into a snippet. While most screen sizes will not present an issue, I still want to prevent it from over occurring.
$(document).ready(function () {
$('.img-with-overlay').hover(function () {
$('.overlay').css('opacity',1);
}, function () {
$('.overlay').css('opacity',0);
})
});
.container {
width: 600px;
height: 248.5px;
overflow: auto;
overflow-x: hidden;
}
.img-with-overlay {
position: relative;
overflow: hidden;
width: 100%;
}
img {
display: block;
width: 100%;
}
.overlay {
position: absolute;
top: 0;
opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="img-with-overlay">
<img src="https://via.placeholder.com/468x200?text=Hover">
<div class="overlay">This is an overlay</div>
</div>
</div>
Why is the scrollbar disappearing/reappearing when the opacity of the overlay changes?
Some interesting things I've noted:
- If the
img
is replaced with adiv
of the same dimensions, the scrollbar does not appear. - If the overlay opacity is set to a value less than 1, such as 0.7, the scrollbar will not disappear on hover.