CSS3高级用法——2D转换、动画、3D转换学习笔记

该文详细介绍了CSS3中的转换特性,包括2D转换如translate的位移、rotate的旋转以及scale的缩放,还讲解了转换中心点transform-origin的设置。此外,文中还涉及到了CSS3动画的创建与应用,如定义关键帧@keyframes和使用不同动画属性。最后,文章探讨了3D转换,包括translate3d、perspective和rotate3d等,以及如何实现3D翻转效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CSS3转换

转换:元素的位移、旋转、缩放

6.1 2D转换

  • 移动translate

语法:

在这里插入图片描述

特点:不会影响其他的盒子

小技巧:实现盒子的水平居中、垂直居中——定位+转换

.fa {
  position: relative;
  width: 400px;
  height: 400px;
  background-color: antiquewhite;
}
.son {
  position: absolute;
  width: 200px;
  height: 200px;
  background-color: aquamarine;
  top: 50%;
  left: 50%;
  /*往上、往左走本身的一半*/
  transform: translate(-50%, -50%);
}
  • 旋转rotate

语法:

在这里插入图片描述

img {
  width: 200px;
  height: 200px;
  transform: rotate(0deg);
  border-radius: 50%;
  border: 5px solid #FFEAB2;
  /* 过渡效果 */
  transition: all 1s;
}
img:hover {
  transform: rotate(360deg);
}

小技巧:实现三角

在这里插入图片描述

div {
  position: relative;
  width: 150px;
  height: 30px;
  border: 1px solid #000;
}
div::after {
  content: '';
  top: 5px;
  right: 15px;
  position: absolute;
  width: 15px;
  height: 15px;
  border-right: 1px solid #000;
  border-bottom: 1px solid #000;
  transform: rotate(45deg);
  transition: all 1s;
}
div:hover::after {
  /*实现上下翻转*/
  transform: rotate(225deg);
}
  • 转换中心点transform- origin

语法:

在这里插入图片描述

练习:
在这里插入图片描述

div {
  overflow: hidden;
  position: relative;
  width: 150px;
  height: 150px;
  border: 2px solid #FBBC03;
  margin: 100px auto;
}
div::before {
  top: 0;
  left: 0;
  position: absolute;
  content: 'TNT';
  width: 150px;
  height: 150px;
  background-color: #FBBC03;
  transform: rotate(180deg);
  transform-origin: left bottom;
  transition: all 7s;
}
div:hover::before {
  transform: rotate(0deg);
}
  • 缩放scale

语法:

在这里插入图片描述

注意:里面的x,y是指的倍数,大于1是放大,小于1是缩小

特点:不影响其他盒子

练习1:图片的缩放

在这里插入图片描述

div {
  overflow: hidden;
  width: 200px;
  height: 200px;
  margin: 100px auto;
}
div a img {
  transition: all .6s;
}
div a img:hover {
  transform: scale(1.2);
}

2D转换的综合写法:

transform*: translate(100px, 100px) rotate(45deg) scale(1.1);

位移一定写在最前面!!

6.2动画

动画:

第一步:定义动画

第二步:调用动画

定义动画:

在这里插入图片描述

0%是动画的开始,100%是动画的结束,可以替换为from和to

调用动画:

在这里插入图片描述

基本使用:

@keyframes yidong {
  0% {
    transform: translate(0px);
  }

  100% {
    transform: translate(500px, 0);
  }
}
div {
  width: 200px;
  height: 200px;
  background-color: antiquewhite;
  animation-name: yidong;
  animation-duration: 2s;
}

动画序列:

@keyframes yidong {
  0% {
    transform: translate(0px);
  }
  25% {
    transform: translate(500px, 0);
  }
  50% {
    transform: translate(500px, 300px);
  }
  75% {
    transform: translate(0, 300px);
  }
  100% {
    transform: translate(0, 0);
  }
}

动画的常用属性:

在这里插入图片描述

简写:
在这里插入图片描述

练习:热点图
在这里插入图片描述

body {
  background-color: #333;
}
.map {
  position: relative;
  width: 747px;
  height: 616px;
  background: url(media/map.png) no-repeat;
  margin: 0 auto;
}
.city {
  position: absolute;
  top: 227px;
  right: 193px;
  color: #fff;
}
.dotted {
  width: 8px;
  height: 8px;
  background-color: #09f;
  border-radius: 50%;
}
.city div[class^="pulse"] {
  /* 保证我们小波纹在父盒子里面水平垂直居中 放大之后就会中心向四周发散 */
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 8px;
  height: 8px;
  box-shadow: 0 0 12px #009dfd;
  border-radius: 50%;
  animation: pulse 1.2s linear infinite;
}
.city div.pulse2 {
  animation-delay: 0.4s;
}
.city div.pulse3 {
  animation-delay: 0.8s;
}
@keyframes pulse {
  0% {}
  70% {
    /* transform: scale(5);  我们不要用scale 因为他会让 阴影变大*/
    width: 40px;
    height: 40px;
    opacity: 1;
  }
  100% {
    width: 70px;
    height: 70px;
    opacity: 0;
  }
}

