CSS_变形之平移&旋转&缩放

平移translate

X和Y轴平移 transform: translateX(-50%) translateY(-50%);

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			body{
				background-color: #dddddd
			}
			.box1 {
				width: 100px;
				height: 100px;
				background-color: #00FF00;
				margin: 0 auto;

			}

			.box2 {
				width: 100px;
				height: 100px;
				background-color: #ffff00;
				margin: 0 auto;
				/*
				变形就是指通过css来改变元素的形状或位置
				变形不会影响到页面的布局
				transform用来设置元素的变形效果
				平移:
					translateX()沿着x轴方向平移
					translateY()沿着y轴方向平移
					translateZ()沿着z轴方向平移
						-平移元素,相对于自身计算
				*/

				transform: translateX(20px);
			}

			.box3 {
				background-color: #ff0000;
				position: absolute;
				/* 相对于包含块的 */
				left: 50%;
				top: 50%;
				transform: translateX(-50%) translateY(-50%);

			}

			.box4,
			.box5 {
				width: 220px;
				height: 300px;
				background-color: #fff;
				float: left;
				margin: 0 10px;
				transition: all .3s;
			}

			.box4:hover,
			.box5:hover {
				transform: translateY(-4px);
				box-shadow: 0 0 10px rgba(6, 0, 0, .3)
			}
		</style>
	</head>
	<body>
		<div class="box1"></div>
		<div class="box2"></div>
		
		<div class="box3">333</div>
		
		<div class="box4"></div>
		<div class="box5"></div>
	</body>
</html>

在这里插入图片描述

Z轴平移 transform: translateZ(200px);

z轴平移,调整元素在z轴的位置,正常情况就是调整元素和人眼之间的距离,
距离越大,元素离人越近
z轴平移属于立体效果(近大远小),默认情况下网页是不支持透视,如果需要看见效果
必须要设置网页的视距

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        html {
            /* 设置当前网页的视距为800px,人眼距离网页的距离 */
            perspective: 800px;
        }
        
        body {
            border: 1px red solid;
            background-color: #bfa
        }
        
        .box1 {
            width: 100px;
            height: 100px;
            background-color: #00FF00;
            margin: 200px auto;
            /* 
				 z轴平移,调整元素在z轴的位置,正常情况就是调整元素和人眼之间的距离,
				 距离越大,元素离人越近
				 z轴平移属于立体效果(近大远小),默认情况下网页是不支持透视,如果需要看见效果
				 必须要设置网页的视距
				 */
            transition: 2s;
        }
        
        body:hover .box1 {
            transform: translateZ(200px);
        }
    </style>
</head>

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

</html>

旋转transform: rotateZ(1turn);

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        html {
            /* 设置当前网页的视距为800px,人眼距离网页的距离 */
            perspective: 800px;
        }
        
        body {
            border: 1px red solid;
            background-color: #bfa
        }
        
        .box1 {
            width: 100px;
            height: 100px;
            background-color: #00FF00;
            margin: 200px auto;
            /* 
				 z轴平移,调整元素在z轴的位置,正常情况就是调整元素和人眼之间的距离,
				 距离越大,元素离人越近
				 z轴平移属于立体效果(近大远小),默认情况下网页是不支持透视,如果需要看见效果
				 必须要设置网页的视距
				 */
            transition: 2s;
        }
        
        body:hover .box1 {
            /*
				通过旋转可以使元素沿着x y或z旋转指定的角度
				rotateX( )
				rotateY( )
				rotateZ( )
				*/
            /* transform: rotateZ(1turn); 转一圈*/
            /* transform: rotateZ(45deg); */
            先向Z轴平移再旋转,也可以返过来
            /* transform: translateZ(400px) rotateY(180deg); */
            transform: rotateY(180deg);
            /* 是否显示元素的背面 */
            backface-visibility: hidden;
        }
    </style>
</head>

<body>
    <div class="box1">
        <img src="mitu.png">
    </div>
</body>

</html>

缩放

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.box1 {
				width: 100px;
				height: 100px;
				background-color: #bfa;
				transition: 2s;
				margin: 100px auto;
			}

			.box1:hover {
				/*
			对元素进行缩放的函数:
			scaleX()水平方向缩放
			scaleY()垂直方向缩放
			scale()双方向的缩放
			*/
				transform: scale(2)
			}

			.img-wrapper {
				width: 200px;
				height: 200px;
				border: 1px red solid;
				overflow: hidden;
			}

			img {
				transition: .2s;
			}

			.img-wrapper:hover img {
				transform: scale(1.2);
			}
		</style>
	</head>
	<body>
		<div class="box1">
			<div class="img-wrapper">
				<img src="./1.jpg"
					width="100%" height="100%">
			</div>
		</div>
	</body>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值