zoukankan      html  css  js  c++  java
  • 01-css3之过渡

    一、介绍

    过渡(transition)是CSS3中具有颠覆性的特征之一,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种样式时为元素添加效果,经常和 :hover 一起 搭配使用。

    二、语法

    transition: 要过渡的属性 花费时间 运动曲线 何时开始;

    transition: property duration timing-function delay;
    

    三、属性

    1.property

    想要变化的 css 属性,宽度、高度、背景颜色、内外边距都可以。

    过渡不能将display:none;过渡到display:block;因为过渡属性只能值变,不能改变单词,如果想让隐藏的元素过渡到显示,可以将opacity: 0;过渡到opacity: 1; 。

    如果想要所有的属性都变化过渡, 写一个all 就可以。

    描述
    none(默认) 没有属性会获得过渡效果
    all 所有属性都将获得过渡效果
    property 定义应用过渡效果的 CSS 属性名称列表,列表以逗号分隔

    2.duration

    默认值是 0,意味着和没加过渡一样瞬间变化。

    time值单位是秒 (必须写单位) 比如 0.5s / .5s

    描述
    time 规定完成过渡效果需要花费的时间(以秒或毫秒计)。

    3.timing-function

    定义过渡的运动曲线,默认是ease,可以省略

    描述
    linear 规定以相同速度开始至结束的过渡效果(等于 cubic-bezier(0,0,1,1)
    ease(默认) 规定慢速开始,然后变快,然后慢速结束的过渡效果(cubic-bezier(0.25,0.1,0.25,1)
    ease-in 规定以慢速开始的过渡效果(等于 cubic-bezier(0.42,0,1,1)
    ease-out 规定以慢速结束的过渡效果(等于 cubic-bezier(0,0,0.58,1)
    ease-in-out 规定以慢速开始和结束的过渡效果(等于 cubic-bezier(0.42,0,0.58,1)
    cubic-bezier(n,n,n,n) 在 cubic-bezier 函数中定义自己的值。可能的值是 0 至 1 之间的数值

    01运动曲线.png

    4.delay

    用于定义运动何时开始,单位是秒(必须写单位)可以设置延迟触发时间 默认是 0s (可以省略)

    描述
    time 规定在过渡效果开始之前需要等待的时间,以秒或毫秒计。

    四、案例

    1.让一个透明的盒子放大位移加阴影。其中用:hover有小问题
    <!DOCTYPE html>
    <html lang="zh-CN">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            div {
                position: absolute;
                top: 0;
                left: 0;
                 50px;
                height: 50px;
                opacity: 0;
                background-color: #f34;
                box-shadow: 0 0 0 0 rgba(0, 0, 0, .1);
                /* transition:要过渡的属性 花费时间(单位s 秒) 运动曲线(默认ease 常用linear匀速) 何时开始(延迟 单位s,默认0)  */
                /* transtion不能使display:none过渡到display:block */
                /* 只能值变,不能变单词 */
                transition: all 1s;
            }
    
            /* 让盒子移动位置时使用hover会有小问题 */
            /* 当盒子左边线移动到鼠标上之后,盒子会默认鼠标已经不再盒子上了,会往回恢复原位 */
            /* 当盒子往回走时又经过了鼠标,即触发位移效果,盒子左边线再次经过移动到鼠标时,又会重复上面的操作,所以出现抖动。 */
            /* div:hover  */
            div:active {
                /* opactiy:可以让盒子和里面的内容变透明 */
                /* 取值0-1的小数,0是透明,1是完全显示 */
                opacity: 1;
                top: 100px;
                left: 100px;
                 150px;
                height: 150px;
                box-shadow: 10px 10px 10px 10px rgba(0, 0, 0, .2);
            }
        </style>
    
    </head>
    
    <body>
        <div>小艾同学</div>
    </body>
    
    </html>
    

    :hover时候出现的抖动问题:
    02过渡抖动.gif

    2.仿小米logo移动效果
    <!DOCTYPE html>
    <html lang="zh-CN">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>小米logo</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
    
            .logo {
                /* 溢出隐藏 */
                overflow: hidden;
                position: relative;
                 55px;
                height: 55px;
                background-color: #ff6700;
                margin: 100px auto;
            }
    
            .logo a {
                position: absolute;
                top: 0;
                left: -55px;
                /* a里面放两个盒子,所以宽度是logo的200% */
                 200%;
                height: 100%;
                /* 谁变化给谁加过渡 */
                transition: all .2s;
            }
    
            a::before,
            a::after {
                content: "";
                float: left;
                /* 小盒子应该和logo一样宽100%,单他的父亲a是200%,所以他是父亲a的一半 */
                 50%;
                height: 100%;
                background: url(images/mi-home.png) center;
    
            }
    
            a::after {
                background: url(images/mi-logo.png) center;
            }
    
            .logo:hover a {
                /* 鼠标经过时a,右移动小盒子的宽度 */
                left: 0px;
            }
        </style>
    </head>
    
    <body>
        <div class="logo">
            <a href="#"></a>
        </div>
    </body>
    
    </html>
    

    03.小米logo.gif

    3.当鼠标经过时给盒子加阴影,有立体感,并在底部弹出一个隐藏的盒子
    <!DOCTYPE html>
    <html lang="zh-CN">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
    
            .dad {
                 1200px;
                margin: 100px auto;
            }
    
            li {
                list-style: none;
            }
    
            li {
                /* 溢出隐藏,让隐藏的盒子看不见 */
                overflow: hidden;
                position: relative;
                float: left;
                 230px;
                height: 300px;
                background-color: #aaa;
                margin: 20px;
                transition: all .5s;
            }
    
            .dad li::after {
                content: "隐藏的盒子";
                position: absolute;
                bottom: -80px;
                left: 0;
                 100%;
                height: 80px;
                background-color: #f34;
                /* 让谁过渡就给谁加 */
                transition: all 1s;
            }
    
            .dad li:hover {
                /* 让li向上走走,更有立体感 */
                margin-top: 10px;
                /* 给li加阴影 */
                box-shadow: 0 10px 30px rgba(0, 0, 0, .3);
            }
    
            .dad li:hover::after {
                /* 鼠标经过li时,li::after向上移动80 */
                bottom: 0;
            }
        </style>
    </head>
    
    <body>
        <div class="dad">
            <ul>
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
            </ul>
        </div>
    </body>
    
    </html>
    

    04过渡3.gif

    4.商品下拉列表
    <!DOCTYPE html>
    <html lang="zh-CN">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
    
            .dropdown {
                /* 隐藏溢出的文字不能给父盒子加,因为dd相对于父盒子永远溢出,鼠标经过时 也不会显示 */
                /* overflow: hidden; */
                 150px;
                height: 45px;
            }
    
            .dt {
                 100%;
                height: 100%;
                line-height: 45px;
                text-align: center;
                background-color: #f34;
            }
    
            .dd {
                /* dd高度为0时文字溢出隐藏 */
                overflow: hidden;
                 100%;
                /* height: 300px; */
                /* 先让高度为0 */
                height: 0;
                background-color: pink;
                transition: all .3s;
            }
    
            /* 鼠标经过dropdown时候,让dd下拉 */
            .dropdown:hover .dd {
                height: 300px;
            }
        </style>
    </head>
    
    <body>
        <div class="dropdown">
            <div class="dt">商品分类</div>
            <div class="dd">
                <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>
                </ul>
            </div>
        </div>
    </body>
    
    </html>
    

    05下拉窗口.gif

    分享完毕,不妥之处,敬请批评指正,谢谢大家!

  • 相关阅读:
    systemtap分析软raid io拆分问题
    Profiling Java Application with Systemtap
    中国南方ORACLE用户组
    Docker 核心技术与实现原理
    The Internals of PostgreSQL
    Alone_Monkey 逆向IOS
    淘宝内核月报 2017
    Linux kernel engineer--trace
    你的按钮到底在帮助用户还是在误导用户?
    2020年值得你去试试的10个React开发工具
  • 原文地址:https://www.cnblogs.com/xiaoaitongxue/p/12649856.html
Copyright © 2011-2022 走看看