坐标系统
要使用元素变形操作需要掌握坐标轴,然后通过改变不同坐标来控制元素的变形。
image-20190901174833435.9b3582ae.png
- X轴是水平轴
- Y轴是垂直轴
( Z轴是纵深轴
变形操作
使用 transform 规则控制元素的变形操作,包括控制移动、旋转、倾斜、3D转换等,下面会详细介绍每一个知识点。
下面是CSS提供的变形动作。
重要:
- translate在进行设置偏移量的时候。并不会两个或者多个元素叠在一起。除非是元素设置position。
- 比如:两个没有设置position的div。长宽都是100%的。那么两个元素都设置translateX(-100%);这样的话实际只会隐藏一个元素。除非设置成一个translateX(-200%),一个translateX(-100%);这样两个才都会隐藏掉
选项 说明
| none | 定义不进行转换。 |
|---|---|
| translate(x,y) | 定义 2D 转换。 |
| translate3d(x,y,z) | 定义 3D 转换。 |
| translateX(x) | 定义转换,只是用 X 轴的值。 |
| translateY(y) | 定义转换,只是用 Y 轴的值。 |
| translateZ(z) | 定义 3D 转换,只是用 Z 轴的值。 |
当 translate设置为百分比时,其参照是当前div的宽高
变形叠加
重复设置变形操作时只在原形态上操作。
默认处理
下面设置了两次移动,并不会移动 550px 而是只移动50px。
<style>
div {
transform: translateX(500px);
width: 100px;
height: 100px;
background: #9b59b6;
}
div:nth-child(1) {
transform: translateX(50px);
}
</style>
<div></div>
伪类叠加
Untitled-7734353.de347599.gif
<style>
div {
transition: 2s;
transform: translateX(200px) translateX(50px);
width: 100px;
height: 100px;
background: #9b59b6;
}
div:hover {
transition: 2s;
transform: translateX(100px);
}
</style>
<div></div>
行级元素
行级元素不产生变形效果,将其转为 inline-block 或 block 以及弹性元素时都可以产生变化效果。
Untitled-7734657.cce0a000.gif
<style>
span {
display: inline-block;
transition: 2s;
transform: translateX(100px) translateX(50px);
width: 100px;
height: 100px;
background: #9b59b6;
}
span:hover {
transition: 2s;
transform: translateX(100px);
}
</style>
<span>hdcms</span>
伪类状态
:hover
鼠标移动上后发生改变。
image-20190902133840698
article div:nth-child(2):hover {
transform: rotate(180deg);
}
:target
以下操作变化时间为零秒,通过掌握后面的过渡动画可以控制变化时间。
Untitled-7404551.0296ff4c.gif
<style>
article {
width: 300px;
height: 300px;
display: grid;
gap: 10px;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(2, 1fr);
position: relative;
border: solid 5px silver;
color: white;
}
article div a {
color: white;
text-decoration: none;
}
article div,
article div aside {
background: blueviolet;
background-clip: content-box;
padding: 5px;
border: solid 2px blueviolet;
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
article div aside {
position: absolute;
display: none;
width: 100px;
height: 100px;
}
aside:target {
display: block;
transform: translateY(150px);
box-shadow: 0 0 10px #ddd;
}
</style>
<article>
<div>
<a href="#hdcms">hdcms</a>
<aside id="hdcms">
内容管理系统
</aside>
</div>
<div>
<a href="#houdunren">houdunren</a>
<aside id="houdunren">
在线社区
</aside>
</div>
</article>
移动元素
沿X轴移动时正值向右移动、负值向左移动
沿Y轴移动时正值向下移动、负值向上移动
如果使用百分数将控制元素的原尺寸计算百分比然后移动
可同时设置多个值,解析器会从左向右依次执行
变形是在原基础上更改,即第二次设置值时不是在第一次值上变化
translateX
正值向右移动、负值向左移动。
download.png
<style>
article {
width: 300px;
height: 300px;
position: relative;
border: solid 5px silver;
}
article div {
width: 100px;
height: 100px;
background: blueviolet;
box-sizing: border-box;
position: absolute;
left: 50%;
margin-left: -50px;
top: 50%;
margin-top: -50px;
}
article div:nth-child(1) {
background: #e9dddd;
}
article div:nth-child(2) {
transform: translateX(100px);
}
</style>
...
<article>
<div></div>
<div></div>
</article>
translateY
正值向下移动、负值向上移动。
download-1.png
article div:nth-child(2) {
transform: translateY(100px);
}
translate
使用 translate 可以控制按X、Y同时移动操作,第一个值控制X移动,第二个值控制Y移动。
download-2.png
article div:nth-child(2) {
transform: translate(100px, -100px);
}
百分比移动
元素宽度为100px设置50%时将移动50px,即百分比是指元素的尺寸的百分比。
article div:nth-child(2) {
transform: translateX(50%);
}
元素居中
居中可以使用多种方式,如弹性布局、定位操作,下面来看使用移动操作居中。
download-3.png
<style>
body {
height: 100vh;
}
main {
width: 400px;
height: 400px;
border: solid 5px silver;
position: relative;
}
main div {
width: 100px;
height: 100px;
background: blueviolet;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
</style>
<main>
<div></div>
</main>
translateZ
控制Z轴移动,正数向外、负数向里移动。因为Z轴是透视轴没有像X/Y一样的固定尺寸,所以不能使用百分数。
Untitled-7827784.63fc4fc8.gif
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
list-style: none;
}
body {
width: 100vw;
height: 100vh;
background: #34495e;
}
main {
position: absolute;
left: 50%;
top: 50%;
width: 200px;
height: 200px;
transform-style: preserve-3d;
transition: 2s;
transform: perspective(900px) rotateY(60deg);
}
body:hover main {
transform: perspective(600px) rotateY(60deg) scaleZ(5);
}
div {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #f1c40f;
}
div.b {
background: #8e44ad;
transform: translateZ(-100px);
}
</style>
<main>
<div class="f"></div>
<div class="b"></div>
</main>
translate3d
用于同时控制X/Y/Z轴的移动,三个值必须输入如果某个轴不需要移动时设置为零。
Untitled-7824321.6a82c902.gif
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
list-style: none;
}
body {
width: 100vw;
height: 100vh;
background: #34495e;
}
main {
position: absolute;
left: 50%;
top: 50%;
width: 200px;
height: 200px;
background: #f1c40f;
perspective: 600px;
transform: perspective(600px) rotateY(35deg);
transition: 2s;
}
body:hover main {
transform: perspective(600px) rotateY(35deg) translate3d(50%, 50%, 200px);
}
</style>
<main>
<div></div>
</main>
渐变表单
Untitled-7851179.821af195.gif
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
width: 100vw;
height: 100vh;
background: #34495e;
}
main {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 300px;
height: 300px;
border: solid 5px silver;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.field {
position: relative;
overflow: hidden;
margin-bottom: 20px;
}
.field::before {
content: '';
position: absolute;
left: 0;
height: 2px;
bottom: 0;
width: 100%;
background: linear-gradient(to right, white, #1abc9c, #f1c40f, #e74c3c, white);
transform: translateX(-100%);
transition: 2s;
}
.field:hover::before {
transform: translateX(100%);
}
.field input {
border: none;
outline: none;
background: #ecf0f1;
padding: 10px;
}
</style>
<main>
<div class="field">
<input type="text" placeholder="请输入后盾人帐号">
</div>
<div class="field">
<input type="text" placeholder="请输入密码">
</div>
</main>
页面切换
下面是使用移动效果制作的页面切换效果。
Untitled-7584912.730ab731.gif
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
* {
padding: 0;
margin: 0;
}
a {
text-decoration: none;
}
body {
display: flex;
width: 100vw;
height: 100vh;
flex-direction: column;
}
main {
position: relative;
background: #f3f3f3;
flex: 1;
overflow: hidden;
}
nav {
display: flex;
justify-content: space-around;
align-items: center;
height: 8vh;
text-align: center;
background: #34495e;
}
nav a {
flex: 1;
font-size: 1.3em;
text-transform: uppercase;
font-weight: bold;
opacity: .8;
color: white;
}
nav a:nth-child(2) {
border-right: solid 1px #aaa;
border-left: solid 1px #aaa;
}
main>div {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
transition: all 1s;
z-index: 1;
background: #f3f3f3;
opacity: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
transform: translate(0, -100%);
color: white;
font-size: 2em;
}
main>div:target {
opacity: 1;
transform: translate(0%, 0%);
}
main>div:nth-of-type(1):target {
background: #3498db;
}
main>div:nth-of-type(2):target {
background: #9b59b6;
}
main>div:nth-of-type(3):target {
background: #16a085;
}
div i[class^="fa"] {
font-size: 100px;
color: white;
}
</style>
<body>
<main>
<div id="home">
<i class="fa fa-home" aria-hidden="true"></i>
houdunren.com
</div>
<div id="video">
<i class="fa fa-vimeo" aria-hidden="true"></i>
</div>
<div id="live">
<i class="fa fa-viadeo" aria-hidden="true"></i>
</div>
</main>
<nav>
<a href="#home">home</a>
<a href="#video">video</a>
<a href="#live">live</a>
</nav>
</body>











网友评论