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. Oracle RAC Failover 详解

    Oracle  RAC 同时具备HA(High Availiablity) 和LB(LoadBalance). 而其高可用性的基础就是Failover(故障转移). 它指集群中任何一个节点的故障都不会 ...

  2. jQuery与DOM对象的转换

    一.jQuery与DOM对象的转换. 1.jQuery对象转换为DOM对象:$cr[0] 或 $cr.get(0) $cr为jQuery对象 2.DOM对象转换为jQuery对象:$(cr) cr为D ...

  3. Git密钥生成步骤SSH Key

    顺便推荐下自己的网站: 一个php后台极速开发框架 https://www.lotusadmin.top/ 一个有趣的网站 https://www.waytomilky.com/ Git是分布式的代码 ...

  4. 关于Spring IOC (DI-依赖注入)

    <Spring入门经典>这本书无论对于初学者或者有经验的工程师还是很值一看的,最近花了点时间回顾了Spring的内容,在此顺带记录一下,本篇主要与spring IOC相关 ,这篇博文适合初 ...

  5. ADO.NET EF 中的实体修改方法

    http://www.cnblogs.com/zfz15011/archive/2010/05/30/1747486.html 1.传统修改模式,看下列代码 using (NorthwindEntit ...

  6. C指针笔试题,蛋疼的多重指针运算,谭浩强的阴影

    对指针的概念清晰的话,做这种题只要耐心就行,然而看这种题就烦(被同学吐槽为谭浩强的阴影……草泥马这种C风格题有意义吗?出题人脑子被门夹了?而且C++11都不支持字面值字符串直接转换成char*了.好吧 ...

  7. Qt QTableWidget用法总结

    转载:李宏兵 QTableWidget是QT程序中常用的显示数据表格的空间,很类似于VC.C#中的DataGrid.说到QTableWidget,就必须讲一下它跟QTabelView的区别了. QTa ...

  8. RDD之七:Spark容错机制

    引入 一般来说,分布式数据集的容错性有两种方式:数据检查点和记录数据的更新. 面向大规模数据分析,数据检查点操作成本很高,需要通过数据中心的网络连接在机器之间复制庞大的数据集,而网络带宽往往比内存带宽 ...

  9. [UE4]产生开枪特效

  10. 如何下载spring的jar包?

    文转载至:https://www.cnblogs.com/yjmyzz/p/3847364.html Spring官网改版后,很多项目的完整zip包下载链接已经隐掉了,虽然Spring旨在引导大家用更 ...