December 10, 2021
Блок сравнения картинок
Видео: https://youtu.be/oQJk2MSnQzo
<div class="comparison" id="compare-1">
<figure>
<div class="divisor"></div>
</figure>
<input type="range" min="0" max="100" value="50" class="slider" oninput="moveDivisor()">
</div>
<div class="comparison" id="compare-2">
<figure>
<div class="divisor"></div>
</figure>
<input type="range" min="0" max="100" value="50" class="slider" oninput="moveDivisor()">
</div>
<style>
.comparison {
background-color: red;
width: 100%;
margin: auto;
height: 400px;
max-width: 350px;
overflow: hidden;
margin-bottom: 25px;
}
.comparison figure {
position: relative;
font-size: 0;
width: 100%;
height: 100%;
margin: 0;
}
#compare-1 {
background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/photoshop-face-before.jpg);
background-size: auto 100%;
background-position: left top;
background-repeat: no-repeat;
}
#compare-1 div {
background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/photoshop-face-after.jpg);
background-size: auto 100%;
background-position: left top;
background-repeat: no-repeat;
}
#compare-2 {
background-image: url(https://cc26299.tmweb.ru/files/photo_2021-11-15_22-38-06%202.jpg);
background-size: auto 100%;
background-position: left top;
background-repeat: no-repeat;
height: 470px;
}
#compare-2 div {
background-image: url(https://cc26299.tmweb.ru/files/photo_2021-11-15_22-38-06%201.jpg);
background-size: auto 100%;
background-position: left top;
background-repeat: no-repeat;
}
.comparison figure > img {
position: relative;
width: 100%;
}
.comparison figure div {
position: absolute;
width: 100%;
box-shadow: 0 5px 10px -2px rgba(0,0,0,0.3);
overflow: hidden;
bottom: 0;
height: 100%;
}
input[type=range]{
-webkit-appearance:none;
-moz-appearance:none;
position: relative;
top: -2rem; left: -2%;
background-color: rgba(255,255,255,0.1);
width: 102%;
}
input[type=range]:focus {
outline: none;
}
input[type=range]:active {
outline: none;
}
input[type=range]::-moz-range-track {
-moz-appearance:none;
height:15px;
width: 98%;
background-color: rgba(255,255,255,0.1);
position: relative;
outline: none;
}
input[type=range]::active {
border: none;
outline: none;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance:none;
width: 20px; height: 15px;
background: #fff;
border-radius: 0;
}
input[type=range]::-moz-range-thumb {
-moz-appearance: none;
width: 20px;
height: 15px;
background: #fff;
border-radius: 0;
}
input[type=range]:focus::-webkit-slider-thumb {
background: rgba(255,255,255,0.5);
}
input[type=range]:focus::-moz-range-thumb {
background: rgba(255,255,255,0.5);
}
</style>
<script>
function moveDivisor() {
Array.from(document.querySelectorAll('.comparison')).map((comparison) => {
var divisor = comparison.querySelector(".divisor"),
slider = comparison.querySelector(".slider");
divisor.style.width = slider.value+"%";
});
};
</script>