|
实现效果
源码(html部分)
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>图像缩放</title>
- <link rel="stylesheet" href="./style.css">
-
- </head>
- <body>
- <div class="carousel">
- <div class="control-button left"><input type="radio" name="control-input"></div>
- <div class="control-button right"><input type="radio" name="control-input" checked></div>
- <div class="rotation-direction">
- <ul class="item-wrapper" style="--_num-elements: 11">
- <li class="item"
- style="--_index: 1; --_image-url: url('https://images.unsplash.com/photo-1730722005859-f93a79460bae?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')">
- <a href="#" target="_blank"></a>
- </li>
- <li class="item"
- style="--_index: 2; --_image-url: url('https://images.unsplash.com/photo-1730815664512-238cea535eff?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')">
- <a href="#" target="_blank"></a>
- </li>
- <li class="item"
- style="--_index: 3; --_image-url: url('https://images.unsplash.com/photo-1731128976871-9dc558f0c3c4?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')">
- <a href="#" target="_blank"></a>
- </li>
- <li class="item"
- style="--_index: 4; --_image-url: url('https://images.unsplash.com/photo-1730407401172-aeed1b1ace5b?q=80&w=1935&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')">
- <a href="#" target="_blank"></a>
- </li>
- <li class="item"
- style="--_index: 5; --_image-url: url('https://images.unsplash.com/photo-1730724583278-151efd042e16?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')">
- <a href="#" target="_blank"></a>
- </li>
- <li class="item"
- style="--_index: 6; --_image-url: url('https://images.unsplash.com/photo-1731000891359-b430173e8491?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')">
- <a href="#" target="_blank"></a>
- </li>
- <li class="item"
- style="--_index: 7; --_image-url: url('https://images.unsplash.com/photo-1730435136320-1410fa8bed02?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')">
- <a href="#" target="_blank"></a>
- </li>
- <li class="item"
- style="--_index: 8; --_image-url: url('https://images.unsplash.com/photo-1730523169782-31cb72f147ff?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')">
- <a href="#" target="_blank"></a>
- </li>
- <li class="item"
- style="--_index: 9; --_image-url: url('https://images.unsplash.com/photo-1730405704088-3d8f36e32b62?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')">
- <a href="#" target="_blank"></a>
- </li>
- <li class="item"
- style="--_index: 10; --_image-url: url('https://images.unsplash.com/photo-1717764737741-b22a89548750?q=80&w=1970&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')">
- <a href="#" target="_blank"></a>
- </li>
- <li class="item"
- style="--_index: 11; --_image-url: url('https://images.unsplash.com/photo-1730386547603-918a97d624d4?q=80&w=1990&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')">
- <a href="#" target="_blank"></a>
- </li>
- <li class="carousel-ground"></li>
- </ul>
- </div>
- </div>
- </body>
- </html>
复制代码 源码(css部分 创建一个style.css文件)- :root {
- --transition-duration: 250ms;
- --transition-ease: ease-out;
- --bg-color-rgb: 0, 0, 0;
- --shadow-color-rgb: 128, 128, 128;
- --item-width: 11.5rem;
- --item-height: 17.5rem;
- --hover-effect: 1.075;
- --reflection-blur: 0.25rem;
- --empty-color-rgb: 255, 255, 255;
- --glow-color-rgb: 255, 255, 255;
- --glow-size: 5rem;
- --diameter: 50rem;
- --perspective: 1000px;
- --perspective-origin: 50% 20%;
- --control-button-width: 1.25rem;
- --control-button-height: 4rem;
- --control-color-rgb: 255, 255, 255;
- --animation-duration: 25s;
- --animation-play-state: running;
- --direction-animation-play-state: paused;
- }
- *,
- *::before,
- *::after {
- margin: 0;
- padding: 0;
- border: 0;
- box-sizing: border-box;
- }
- *:focus {
- outline: none;
- }
- a {
- -webkit-tap-highlight-color: transparent;
- user-select: none;
- }
- body {
- display: flex;
- align-items: center;
- justify-content: center;
- height: 100vh;
- background-color: rgb(var(--bg-color-rgb));
- overflow: hidden;
- }
- .carousel {
- --_diameter: var(--diameter);
- --_radius: calc(var(--_diameter) / 2);
- --_item-width: var(--item-width);
- --_item-height: var(--item-height);
- perspective: var(--perspective);
- perspective-origin: var(--perspective-origin);
- width: var(--_diameter);
- height: var(--_diameter);
- }
- @media only screen and (max-width: 48rem) {
- .carousel {
- --_scale: 0.75;
- --_diameter: calc(var(--diameter) * var(--_scale));
- --_item-width: calc(var(--item-width) * var(--_scale));
- --_item-height: calc(var(--item-height) * var(--_scale));
- }
- }
- @media only screen and (max-width: 32rem) {
- .carousel {
- --_scale: 0.6;
- --_diameter: calc(var(--diameter) * var(--_scale));
- --_item-width: calc(var(--item-width) * var(--_scale));
- --_item-height: calc(var(--item-height) * var(--_scale));
- }
- }
- @media only screen and (max-width: 16rem) {
- .carousel {
- --_scale: 0.25;
- --_diameter: calc(var(--diameter) * var(--_scale));
- --_item-width: calc(var(--item-width) * var(--_scale));
- --_item-height: calc(var(--item-height) * var(--_scale));
- }
- }
- .control-button {
- --_width: var(--control-button-width);
- --_height: var(--control-button-height);
- z-index: 1;
- width: var(--_width);
- height: var(--_height);
- background-color: rgb(var(--control-color-rgb));
- opacity: 0.2;
- transition: opacity var(--transition-duration) var(--transition-ease);
- position: absolute;
- &:hover {
- opacity: 0.4;
- }
- &:has(input:checked) {
- opacity: 0.8;
- }
- & input {
- appearance: none;
- opacity: 0;
- width: 100%;
- height: 100%;
- cursor: pointer;
- }
- &.left {
- clip-path: polygon(0% 50%, 100% 0%, 100% 100%);
- top: calc(var(--_radius) - var(--_height) / 2);
- left: 0;
- }
- &.right {
- clip-path: polygon(0% 0%, 100% 50%, 0% 100%);
- top: calc(var(--_radius) - var(--_height) / 2);
- right: 0;
- }
- }
- .carousel:has(.control-button.left input:checked) {
- --direction-animation-play-state: running;
- }
- .carousel:has(.control-button.right input:checked) {
- --direction-animation-play-state: paused;
- }
- .rotation-direction {
- --_direction-animation-play-state: var(--direction-animation-play-state);
- --_z: calc(var(--_radius) * -1);
- transform: translateZ(var(--_z));
- transform-style: preserve-3d;
- animation: rotation-reverse calc(var(--animation-duration) / 2) reverse linear infinite var(--_direction-animation-play-state);
- transition: all var(--transition-duration) var(--transition-ease);
- }
- @keyframes rotation-reverse {
- from { transform: translateZ(var(--_z)) rotateY(0deg); }
- to { transform: translateZ(var(--_z)) rotateY(360deg); }
- }
- @keyframes rotation-normal {
- from { transform: rotateY(0deg); }
- to { transform: rotateY(360deg); }
- }
- .item-wrapper {
- transform-style: inherit;
- width: inherit;
- height: inherit;
- list-style-type: none;
- position: relative;
- animation: rotation-normal var(--animation-duration) normal linear infinite var(--animation-play-state);
- transition: all var(--transition-duration) var(--transition-ease);
- }
- .rotation-direction:has(.item:hover) {
- --animation-play-state: paused;
- --_direction-animation-play-state: paused;
- }
- .item {
- --_width: var(--_item-width);
- --_height: var(--_item-height);
- --_rotation: calc(360 / var(--_num-elements) * var(--_index) * 1deg);
- left: calc(var(--_radius) - var(--_item-width) / 2);
- top: calc(var(--_radius) - var(--_item-height) / 2);
- transform: rotateY(var(--_rotation)) translateZ(var(--_radius));
- transform-style: inherit;
- width: var(--_width);
- height: var(--_height);
- transition: all var(--transition-duration) var(--transition-ease);
- box-shadow: 0 0 var(--glow-size) transparent;
- position: absolute;
- &:hover {
- box-shadow: 0 0 var(--glow-size) rgb(var(--glow-color-rgb));
- transform: rotateY(var(--_rotation)) translateZ(calc(var(--_radius) * var(--hover-effect)));
- }
- & a {
- display: block;
- width: inherit;
- height: inherit;
- text-indent: -9999px;
- background-color: rgba(var(--empty-color-rgb), 0.5);
- background-image: var(--_image-url);
- background-repeat: no-repeat;
- background-position: center;
- background-size: cover;
- transition: filter var(--transition-duration) var(--transition-ease);
- filter: grayscale(100%);
- &:hover {
- filter: grayscale(0%);
- }
- }
- &::before {
- content: '';
- width: inherit;
- height: inherit;
- background-color: rgba(var(--empty-color-rgb), 0.5);
- background-image:
- linear-gradient(to top, rgba(var(--bg-color-rgb), 0.25) 0%, rgba(var(--bg-color-rgb), 1.0) 75%),
- var(--_image-url);
- background-repeat: no-repeat;
- background-position: center;
- background-size: cover;
- pointer-events: none;
- filter: blur(var(--reflection-blur)) grayscale(100%);
- transition: filter var(--transition-duration) var(--transition-ease);
- transform-style: inherit;
- transform-origin: center bottom;
- transform: rotateX(90deg) rotateZ(180deg) rotateY(180deg);
- position: absolute;
- &:hover {
- filter: blur(var(--reflection-blur)) grayscale(0%);
- }
- }
- }
- .ground {
- --_width: var(--_diameter);
- --_height: var(--_diameter);
- --_rotation: 90deg;
- left: calc(var(--_radius) - var(--_width) / 2);
- top: calc(var(--_radius) - var(--_height) / 2);
- transform: rotateX(var(--_rotation)) translateZ(calc(var(--_item-height) / -2));
- width: var(--_width);
- height: var(--_height);
- border-radius: 50%;
- background: radial-gradient(rgba(var(--shadow-color-rgb), 0.75) 15%, rgba(var(--bg-color-rgb), 0) 60%);
- opacity: 0.5;
- transition: opacity var(--transition-duration) var(--transition-ease);
- position: absolute;
- &.item-wrapper:has(.item:hover) {
- opacity: 0.75;
- }
- }
复制代码
|
|