- In my current project, I am faced with the challenge of smoothly resizing container divs as users switch between image or video assets of varying sizes/heights within a Bootstrap carousel.
Currently, the resizing process appears to be abrupt and not visually appealing, giving a "jumping" effect from one image size to another, which is causing a jarring experience.
I have attempted different methods such as using percentage sizing, vh vw units, and exploring transitions/ease effects, but I have not been successful in achieving the desired smooth resizing behavior. Any guidance or suggestions on how to solve this issue would be greatly appreciated.
- While the content resizing seems to work well for wider images or videos, I am facing difficulties when dealing with taller assets. Is it possible to maintain proportional resizing for tall images as well, or do I need to prioritize one over the other? Currently, tall images get cropped when the browser window is made shorter, and I would like to explore options that allow the assets to scale down without being cut off.
Below is the snippet of the current code being used:
$(document).ready(function() {
function adjustContainerSize() {
$('.responsive-container').each(function() {
var maxWidth = $(this).parent().width();
var maxHeight = $(window).height() * 0.9; // 90% of viewport height
$(this).css({
'max-width': maxWidth,
'max-height': maxHeight
});
});
}
adjustContainerSize();
$(window).resize(function() {
adjustContainerSize();
});
});
body {
background-color: violet;
margin: 0;
/* Reset margin to 0 */
padding: 0;
/* Reset padding to 0 */
}
.container {
background-color: green;
margin-bottom: 50px;
padding: 0px;
display: flex;
justify-content: center;
align-items: center;
}
.slideshow-container {
position: relative;
width: 100%;
margin: auto;
max-width: 100%;
max-height: 100%;
overflow: hidden;
background-color: #b9dbe5;
display: flex;
justify-content: center;
align-items: center;
}
.carousel-item {
text-align: center;
/* Center images horizontally */
}
.slideshow-container img,
.slideshow-container iframe {
max-width: 100%;
max-height: 100%;
object-fit: contain;
}
/* Show arrows only on hover */
.slideshow-container:hover .prev,
.slideshow-container:hover .next {
display: block;
}
.prev,
.next {
display: none;
cursor: default;
/* Change cursor to default */
z-index: 1000;
color: silver;
font-weight: bold;
font-size: 30px;
position: absolute;
top: 50%;
transform: translateY(-50%);
width: auto;
padding: 16px;
border-radius: 0 3px 3px 0;
user-select: none;
text-decoration: none;
/* Remove underline */
}
.prev:hover,
.next:hover {
text-decoration: none;
/* Remove underline */
color: silver;
/* Change color on hover */
}
.prev {
left: 0;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.prev.disabled,
.next.disabled {
pointer-events: none;
opacity: 0.5;
}
/* Responsive YouTube Video */
.video-container {
position: relative;
width: 90%;
/* Adjust the width as needed */
padding-bottom: 56.25%;
/* 16:9 Aspect Ratio (iframe video) */
overflow: hidden;
margin: auto;
/* Center the video */
background-color: #b1e1af;
/* light green */
}
.video-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
/* Responsive container dimensions */
.responsive-container {
max-width: 90vw;
max-height: 90vh;
}
/* Media queries for different viewport sizes */
@media (min-width: 768px) {
.responsive-container {
max-width: 750px;
max-height: 600px;
}
}
@media (min-width: 992px) {
.responsive-container {
max-width: 970px;
max-height: 700px;
}
}
@media (min-width: 1200px) {
.responsive-container {
max-width: 1170px;
max-height: 800px;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
<div id="myCarousel1" class="slideshow-container carousel slide responsive-container" data-ride="carousel" data-interval="false">
<!-- Image Slides -->
...
<!-- Video Slide -->
...
...
</div>
...
...
...
...
</div>
</div>
...
...
...
...
</div>
</div>
<!-- Bootstrap JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>