Css 特性之 transition和transform
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的更多相关文章
- CSS动画:animation、transition、transform、translate
		https://blog.csdn.net/px01ih8/article/details/80780470 一.区分容易混淆的几个属性和值 先区分一下css中的几个属性:animation(动画). ... 
- css3之transition、transform、animation比较
		css3动画多少都有些了解,但是对于transition.transform.animation这几个属性一直是比较模糊的,所以啊,这里做一个总结,也希望大家都可以对此有一个更好地理解. 其实, ... 
- CSS:word-wrap/overflow/transition
		一 自动换行:一个div有固定宽高,如果其内容很长,必须两行以上才能显示完整的时候,有两种情况要留意 1 默认如果其内容都是中文,那么内容是可以自适应,而不会溢出div 2 如果内容除了中文之外,还有 ... 
- 深入探讨 CSS 特性检测 @supports 与 Modernizr
		什么是 CSS 特性检测?我们知道,前端技术日新月异的今天,各种新技术新属性层出不穷.在 CSS 层面亦不例外. 一些新属性能极大提升用户体验以及减少工程师的工作量,并且在当下的前端氛围下: 很多实验 ... 
- css 调转180度:transform: rotate(180deg);
		css 调转180度:transform: rotate(180deg); 
- 2017年值得学习的3个CSS特性
		原文:https://bitsofco.de/3-new-css-features-to-learn-in-2017/译文:http://caibaojian.com/3-new-css-featur ... 
- 即将来到: CSS Feature Queries (CSS特性查询)
		Feature Queries 是CSS3 Conditional Rules specification中的一部分,它支持“@supports”规则,“@supports”规则可以用来测试浏览器是否 ... 
- CSS3的transition和transform
		CSS3中的transition和transform是制作HTML5动画一定要使用到的两个属性. 注:这篇文章不考虑兼容性,只讨论webkit核心的浏览器.所以本文的所有例子请用chrome,safa ... 
- css基础(css书写 背景设置 标签分类 css特性)
		css书写位置 行内式写法 <p style="color:red;" font-size:12px;></p> 外联式写法 <link re ... 
随机推荐
- linux同一客户端多个git账号的配置
			有时候我们需要在同一台机器上使用多个git账号,为了避免冲突,我们需要配置~/.ssh/config文件. 步骤一:用ssh-keygen命令生成一组新的id_rsa_new和id_rsa_new.p ... 
- AppBox Mvc数据库初始化
			下载AppBoxMvc后,以为CTRL_F5运行后就能数据库初始化了.一直报失败 我的环境是VS2017,利用VS2017自带的数据库 后修改: 1. 修改AppBoxContext.cs publ ... 
- AppBox中main树节点单击事件JS(还有叶子的节点的页面链接)
			AppBox中main.aspx.csif (menu.IsTreeLeaf) { node.Leaf = true; ... 
- kvm 基本运维命令
			Kvm基本命令 一.查询命令 1.列出所有的虚拟机 virsh list –all 2.显示虚拟机信息 virsh dominfo kvm-1 3.显示虚拟机内存和cpu的使用情况 yum insta ... 
- ionic使用常见问题(八)——PHP无法获取$http的post数据
			一个简单的post请求 $http.post('do-submit.php',myData) .success(function(){ // some code }); 可是,用angularjs ... 
- Mycat 数据库分库分表中间件
			http://www.mycat.io/ Mycat 国内最活跃的.性能最好的开源数据库中间件! 我们致力于开发高性能的开源中间件而努力! 实体书Mycat权威指南 »开源投票支持Mycat下载 »s ... 
- 分析报告:云之家V9 VS 钉钉3.5
			http://news.yesky.com/hotnews/1/244252501.shtml 1.市场调研 1.1 企业需求 笔者所在单位是一家中型企业,企业流程和信息化基础较为成熟.随着移动互联网 ... 
- 从c#数组求和说起
			c#是一种玩具语言 为什么这么说, 举个简单的例子,提问:对数组[1,2,3]求和有几种方法? 我能说出来的,四种.说出来,不是上网查出来. for,foreach,sum,while. for好像大 ... 
- android开发常用组件【持续更新中。。。】
			UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ... 
- 2013-7-27 802.1X学习
			最近搭了企业级加密的server 2003服务器,教程完全google,无任何自主创新.折腾了一周,总算搞定了,同时也验证了server 2003下的TLS和PEAP0加密算法是正常的. 至于搭建se ... 
