css3-形变、过渡、动画

一、2D形变

平移

transform:translate(x,y)

相对当前位置,向左移动x像素,像下移动y像素

transform:translateX(num)

相对当前位置,向左移动num像素

transform:translateY(num)

相对当前位置,向下移动num像素

缩放

transform:scale(x,y)

scale()方法,该元素增加或减少的大小,取决于宽度(X轴)和高度(Y轴)的参数

transform:scaleX( num )

改变元素的宽度

transform:scaleY( num )

改变元素的高度

旋转

transform:rotate( 角度 );

参数是一个角度,表示旋转多少度

倾斜

transform:skew(角度x,角度y)

沿着x轴旋转多少度,沿着y轴转多少度

transform:skewX(角度x)

沿着x轴旋转多少度

transform:skewY(角度y)

沿着y轴旋转多少度

二、3D形变

平移

transform:translate3d( x , y , z );

定义三个方向上的平移

transform:translateZ( z );

定义Z方向的平移(单用看不出效果)

缩放

transform: scale3d( x , y , z);

定义3个方向上的缩放,同时可以分别实现3个方向的分别设置

旋转

rotate3d( x , y , z , angle )

x,表示旋转轴X坐标方向的矢量。
y,表示旋转轴Y坐标方向的矢量。
z,表示旋转轴Z坐标方向的矢量。
angle,表示旋转角度。正的角度值表示顺时针旋转,负值表示逆时针旋转。

三、过渡

transition属性

通过transition,我们可以在不使用flash的情况下,让元素从一种样式变化成另一种样式

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <style type="text/css">
        body{
            position: relative;
        }
        #box{
            height: 100px;
            width: 100px;
            background-color: skyblue;
            transition: margin-left 3s;
        }
    </style>
</head>
<body>
<!-- 通过transition,我们可以在不使用flash的情况下,当元素从一种样式变化成另一种样式  -->
<div id="box"></div>

<script type="text/javascript">
   // var box = document.querySelector("box");
    box.onclick = function(){
        this.style.marginLeft = "600px";
}
</script>
</body>
</html>

运行效果

GIF.gif

transition实现多个过渡

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <style type="text/css">
        body{
            position: relative;
        }
        #box{
            /*如果动画的元素是margin,不能设置auto*/
            height: 100px;
            width: 100px;
            background-color: pink;
            position: absolute;
            top: 20px;
            transition: margin-left 3s,background-color 3s,border-radius 3s,top 3s;
        }
    </style>
</head>
<body>

<!-- 通过transition,我们可以在不使用flash的情况下,当元素从一种样式变化成另一种样式  -->

<div id="box"></div>
<div id="box1"></div>

<script type="text/javascript">
    //var box = document.querySelector("box");
    box.onclick = function(){
        this.style.marginLeft = "600px";
        this.style.backgroundColor = "red";
        this.style.borderRadius = "50px";
        this.style.top = "100px";
    }
</script>
</body>
</html>

运行效果

GIF.gif

利用伪元素实现transition过度

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <style type="text/css">
        body{
            position: relative;
        }
        #box{
            /*如果动画的元素是margin,不能设置auto*/
            height: 100px;
            width: 100px;
            background-color: pink;
            position: absolute;
            top: 20px;
            transition: margin-left 3s,background-color 3s,border-radius 3s,top 3s;
        }
        #box:hover{
            background-color: purple;
            border-radius: 50px;
            top: 50px;
        }
    </style>
</head>
<body>

<div id="box"></div>

</body>
</html>

运行结果

GIF.gif

利用transition实现3D动画过度

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <style type="text/css">
        body{
            position: relative;
        }
        #box{
            /*如果动画的元素是margin,不能设置auto*/
            height: 300px;
            width: 300px;
            margin: 15px 0 0 15px;
            position: relative;
            background: url(img/bg.jpg) round;
            /*将来写有关动画相关的代码,要写兼容*/
            /*transform-style: preserve-3d;*/
            /*设置镜头距离*/
            perspective: 20000px;
        }
        div img{
            width: 130px;
            height: 200px;
            position: absolute;
            top: 50px;
            left: 85px;
            transform-style: preserve-3d;
            transform: rotateZ(180deg) rotateX(0) rotateY(0deg);
            transition: all 3s;
        }
        div img:hover{
            transform: rotateZ(0) rotateX(360deg) rotateY(180deg);
        }
    </style>