animation-timing-function:动画的速度曲线

在这里插入图片描述

奔跑的熊大:

body {
  width: 100%;
  height: 336px;
  position: absolute;
  background: url(media/bg1.png) no-repeat;
  animation: leftmove 20s linear infinite forwards;
}
div {
  position: absolute;
  z-index: 3;
  top: 200px;
  width: 200px;
  height: 100px;
  background: url(media/bear.png) no-repeat;
  animation: bear 2.5s steps(8) infinite, move 7s forwards;
}
@keyframes bear {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: -1600px 0;
  }
}
@keyframes move {
  0% {
    left: 0;
  }
  100% {
    left: 50%;
    transform: translate(-50%);
  }
}
@keyframes leftmove {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 100% 0;
  }
}

在这里插入图片描述

6.3 3D转换

三维坐标系:

在这里插入图片描述

  • 3D移动translate3d
   transform: translate3d(x,y,z);
  • 透视perspective

要加在父盒子上

  • 3D旋转rotate3d

遵循左手准则:

在这里插入图片描述

/* transform: rotateY(-45deg); */
/* 延x轴旋转 */
/* transform: rotate3d(1, 0, 0, 45deg); */
/* 延对角线旋转 */
transform: rotate3d(1, 1, 0, 45deg);
  • 3D呈现transfrom-style

控制子盒子是否开启三维立体环境

transform-style: preserve-3d;//开启3d环境

在这里插入图片描述

body {
  perspective: 500px;
}
.box {
  position: relative;
  width: 200px;
  height: 200px;
  margin: 100px auto;
  transform-style: preserve-3d;
  transition: all .5s;
}
.box:hover {
  transform: rotateY(-45deg);
}
.box div {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: aquamarine;
}
.box div:last-child {
  background-color: bisque;
  transform: rotateX(60deg);
}

练习:两个盒子翻转

在这里插入图片描述

body {
  perspective: 400px;
}
.box {
  position: relative;
  width: 300px;
  height: 300px;
  margin: 100px auto;
  transform-style: preserve-3d;
  transition: all 7s;
}
.box:hover {
  transform: rotateY(180deg);
}
.front,
.back {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  color: azure;
  font-size: 30px;
  text-align: center;
  line-height: 300px;
  border-radius: 50%;
}
.front {
  background-color: orange;
  z-index: 1;
  /* 背面隐藏 背面朝向用户时不可见 默认值是visible(可见) */
  backface-visibility: hidden;
}
.back {
  background-color: pink;
  transform: rotateY(180deg);
}

练习:盒子上下翻转

在这里插入图片描述

body {
  perspective: 400px;
}
.box {
  position: relative;
  width: 200px;
  height: 100px;
  margin: 100px auto;
  transform-style: preserve-3d;
  transition: all 2s;
}
.box:hover {
  transform: rotateX(90deg);
}
.front,
.back {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  color: azure;
  font-size: 20px;
  text-align: center;
  line-height: 100px;
}
.front {
  background-color: orange;
  z-index: 1;
  /* 背面隐藏 背面朝向用户时不可见 默认值是visible(可见) */
  backface-visibility: hidden;
  transform: translateZ(50px);
}
.back {
  background-color: pink;
  transform: translateY(50px) rotateX(-90deg);
}

练习:旋转木马

在这里插入图片描述

body {
  perspective: 1000px;
}
section {
  position: relative;
  width: 320px;
  height: 213px;
  margin: 150px auto;
  transform-style: preserve-3d;
  animation: move 10s linear infinite;
}
section:hover {
  animation-play-state: paused;
}
@keyframes move {
  0% {
    transform: rotateY(0deg);
  }
  100% {
    transform: rotateY(360deg);
  }
}
section div {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background: url(media/tnt.jpeg) no-repeat;
}
section div:first-child {
  transform: translateZ(300px);
}
section div:nth-child(2) {
  transform: rotateY(60deg) translateZ(300px);
}
section div:nth-child(3) {
  transform: rotateY(120deg) translateZ(300px);
}
section div:nth-child(4) {
  transform: rotateY(180deg) translateZ(300px);
}
section div:nth-child(5) {
  transform: rotateY(240deg) translateZ(300px);
}
section div:last-child {
  transform: rotateY(300deg) translateZ(300px);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值