CSS各种变换

本文介绍了CSS3中的各种动画效果,包括线性和径向渐变的使用,以及如何创建过渡动画。此外,还详细讲解了2D变换如缩放、旋转和倾斜,以及3D变换中的立方体效果。文章还提到了关键帧动画和Animate.css这样的CSS3动画库的使用。
摘要由CSDN通过智能技术生成

渐变

线性渐变

div{
        height: 300px;
        width: 300px;
        border: 10px solid gray;
        /* background: linear-gradient(red,yellow); */
        /* background: linear-gradient(to top,red,yellow); */
        background: linear-gradient(90deg,red,yellow);
        /*
        支持多颜色渐变
        支持方向 to left....to bottom right...
        支持角度的写法
        */
    }

径向渐变

重复渐变

太极案例

实现方法

div{
   width: 200px;
   height: 200px;
   border: 1px solid gray;
   background: linear-gradient(white 50%,black 50%);
   margin: auto;
   display: flex;
   align-items: center;
   border-radius: 50%;
}
div::before{
    content:"";
    width:100px;
    display: block;
    height: 100px;
    background:radial-gradient(white 25%,black 25%);
    border-radius: 50%;
}
div::after{
    content:"";
    width:100px;
    display: block;
    height: 100px;
    background:radial-gradient(black 25%,white 25%);
    border-radius: 50%;
}

过渡动画

 div{
   width: 200px;
   height: 200px;
   background:yellow;
   transition: all 2s linear 1s;
}

/* all 所有属性
2s 动画时间
linear 匀速
1s 延迟
除了 display:none/block属性不行
*/
div:hover{
    width: 400px;
    height: 600px;
    background: red;
}

在菜单中的过渡动画

2D属性-transfrom

缩放

改变中心点的位置移动

旋转

rotate中心,正值顺时针,负值逆时针=====rotateZ

opacity: 0;//透明度

扇子打开案例

<style>
*{
    padding: 0;
    margin: 0;
}
ul{
    list-style: none;
}
ul{
    margin: 10px auto;
    width: 600px;
    height: 400px;
    border:5px solid gray;
    position: relative;
}
li{
    position: absolute;
    width: 60px;
    height: 200px;
    left:50%;
    text-align:center;
    margin-left: -30px;
    bottom: 30px;
    transform-origin: bottom center;
    border-radius: 10px;
    transition: all 2s;
}
ul li:not(:nth-child(7)){
    opacity: 0;
}
ul:hover li{
    opacity: 1;
}
ul li:nth-child(1),ul li:nth-child(13){
background: purple;
}
ul li:nth-child(2),ul li:nth-child(12){
background: darkblue;
}
ul li:nth-child(3),ul li:nth-child(11){
background: deeppink;
}
ul li:nth-child(4),ul li:nth-child(10){
background: deepskyblue
}
ul li:nth-child(5),ul li:nth-child(9){
background: green;
}
ul li:nth-child(6),ul li:nth-child(8){
background: yellow;
}
ul li:nth-child(7){
background: red;
}
ul:hover li:nth-child(1){
    transform: rotate(90deg);
}
ul:hover li:nth-child(13){
    transform: rotate(-90deg);
}
ul:hover li:nth-child(2){
    transform: rotate(75deg);
}
ul:hover li:nth-child(12){
    transform: rotate(-75deg);
}
ul:hover li:nth-child(3){
    transform: rotate(60deg);
}
ul:hover li:nth-child(11){
    transform: rotate(-60deg);
}
ul:hover li:nth-child(4){
    transform: rotate(45deg);
}
ul:hover li:nth-child(10){
    transform: rotate(-45deg);
}
ul:hover li:nth-child(5){
    transform: rotate(30deg);
}
ul:hover li:nth-child(9){
    transform: rotate(-30deg);
}
ul:hover li:nth-child(6){
    transform: rotate(15deg);
}
ul:hover li:nth-child(8){
    transform: rotate(-15deg);
}
</style>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li>10</li>
        <li>11</li>
        <li>12</li>
        <li>13</li>
    </ul>