</head>
<body>

<div id="box">![](img/shu.jpg)</div>

</body>
</html>

运行结果

GIF.gif

css3动画

如果用@keyframes 创建动画,把它绑定到一个选择器,否则动画不会有任何效果。
指定至少这两个CSS3的动画属性绑定向一个选择器:
规定动画的名称
规定动画的时长

from-to

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <style type="text/css">
    
    #box{
        height: 30px;
        width: 30px;
        background-color: blue;
        animation: rect 3s linear 0.5s alternate 3 ;
    }

    @keyframes rect{
        from{
            transform: translate(0,0);
            background-color: blue;
        }
        to{
            transform: translate(200px,200px);
            background-color: red;
        }
    }   
    </style>    
</head>
<body>

<div id="box"></div>

</body>
</html>

运行效果

GIF.gif

百分比

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <style type="text/css">
    
    #box{
        height: 50px;
        width: 50px;
        background-color: blue;
        animation: rect 3s linear infinite alternate;
    }

    @keyframes rect{
        0%{
            transform: translate(0,0);
            background-color: blue;
        }
        25%{
            transform: translate(300px,0px);
            background-color: red;
            border-radius: 50%;
        }
        50%{
            transform: translate(300px,300px);
            background-color: pink;
            border-radius: 0;
        }
        75%{
            transform: translate(0px,300px);
            background-color: orange;
            border-radius: 50%;
        }
        100%{
            transform: translate(0px,0px);
            background-color: yellow;
        }
    }   

    </style>
    
</head>
<body>

<div id="box"></div>

</body>
</html>

运行效果

GIF.gif

小案例(动画实现一个简易大风车)

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <style type="text/css">
        .circle{
            height: 200px;
            width: 200px;
            margin: 50px auto;
            background-color: pink;
            border-radius: 50%;
            position: relative;
            animation: rect 2s linear infinite;
        }
        .f1{
            height: 80px;
            width: 50px;
            background-color: blue;
            position: absolute;
            left: 100px;
            top: 20px;
            border-top-right-radius: 100%;
        }
        .f2{
            width: 80px;
            height: 50px;
            background-color: red;
            position: absolute;
            left: 100px;
            top: 100px;
            border-bottom-right-radius: 100%;
        }
        .f3{
            height: 80px;
            width: 50px;
            background-color: orange;
            position: absolute;
            left: 50px;
            top: 100px;
            border-bottom-left-radius: 100%;
        }
        .f4{
            height: 50px;
            width: 80px;
            background-color: green;
            position: absolute;
            left: 20px;
            top: 50px;
            border-top-left-radius: 100%;
        }
        @keyframes rect{
            from{
                transform: rotate(0);
            }
            to{
                transform: rotate(360deg);
            }
        }
    </style>
</head>
<body>

<div class="circle">
    <div class="f1"></div>
    <div class="f2"></div>
    <div class="f3"></div>
    <div class="f4"></div>
</div>

</body>
</html>

运行结果

GIF.gif

喜欢就点赞

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容

  • 看了很多视频、文章,最后却通通忘记了,别人的知识依旧是别人的,自己却什么都没获得。此系列文章旨在加深自己的印象,因...
    DCbryant阅读 1,854评论 0 4
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,083评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,438评论 6 30
  • 关于css3变形 CSS3变形是一些效果的集合,比如平移、旋转、缩放和倾斜效果,每个效果都被称作为变形函数(Tra...
    hopevow阅读 6,309评论 2 13
  • 很好,昨晚的三个单词还记忆深刻。(practice + spare + innocent)不听老人言,吃亏在眼前。...
    6726a3241fb1阅读 749评论 0 0