"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > CSS의 간단한 이미지 비교

CSS의 간단한 이미지 비교

2024-11-04에 게시됨
검색:876

CSS에서 이미지 비교를 위한 간단한 방법

Simple Image Comparison in CSS

입력 범위 슬라이더와 그 아래에 클래스 이름이 '.img-before-after인 상위 div 내부에 클래스 이름이 .front, .back인 두 개의 div를 만들 수 있습니다. '. 인라인 스타일 width:50%.front div
에 할당

img-before-after, 입력 범위, input-slider-thumb, , 뒤쪽에

body {
    background: #d6d6d6;
}

.img-before-after {
    position: relative;
    width: 900px;
    height: 600px;
}

input[type="range"] {
    background: transparent;
    width: 100%;
    height: 100%;
    margin: 0;
    outline: none;
    position: absolute;
    z-index: 2;
    -webkit-appearance: none;
}

input[type="range"]::-webkit-slider-thumb {
    width: 10px;
    height: 600px;
    cursor: pointer;
    -webkit-appearance: none;
    background: black;
}

.front.back div에 배경 이미지를 추가합니다.

.front, .back {
    position: absolute;
    width: 100%;
    height: 600px;
    background: url("https://shorturl.at/kbKhz") no-repeat;
    background-size: cover;
    z-index: 1;
}

z-index를 사용하여 .front div 뒤에 .back div를 보내고 회색조로 만듭니다.

.back {
    filter: grayscale(1);
    z-index: 0;
}

입력 슬라이더를 드래그할 때 '.front' div의 너비를 동적으로 늘리거나 줄여야 합니다. '.front' div
의 너비에 입력 범위 값을 추가해야 합니다.

oninput="this.nextElementSibling.style.width = `${this.value}%`"

산출:

Simple Image Comparison in CSS

아래에서 슬라이더 범위를 드래그할 때 개발 도구에서 너비가 어떻게 증가하고 감소하는지 확인할 수 있습니다.

Simple Image Comparison in CSS

아래와 같이 흐림, 반전 등과 같은 CSS의 다양한 변형을 시도해 볼 수 있습니다.

흐림

.back {
    filter: blur(5px);
    z-index: 0;
}

Simple Image Comparison in CSS

거꾸로 하다

.back {
    filter: invert(1);
    z-index: 0;
}

Simple Image Comparison in CSS

최종 출력: 회색조 포함

Simple Image Comparison in CSS

시청해주셔서 감사합니다...

릴리스 선언문 이 글은 https://dev.to/prahalad/simple-image-comparison-in-css-48fd?1에서 복제됩니다.1 침해 내용이 있는 경우, [email protected]으로 연락하여 삭제하시기 바랍니다.
최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3