</body>


 多属性

 倾斜

关键帧动画

 div{
        width: 200px;
        height: 200px;
        background: red;
        animation: cxl2 6s linear infinite;
        /* infinite 无限次执行 */
    }
    /* 声明动画 */
    @keyframes cxl1 {
        from{
            width: 200px;
            height: 200px;
            background: red;
        }
        to{
            width: 200px;
            height: 400px;
            background: blue;
        }
    }
    @keyframes cxl2 {
        0%{
            width: 200px;
            height: 200px;
            background: red;
        }
        20%{
            width: 400px;
            height: 200px;
            background: yellow;
        }
        40%{
            width: 400px;
            height: 200px;
            background: green;
        }
        60%{
            width: 400px;
            height: 200px;
            background: black;
        }
        80%{
            width: 400px;
            height: 200px;
            background: yellow;
        }
        100%{
            width: 200px;
            height: 400px;
            background: blue;
        }
    }

 div{
    height: 100%;
    width: 200px;
    background-color: red;
    position: fixed;
    left: 0px;
    top:0px;
    transform: translateX(-100%);
    animation: run 1s forwards;
    animation: run 1s forwards reverse;
    /* animation-fill-mode: forwards; */
    /* forwards:保留最后画面
    backwards:初始画面
    none:默认值 */
   }
   @keyframes run {
    from{
        transform: translateX(-100%);
    }
    to{
        transform: translateX(0);
    }
   }

关键帧轮播案例

<style>
   *{
    padding: 0;
    margin: 0;
   }
   .box{
    width: 640px;
    height: 300px;
    margin:  auto;
    overflow: hidden;
   }
   .box img{
    width: 640px;
    height: 300px;
   }
   .box2{
    width: 9999px;
    animation: run 5s linear infinite;
   }
   //鼠标移上去暂停,移开滚动
    .box2:hover{
    animation-play-state: paused;
   }
   @keyframes run {
    0%{
        transform: translateX(0);
    }
    5%{
        transform: translateX(-640px);
    }
    25%{
        transform: translateX(-640px);
    }
    30%{
        transform: translateX(-1280px);
    }
    50%{
        transform: translateX(-1280px);
    }
    55%{
        transform: translateX(-1920px);
    }
    75%{
        transform: translateX(-1920px);
    }
    80%{
        transform: translateX(-2560px);
    }
    100%{
        transform: translateX(-2560px);
    }
   }
   .box3{
    float: left;
   }

</style>

<body>
    <div class="box">
        <div class="box2">
            <div class="box3"> <img src="01.jpeg" alt=""></div>
            <div class="box3"> <img src="02.jpeg" alt=""></div>
            <div class="box3"> <img src="03.jpeg" alt=""></div>
            <div class="box3"> <img src="04.jpeg" alt=""></div>
           <!-- 为了实现无缝轮播,第一张和最后一张一样 -->
            <div class="box3"> <img src="01.jpeg" alt=""></div>
        </div>
    </div>
</body>

逐帧动画

div{
        width: 200px;
        height: 200px;
        background: red;
        /* animation: run 5s steps(1,start); */
        animation: run 5s step-start;
        /* end(保留当前帧,看不到最后一帧,动画结束看不见) */
        /* start(保留下一帧,看不见第一帧,从第一帧很快跳到最后一帧 */
    }
    @keyframes run {
        0%{
            background: red;
        }
        50%{
            background: green;
        }
        100%{
            background: yellow;
        }
    }

step案例

div{
      width: 252px;
      height: 300px;
      background-image: url(./cat.png);
      animation: run 2s step-end infinite;
    }
    @keyframes run {
       0%{
        background-position: 0px 0;
       }
       20%{
        background-position: -252px 0;
       }
       40%{
        background-position: -504px 0;
       }
       60%{
        background-position: -756px 0;
       }
       80%{
        background-position: -1008px 0;
       }
       100%{
        background-position: -1008px 0;
       }
    }

