首頁 > web前端 > css教學 > 主體

CSS 中的簡單圖片比較

Mary-Kate Olsen
發布: 2024-10-01 06:13:29
原創
329 人瀏覽過

CSS 中影像比較的簡單技巧

Simple Image Comparison in CSS

讓我們建立輸入範圍滑桿及其下面的兩個div,其類別名稱為.front.back 在父div 內,類別名為'.img-before-after '。將內嵌樣式 width:50% 分配給 .front div

<div class="img-before-after">
    <input type="range" class="range" value="50">
    <div class="front" style="width: 50%;"></div>
    <div class="back"></div>
</div>
登入後複製

img-before-afterinput-rangeinput-slider-thumbfront返回

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;
}
登入後複製

讓我們將 .back div 送到 .front div 後面,並使用 z-index
並將其設為灰階。

.back {
    filter: grayscale(1);
    z-index: 0;
}
登入後複製

當我們拖曳輸入滑桿時,我們需要動態增加/減少 '.front' div 的寬度。我們必須將輸入範圍值附加到 '.front
' div 的寬度。

oninput="this.nextElementSibling.style.width = `${this.value}%`"
登入後複製
<div class="img-before-after">
   <input type="range" class="range" value="50"
       oninput="this.nextElementSibling.style.width = `${this.value}%`">
   <div class="front" style="width: 50%;"></div>
   <div class="back"></div>
</div>
登入後複製

輸出:

Simple Image Comparison in CSS

下面您可以看到當我們拖曳滑桿範圍時,開發工具中的寬度如何增加和減少。

Simple Image Comparison in CSS

您可以嘗試使用 CSS 的不同變體,例如 blurinvert

等,如下所示。

模糊

.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

感謝您的觀看...

以上是CSS 中的簡單圖片比較的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!