20231028-黑马web进阶-平面转换

文章目录

位移:translate

语法:transform:translate(水平移动距离,垂直移动距离);
取值(正负均可):
①像素单位数值
②百分比(参照物为盒子自身尺寸)
注意:
①X轴正向为右,Y轴正向为下
②translate()如果只给出一个值,表示X轴方向移动距离
③单独设置某个方向的移动距离:translateX()&translateY()

案例-双开门

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>平面转换-位移基本使用</title>
  <style>
    .father {
      width: 960px;
      height: 540px;
      margin: 100px auto;
      border: 1px solid black;
      background-color: blueviolet;
      overflow: hidden;
      /* 超出父级范围的隐藏 */
    }

    .sonLeft {
      width: 50%;
      height: 100%;
      float: left;
      background-color: aqua;
      transition: all 0.5s;
      /* 设置过渡效果,持续时间为0.5秒 */
    }

    .sonRight {
      width: 50%;
      height: 100%;
      float: right;
      background-color: skyblue;
      transition: all 0.5s;
      /* 设置过渡效果,持续时间为0.5秒 */
    }

    /* 定义鼠标悬停时子元素的样式 */
    .father:hover .sonLeft {
      transform: translate(-100%);
      /* 设置位移变换效果,将子元素向右下方位移75% */
    }

    .father:hover .sonRight {
      transform: translateX(100%);
      /* 设置位移变换效果,将子元素向右下方位移75% */
    }
  </style>
</head>

<body>
  <div class="father">
    <div class="sonLeft"></div>
    <div class="sonRight"></div>
  </div>
</body>

</html>

旋转

旋转:transform: rotate(角度deg);(角度正负均可)

转换原点:transform-origin:原点水平位置 原点垂直位置;(默认原点是盒子的中心点)
取值:
①方位名词:left top right bottom center
②像素单位数值
③百分比(参照盒子自身尺寸计算)

案例

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>平面转换-旋转</title>
  <style>
    .box {
      width: 200px;
      height: 200px;
      margin: 400px auto;
      background-color: aquamarine;
      transition: all 2s;
      /* 设置过渡效果,持续时间为2秒 */
      transform-origin: right bottom;
      /* 设置旋转的原点为盒子的右下角 */
    }

    /* 定义鼠标悬停时盒子的样式 */
    .box:hover {
      transform: rotate(360deg);
      /* 设置旋转变换效果,将盒子顺时针旋转360度(一圈) */
    }
  </style>
</head>

<body>
  <div class="box"></div>
</body>

</html>

多重转换

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>平面转换-多重转换</title>
  <style>
    .box {
      width: 800px;
      height: 200px;
      border: 1px solid black;
    }

    .circle {
      width: 200px;
      height: 200px;
      background-color: blueviolet;
      border-radius: 50%;
      transition: all 3s;
    }

    .box:hover .circle {
      transform: translate(600px) rotate(720deg);
      /* 注意:旋转会改变坐标轴向,多重转换如果涉及旋转往最后书写 */
    }
  </style>
</head>

<body>
  <div class="box">
    <div class="circle"></div>
  </div>
</body>

</html>

缩放

语法:transform:scale(x轴缩放倍数,y轴缩放倍数);
技巧:
一般情况下,只为scale设置一个值,表示x轴和y轴等比例缩放
transform:scale(缩放倍数);
scale值大于1表示放大,scale值小于1表示缩小

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>平面转换-缩放</title>
  <style>
    .box {
      width: 300px;
      height: 300px;
      background-color: blueviolet;
      border: 1px solid black;
      margin: 300px auto;
      overflow: hidden;
    }

    .btn {
      width: 50px;
      height: 50px;
      background-color: cadetblue;
      margin: 125px auto;
      border-radius: 50%;
      transform: scale(8);
      opacity: 0;
      transition: all 0.3s;
    }

    .box:hover .btn {
      transform: scale(1);
      opacity: 1;
    }
  </style>
</head>

<body>
  <div class="box">
    <div class="btn"></div>
  </div>
</body>

</html>

渐变background-image:linear-gradient(颜色1,颜色2,...);

常用:background-image:linear-gradient(transparent,rgba(0, 0, 0, .5));

相关推荐
长天一色4 分钟前
【ECMAScript 从入门到进阶教程】第三部分:高级主题(高级函数与范式,元编程,正则表达式,性能优化)
服务器·开发语言·前端·javascript·性能优化·ecmascript
NiNg_1_23422 分钟前
npm、yarn、pnpm之间的区别
前端·npm·node.js
秋殇与星河24 分钟前
CSS总结
前端·css
BigYe程普1 小时前
我开发了一个出海全栈SaaS工具,还写了一套全栈开发教程
开发语言·前端·chrome·chatgpt·reactjs·个人开发
神之王楠1 小时前
如何通过js加载css和html
javascript·css·html
余生H1 小时前
前端的全栈混合之路Meteor篇:关于前后端分离及与各框架的对比
前端·javascript·node.js·全栈
程序员-珍1 小时前
使用openapi生成前端请求文件报错 ‘Token “Integer“ does not exist.‘
java·前端·spring boot·后端·restful·个人开发
axihaihai1 小时前
网站开发的发展(后端路由/前后端分离/前端路由)
前端
流烟默1 小时前
Vue中watch监听属性的一些应用总结
前端·javascript·vue.js·watch
2401_857297912 小时前
招联金融2025校招内推
java·前端·算法·金融·求职招聘