I am looking for a way to have my background-image
transition smoothly, even when it has a different aspect ratio predecessor. Below is an example demonstrating this stretching effect.
toggle = true
jQuery(document).ready(function() {
jQuery(".button").on("click", function() {
if (toggle) {
jQuery(".test").css("background-image", "url('https://images.pexels.com/photos/4534200/pexels-photo-4534200.jpeg?cs=srgb&dl=pexels-arthouse-studio-4534200.jpg&fm=jpg')")
toggle = false
} else {
jQuery(".test").css("background-image", "url('https://images.unsplash.com/photo-1547922374-968968e3f658?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&dl=bosco-shots-SlR66yjPsoI-unsplash.jpg')")
toggle = true
}
})
})
* {
margin: 0;
padding: 0;
}
.test {
width: 100vw;
height: 100vh;
background-image: url('https://images.unsplash.com/photo-1547922374-968968e3f658?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&dl=bosco-shots-SlR66yjPsoI-unsplash.jpg');
transition: background-image 0.5s;
-webkit-transition: background-image 0.5s;
-o-transition: background-image 0.5s;
-moz-transition: background-image 0.5s;
-ms-transition: background-image 0.5s;
background-position: center center;
background-size: cover;
background-repeat: no-repeat;
}
.button {
padding: 2px;
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="test">
<input class="button" type="button" value="change">
</div>
If there is an option available that allows me to still use background-size: cover;
and achieve a smooth background-image
transition, I would greatly appreciate it.