Animate.css---------css3动画库

animate.css动画演示_dowebok

vs中引入,在class中使用

 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/

    animate.css/3.5.1/animate.min.css">

    <div class="animated bounce"></div>

兼容性在 Can I use... Support tables for HTML5, CSS3, etc网站中查询

3D部分

 .box{
        width: 500px;
        height: 500px;
        margin: 20px auto;
        border: 3px solid black;
        transform-style: preserve-3d;
        position: relative;
        /* transform: rotateY(30deg); */
        perspective: 900px;
        /* 设置景深 */
    }
    .center{
        position: absolute;
        width: 200px;
        height: 200px;
        left: 50%;
        top: 50%;
        background-color: red;
        margin-top: -100px;
        margin-left: -100px;
        /* transform: translateZ(100px); */
        transition: all 2s;
    }
    .box:hover .center{
        transform: translateZ(900px);
        /* transform: translate3d(x,y,z); */
    }

前面三个值取值0-1

3d缩放

立方体

<style>
    *{
        margin: 0;
        padding: 0;
    }
    .box{
        width: 600px;
        height: 600px;
        margin: 20px auto;
        border: 3px solid black;
        position: relative;
        transform-style: preserve-3d;
        transform: rotateY(30deg) rotateX(30deg);
        animation: run 6s linear infinite;
    }
    @keyframes run {
        0%{
            transform: rotateY(30deg) rotateX(30deg);
        }
        50%{
            transform: rotateY(300deg) rotateX(300deg);
        }
        100%{
            transform: rotateY(30deg) rotateX(30deg);
        }
    }
    .box div{
        width: 200px;
        height: 200px;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -100px;
        margin-left: -100px;
        line-height: 200px;
        text-align: center;
        font-size: 50px;
        color: #fff;
        opacity: 0.8;
    }
  .box div:nth-child(1){
    background: gray;
    transform: translateZ(100px);
  }
  .box div:nth-child(2){
    background: blue;
    transform: translateX(-100px) rotateY(-90deg);
  }
  .box div:nth-child(3){
    background: yellow;
    transform: translateY(-100px) rotateX(90deg);
  }
  .box div:nth-child(4){
    background: pink;
    transform: translateY(100px) rotateX(-90deg);
  }
  .box div:nth-child(5){
    background: purple;
    transform: translateX(100px) rotateY(90deg);
  }
  .box div:nth-child(6){
    background: orange;
    transform: translateZ(-100px);
  }
</style>
<body>
<div class="box">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
</div>
</body>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现CSS边框的彩虹变换效果,可以使用背景渐变和动画属性来实现。首先,为目标元素添加一个边框,并将其设置为透明。然后,使用background-image属性添加一个渐变背景,并将背景色设置为彩虹颜色。接下来,使用动画属性让渐变颜色在一定的时间内循环播放。下面是一个实现边框彩虹变换效果的例子: ```html <div class="rainbow-border"></div> ``` ```css .rainbow-border { width: 200px; height: 200px; border: 5px solid transparent; background-image: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet); background-clip: padding-box; animation: rainbow 5s linear infinite; } @keyframes rainbow { 0% { background-position: 0% 50%; } 100% { background-position: 100% 50%; } } ``` 上面的代码中,我们定义了一个名为"rainbow-border"的类,设置了宽度、高度和边框。然后,使用linear-gradient函数创建了一个从红色到紫色的渐变背景,并将其应用于背景图像。接下来,通过animation属性添加了一个名为"rainbow"的动画,设置了动画的持续时间、循环次数和缓动函数。动画的关键帧中,通过改变background-position属性的值来实现渐变颜色的循环播放效果。 这样,当你将"rainbow-border"类应用于一个元素时,它的边框就会呈现出彩虹变换的效果。这是一种简单而有效的方法来实现CSS边框的彩虹变换效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [有趣的CSS | css-border特效(转动边框,彩虹边框,渐变边框)和css变量](https://blog.csdn.net/qq_39370934/article/details/118439096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值