CSS中的transform-origin

前言

在没有设置过transform-origin属性时,CSS变形进行的旋转、移位、缩放等操作都是以元素自己中心(变形原点/中心点)位置进行变形的。而CSS3 中的transform-origin属性用于设置旋转元素的基点位置,熟悉使用transform-origin并结合CSS3动画可以使元素沿着某一基点进行旋转,位移

基本语法

1.语法

transform-origin: x-axis y-axis z-axis;

2.默认值

transform-origin:50% 50% 0;

3.单位

transform-origin属性值可以是百分比、em、px等具体的值,也可以是top、right、bottom、left和center这样的关键词。

示例

1.正常情况下默认值选择45deg

        .outer {
			      width: 100px;
			      height: 100px;
			      background-color: gray;
			    }
			    .inner {
			      transform: rotate(45deg);
			      width: 100%;
			      height: 100%;
			      background-color: indianred;
				}

 2.以顶部选择180deg

			 .outer {
			      width: 100px;
			      height: 100px;
			      background-color: gray;
			    }
			    .inner {
			      transform: rotate(180deg);
			      width: 100%;
			      height: 100%;
			      background-color: indianred;
				  transform-origin:50% 0;
				   /* animation:an-circle 3s ease-in-out infinite; */
				}

3.以右侧中心旋转180deg 

         .outer {
			      width: 100px;
			      height: 100px;
			      background-color: gray;
			    }
			    .inner {
			    transform: rotate(180deg);
					transform-origin:100% 50%;
			      width: 100%;
			      height: 100%;
			      background-color: indianred;
				   animation:an-circle 3s ease-in-out infinite;
				}
				@keyframes an-circle  {
				       to {
				          transform: rotate(1.5turn);
				       }
				    }

4.以右上角为中心开始旋转

         .outer {
			      width: 100px;
			      height: 100px;
			      background-color: gray;
			    }
			    .inner {
			    /* transform: rotate(0deg); */
			         transform-origin:100% 0;
			      width: 100%;
			      height: 100%;
			      background-color: indianred;
				   animation:an-circle 3s ease-in-out infinite;
				}
				@keyframes an-circle  {
				       to {
				          transform: rotate(1.5turn);
				       }
				    }

 从以上的几个例子当中可以清楚地知道了transform-origin: x-axis y-axis;属性值的所代表的偏移位置,了解各属性值所代表的偏移位置后可以很灵活得使元素按照某一基准点进行旋转。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值