I have followed this tutorial to create a before/ after slider on a page:
I was able to get one working but i am unable to make it work with a second. Here is my code:
https://codepen.io/acodeaday/pen/qBYrdbJ
HTML
<!-- Image 1 -->
<div class="containerBA">
<div class="image-container">
<img
class="image-before slider-image"
src="/images/conservatoryBefore.jpg"
alt="before"
/>
<img
class="image-after slider-image"
src="/images/conservatoryAfter.jpg"
alt="after"
/>
</div>
<!-- step="10" -->
<input
type="range"
min="0"
max="100"
value="50"
aria-label="Percentage of before photo shown"
class="slider"
/>
<div class="slider-line" aria-hidden="true"></div>
<div class="slider-button" aria-hidden="true">
<svg
xmlns="http://www.w3.org/2000/svg"
width="30"
height="30"
fill="currentColor"
viewBox="0 0 256 256"
>
<rect width="256" height="256" fill="none"></rect>
<line
x1="128"
y1="40"
x2="128"
y2="216"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
></line>
<line
x1="96"
y1="128"
x2="16"
y2="128"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
></line>
<polyline
points="48 160 16 128 48 96"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
></polyline>
<line
x1="160"
y1="128"
x2="240"
y2="128"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
></line>
<polyline
points="208 96 240 128 208 160"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
></polyline>
</svg>
</div>
</div>
<!-- Image 2 -->
<div class="containerBA2">
<div class="image-container2">
<img
class="image-before2 slider-image2"
src="/images/conservatoryBefore2.jpg"
alt="before"
/>
<img
class="image-after2 slider-image2"
src="/images/conservatoryAfter2.jpg"
alt="after"
/>
</div>
<!-- step="10" -->
<input
type="range"
min="0"
max="100"
value="50"
aria-label="Percentage of before photo shown"
class="slider2"
/>
<div class="slider-line2" aria-hidden="true"></div>
<div class="slider-button2" aria-hidden="true">
<svg
xmlns="http://www.w3.org/2000/svg"
width="30"
height="30"
fill="currentColor"
viewBox="0 0 256 256"
>
<rect width="256" height="256" fill="none"></rect>
<line
x1="128"
y1="40"
x2="128"
y2="216"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
></line>
<line
x1="96"
y1="128"
x2="16"
y2="128"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
></line>
<polyline
points="48 160 16 128 48 96"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
></polyline>
<line
x1="160"
y1="128"
x2="240"
y2="128"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
></line>
<polyline
points="208 96 240 128 208 160"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
></polyline>
</svg>
</div>
</div>
CSS
/* Image 001 */
.image-container img {
display: block;
max-width: 100%;
}
main {
display: grid;
place-items: center;
min-height: 100vh;
}
.containerBA {
display: grid;
place-content: center;
position: relative;
overflow: hidden;
border-radius: 1rem;
--position: 50%;
}
.image-container {
max-width: 800px;
max-height: 90vh;
/* aspect-ratio: 1/1; */
}
.slider-image {
width: 100%;
height: 100%;
object-fit: cover;
object-position: left;
}
.image-before {
position: absolute;
inset: 0;
width: var(--position);
/* filter: grayscale(100%) */
}
.slider {
position: absolute;
inset: 0;
cursor: pointer;
opacity: 0;
/* for Firefox */
width: 100%;
height: 100%;
}
.slider:focus-visible ~ .slider-button {
outline: 5px solid black;
outline-offset: 3px;
}
.slider-line {
position: absolute;
inset: 0;
width: .2rem;
height: 100%;
background-color: #fff;
/* z-index: 10; */
left: var(--position);
transform: translateX(-50%);
pointer-events: none;
}
.slider-button {
position: absolute;
background-color: #fff;
color: black;
padding: .5rem;
border-radius: 100vw;
display: grid;
place-items: center;
top: 50%;
left: var(--position);
transform: translate(-50%, -50%);
pointer-events: none;
/* z-index: 100; */
box-shadow: 1px 1px 1px hsl(0, 50%, 2%, .5);
}
/* Image 002 */
.containerBA2 {
display: grid;
place-content: center;
position: relative;
overflow: hidden;
border-radius: 1rem;
--position: 50%;
}
.image-container2 {
max-width: 800px;
max-height: 90vh;
/* aspect-ratio: 1/1; */
}
.slider-image2 {
width: 100%;
height: 100%;
object-fit: cover;
object-position: left;
}
.image-before2 {
position: absolute;
inset: 0;
width: var(--position);
/* filter: grayscale(100%) */
}
.slider2 {
position: absolute;
inset: 0;
cursor: pointer;
opacity: 0;
/* for Firefox */
width: 100%;
height: 100%;
}
.slider2:focus-visible ~ .slider-button2 {
outline: 5px solid black;
outline-offset: 3px;
}
.slider-line2 {
position: absolute;
inset: 0;
width: .2rem;
height: 100%;
background-color: #fff;
/* z-index: 10; */
left: var(--position);
transform: translateX(-50%);
pointer-events: none;
}
.slider-button2 {
position: absolute;
background-color: #fff;
color: black;
padding: .5rem;
border-radius: 100vw;
display: grid;
place-items: center;
top: 50%;
left: var(--position);
transform: translate(-50%, -50%);
pointer-events: none;
/* z-index: 100; */
box-shadow: 1px 1px 1px hsl(0, 50%, 2%, .5);
}
JS
const container = document.querySelector('.containerBA');
document.querySelector('.slider').addEventListener('input', (e) => {
container.style.setProperty('--position', `${e.target.value}%`);
})
// My js edit 002
const container2 = document.querySelector('.containerBA2');
document.querySelector('.slider2').addEventListener('input', (e) => {
container.style.setProperty('--position', `${e.target.value}%`);
})
if you look at the sliders underneath the code, the top slider works but the bottom slider controls the top slider.
I've tried to go through and change every reference and class, so i really can't figure out where the overlap is coming from.
I thought it could possibly be coming from --position, but i tried to change it to --position2 but that broke it, im not really sure how to separate that if that is the problem.
Thank you for any help.