CSS 有一些新的属性,可以简化代码的书写,用简单的代码就可以实现复杂的变化。不用像 js 那样,需要写很多代码

这里主要介绍三个属性:transition ,transform,以及translate

1. transition: 允许属性在一定时间内进行过渡

规则: transition:property duration timing-function delay;

property--->属性值,例如width,height,background-color等,默认值为all

duration---->过渡时间,必须有单位,如5s,1000ms 等,默认值为0s

timing-function---->过渡效果,如 linear|ease|ease-in|ease-out|ease-in-out|cubic-bezier(n,n,n,n); 默认为ease;

delay--->过渡延迟时间,默认为0s

可以分开写:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:300px;
height:300px;
background: #ccc;
margin:30px auto;
position:relative;
}
.child{
position:absolute;
width:100px;
height:100px;
top:50%;
left:50%;
background: #666;
margin-left:-50px;
margin-top: -50px; transition-property: width;
transition-duration: 2s;
transition-timing-function: linear;
transition-delay: 200ms; -moz-transition-property:width; /* Firefox 4 */
-webkit-transition-property:width; /* Safari and Chrome */
-o-transition-property:width; /* Opera */ -moz-transition-duration:2s; /* Firefox 4 */
-webkit-transition-duration:2s; /* Safari and Chrome */
-o-transition-duration:2s; /* Opera */ -moz-transition-timing-function:linear; /* Firefox 4 */
-webkit-transition-timing-function:linear; /* Safari and Chrome */
-o-transition-timing-function:linear; /* Opera */ -moz-transition-delay:200ms; /* Firefox 4 */
-webkit-transition-delay:200ms; /* Safari and Chrome */
-o-transition-delay:200ms; /* Opera */
}
.child:hover{
width:200px;
cursor:pointer;
} </style>
</head>
<body>
<div class="box">
<div class="child"></div>
</div>
</body>
</html>

也可以合起来将4个属性写在一起

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:300px;
height:300px;
background: #ccc;
margin:30px auto;
position:relative;
}
.child{
position:absolute;
width:100px;
height:100px;
top:50%;
left:50%;
background: #666;
margin-left:-50px;
margin-top: -50px; transition:width 2s linear 200ms;
-moz-transition:width 2s linear 200ms; /* Firefox 4 */
-webkit-transition:width 2s linear 200ms; /* Safari and Chrome */
-o-transition:width 2s linear 200ms; /* Opera */ }
.child:hover{
width:200px;
cursor:pointer;
} </style>
</head>
<body>
<div class="box">
<div class="child"></div>
</div>
</body>
</html>

运行结果:

要改变多个属性时候,用逗号分开,如:

transition:witdth 2s linear 200ms,background 2s ease 200ms;

2.transform :就是变形,改变的意思。主要有几种效果:旋转rotate,扭曲skew,缩放scale,移动translate 以及矩阵变形matrix(还可以细分(2D)对应为X,Y)

规则:transform : none | transform-function

2.1 旋转rotate

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:300px;
height:300px;
background: #ccc;
margin:30px auto;
position:relative;
}
.child{
position:absolute;
width:100px;
height:100px;
top:50%;
left:50%;
background: #666;
margin-left:-50px;
margin-top: -50px; }
.child:hover{
transform: rotate(20deg);
cursor:pointer;
} </style>
</head>
<body>
<div class="box">
<div class="child"></div>
</div>
</body>
</html>

运行结果:

也可以单独旋转X轴或者Y轴,对应函数rotateX(),rotateY()

2.2 扭曲skew

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:300px;
height:300px;
background: #ccc;
margin:30px auto;
position:relative;
}
.child{
position:absolute;
width:100px;
height:100px;
top:50%;
left:50%;
background: #666;
margin-left:-50px;
margin-top: -50px; }
.child:hover{
transform: skew(30deg);
cursor:pointer;
} </style>
</head>
<body>
<div class="box">
<div class="child"></div>
</div>
</body>
</html>

运行结果:

也可以单独扭曲X轴或者Y轴,对应函数skewX(),skewY()

2.3 缩放scale

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:300px;
height:300px;
background: #ccc;
margin:30px auto;
position:relative;
}
.child{
position:absolute;
width:100px;
height:100px;
top:50%;
left:50%;
background: #666;
margin-left:-50px;
margin-top: -50px; }
.child:hover{
transform: scale(1.5);
cursor:pointer;
} </style>
</head>
<body>
<div class="box">
<div class="child"></div>
</div>
</body>
</html>

运行效果:

也可以单独缩放X轴或者Y轴,对应函数scaleX(),scaleY()

2.4 移动translate

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:300px;
height:300px;
background: #ccc;
margin:30px auto;
position:relative;
}
.child{
position:absolute;
width:100px;
height:100px;
top:50%;
left:50%;
background: #666;
margin-left:-50px;
margin-top: -50px; }
.child:hover{
transform: translate(50%,50%);
cursor:pointer;
} </style>
</head>
<body>
<div class="box">
<div class="child"></div>
</div>
</body>
</html>

