08Cross-fade hai ảnh
Ảnh thứ hai xếp chồng cùng vị trí mờ dần hiện lên trên ảnh đầu qua opacity, đổi sang cảnh khác.
Dùng choẢnh sản phẩm before/after, xem trước tùy chọn màu
opacity 교차position:absolute0.4초

<main class="stage">
<figure class="photo is-demo" tabindex="0">
<span class="photo__frame" aria-hidden="true">
<span class="photo__a">
<span class="photo__sun"></span>
<span class="photo__hill"></span>
</span>
<span class="photo__b">
<span class="photo__sun photo__sun--b"></span>
<span class="photo__hill photo__hill--b"></span>
</span>
</span>
<figcaption class="photo__cap">Tùy chọn màu</figcaption>
</figure>
</main>@use "../../../../site/static/demo-tokens" as *;
$duration: $dur-base; // 두 번째 사진이 번지는 시간
$easing: $ease-out;
$color: $subject-ink; // 사진 프레임 색
@include stage($stage-paper);
.stage { padding: clamp(6px, 2.4vw, 16px); } // 320px 대응: 데모 카드가 작아 여백도 줄인다
.photo {
margin: 0;
width: min(160px, 50vw);
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
border: 3px solid $color;
outline: none;
cursor: pointer;
}
.photo__frame {
display: block;
position: relative;
aspect-ratio: 4 / 3;
}
.photo__a,
.photo__b {
position: absolute;
inset: 0;
}
.photo__a {
background: linear-gradient(155deg, $subject-cream 0%, $subject-blue 100%);
}
.photo__b {
background: linear-gradient(155deg, $subject-blue 0%, $subject-ink 100%);
opacity: 0; // 평소엔 첫 사진만 보인다
transition: opacity $duration $easing;
}
.photo__sun {
position: absolute;
top: 16%;
right: 18%;
width: 20%;
aspect-ratio: 1;
border-radius: 50%;
background: $stage-yellow;
}
.photo__sun--b { background: $subject-cream; }
.photo__hill {
position: absolute;
left: -8%;
right: -8%;
bottom: -10%;
height: 48%;
border-radius: 50% 50% 0 0 / 100% 100% 0 0;
background: $subject-ink;
opacity: .85;
}
.photo__hill--b { background: $stage-orange; }
.photo__cap {
word-break: keep-all;
margin: 0;
padding: 10px 14px;
font-weight: 600; font-size: clamp(12px, 3.4vw, 14px); line-height: 1.3; font-family: inherit;
color: $subject-cream;
background: $color;
}
.photo:hover .photo__b,
.photo:focus-visible .photo__b,
.photo:active .photo__b {
opacity: 1;
}
.photo.is-demo .photo__b {
animation: cross-loop $dur-loop $easing infinite;
}
@keyframes cross-loop {
0%, 20% { opacity: 0; }
50%, 75% { opacity: 1; }
100% { opacity: 0; }
}
@media (hover: none) {
.photo:active .photo__b { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
.photo__b { transition: none; }
.photo.is-demo .photo__b { animation: none; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ffffff;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.stage {
padding: clamp(6px, 2.4vw, 16px);
}
.photo {
margin: 0;
width: min(160px, 50vw);
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
border: 3px solid #17141a;
outline: none;
cursor: pointer;
}
.photo__frame {
display: block;
position: relative;
aspect-ratio: 4/3;
}
.photo__a,
.photo__b {
position: absolute;
inset: 0;
}
.photo__a {
background: linear-gradient(155deg, #fff7e6 0%, #2f6df6 100%);
}
.photo__b {
background: linear-gradient(155deg, #2f6df6 0%, #17141a 100%);
opacity: 0;
transition: opacity 300ms ease-out;
}
.photo__sun {
position: absolute;
top: 16%;
right: 18%;
width: 20%;
aspect-ratio: 1;
border-radius: 50%;
background: #ffd23f;
}
.photo__sun--b {
background: #fff7e6;
}
.photo__hill {
position: absolute;
left: -8%;
right: -8%;
bottom: -10%;
height: 48%;
border-radius: 50% 50% 0 0/100% 100% 0 0;
background: #17141a;
opacity: 0.85;
}
.photo__hill--b {
background: #ff4d1f;
}
.photo__cap {
word-break: keep-all;
margin: 0;
padding: 10px 14px;
font-weight: 600;
font-size: clamp(12px, 3.4vw, 14px);
line-height: 1.3;
font-family: inherit;
color: #fff7e6;
background: #17141a;
}
.photo:hover .photo__b,
.photo:focus-visible .photo__b,
.photo:active .photo__b {
opacity: 1;
}
.photo.is-demo .photo__b {
animation: cross-loop 2s ease-out infinite;
}
@keyframes cross-loop {
0%, 20% {
opacity: 0;
}
50%, 75% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@media (hover: none) {
.photo:active .photo__b {
opacity: 1;
}
}
@media (prefers-reduced-motion: reduce) {
.photo__b {
transition: none;
}
.photo.is-demo .photo__b {
animation: none;
}
}import type { CSSProperties } from "react";
import styles from "./CrossFadePhoto.module.scss";
export type CrossFadePhotoProps = {
caption: string;
/** 두 번째 사진이 번지는 시간 (기본 300ms) */
duration?: number;
easing?: string;
/** 프레임 색 */
color?: string;
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
export default function CrossFadePhoto({
caption,
duration = 300,
easing = "ease-out",
color = "#17141a",
}: CrossFadePhotoProps) {
const vars: Vars = { "--duration": `${duration}ms`, "--easing": easing, "--color": color };
return (
<figure className={styles.photo} style={vars} tabIndex={0}>
<span className={styles.frame} aria-hidden="true">
<span className={styles.a}>
<span className={styles.sun} />
<span className={styles.hill} />
</span>
<span className={styles.b}>
<span className={`${styles.sun} ${styles.sunB}`} />
<span className={`${styles.hill} ${styles.hillB}`} />
</span>
</span>
<figcaption className={styles.cap}>{caption}</figcaption>
</figure>
);
}08 Cross-fade hai ảnh — Diện tích đo được 14.624% · khung hình chuyển động 11/23Nguyên tác GODRICH · SCSS và React nằm trong tệp zip · 9 CSS Image Hover Effects — dán 1 dòng là chạy
Bài viết chứa demo này
9 CSS Image Hover Effects — dán 1 dòng là chạyCSS image hover effects là các hiệu ứng chỉ chạy bằng CSS khi con trỏ dừng trên một ảnh, không cần JavaScript. Chín kiểu Xem tất cả demo trong bài này