translate的单位,可以是百分比(相对本身),也可以是px

运行结果:

也可以单独移动X轴或者Y轴,对应函数translateX(),translateY()

2.5 矩阵变形matrix,这部分我自己现在也还没有搞懂,就不说了

不过以上四种一般情况下也够用了

2.6 当需要多个属性一起变化时,用空格隔起来就可以了

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:300px;
height:300px;
background: #ccc;
margin:30px auto;
position:relative;
}
.child{
position:absolute;
width:100px;
height:100px;
top:50%;
left:50%;
background: #666;
margin-left:-50px;
margin-top: -50px; }
.child:hover{
transform: translate(50%,50%) scale(1.5) rotate(30deg);
cursor:pointer;
} </style>
</head>
<body>
<div class="box">
<div class="child"></div>
</div>
</body>
</html>

运行结果:

Css 特性之 transition和transform的更多相关文章

  1. CSS动画:animation、transition、transform、translate

    https://blog.csdn.net/px01ih8/article/details/80780470 一.区分容易混淆的几个属性和值 先区分一下css中的几个属性:animation(动画). ...

  2. css3之transition、transform、animation比较

    css3动画多少都有些了解,但是对于transition.transform.animation这几个属性一直是比较模糊的,所以啊,这里做一个总结,也希望大家都可以对此有一个更好地理解.    其实, ...

  3. CSS:word-wrap/overflow/transition

    一 自动换行:一个div有固定宽高,如果其内容很长,必须两行以上才能显示完整的时候,有两种情况要留意 1 默认如果其内容都是中文,那么内容是可以自适应,而不会溢出div 2 如果内容除了中文之外,还有 ...

  4. 深入探讨 CSS 特性检测 @supports 与 Modernizr

    什么是 CSS 特性检测?我们知道,前端技术日新月异的今天,各种新技术新属性层出不穷.在 CSS 层面亦不例外. 一些新属性能极大提升用户体验以及减少工程师的工作量,并且在当下的前端氛围下: 很多实验 ...

  5. css 调转180度:transform: rotate(180deg);

    css 调转180度:transform: rotate(180deg);

  6. 2017年值得学习的3个CSS特性

    原文:https://bitsofco.de/3-new-css-features-to-learn-in-2017/译文:http://caibaojian.com/3-new-css-featur ...

  7. 即将来到: CSS Feature Queries (CSS特性查询)

    Feature Queries 是CSS3 Conditional Rules specification中的一部分,它支持“@supports”规则,“@supports”规则可以用来测试浏览器是否 ...

  8. CSS3的transition和transform

    CSS3中的transition和transform是制作HTML5动画一定要使用到的两个属性. 注:这篇文章不考虑兼容性,只讨论webkit核心的浏览器.所以本文的所有例子请用chrome,safa ...

  9. css基础(css书写 背景设置 标签分类 css特性)

      css书写位置   行内式写法 <p style="color:red;" font-size:12px;></p> 外联式写法 <link re ...

随机推荐

  1. 【转】每天一个linux命令(16):which命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/08/2759805.html 我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面 ...

  2. Linux开机自动启动ORACLE设置

    1.安装好Oracle数据库后: 执行 dbstart和dbshut会提示: [oracle@oracle11g ~]$ dbstartORACLE_HOME_LISTNER is not SET, ...

  3. npx 知识点

    npx 介绍:https://segmentfault.com/a/1190000010149499

  4. 使用 extract-text-webpack-plugin 报错:Error: Chunk.entry was removed. Use hasRuntime()

    问题:使用 extract-text-webpack-plugin 报错:Error: Chunk.entry was removed. Use hasRuntime() 解决:先运行npm unin ...

  5. hadoop入门篇---超详细hadoop服务器环境配置教程

    虚拟机以及Linux系统安装在之前的两篇分享中已经详细的介绍了方法,并且每一步的都配图了.如果有朋友还是看不懂,那我也爱莫能助了.本篇主要就hadoop服务器操作系统配置进行详细说明,hadoop安装 ...

  6. 织梦dedecms模板制作时,循环递增autoindex使用方法整理

    文章转载:http://www.maihui123.com/dedecms/2012051964.html 织梦dedecms模板制作时,我们需要每循环一次,变量加一,这是就需要使用到autoinde ...

  7. 开始转型学习java

    什么编程语言这些都是一样的,编程思想都是一样的.只不过是表现形式. 标识符 每一个字符在ascll码表例都有对应的数字 所以字符和数字是可以相加的   'a'+1 也可以显示数字对应的字符   (ch ...

  8. C# 解析 json Newtonsoft果然强大,代码写的真好

    C# 解析 json JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的 ...

  9. 学习笔记之Bitbucket

    Bitbucket | The Git solution for professional teams https://bitbucket.org/ Git Tutorials and Trainin ...

  10. 企业常用的RPC框架比较

    RPC框架比较     语言 协议 服务治理 社区 机构 Hessian 多语言 hessian(二进制) – 不活跃 Caucho Thrift 多语言 thrift – 活跃 Apache Fin ...