css3之transform-style 3D旋转效果

//运行看效果

<!DOCTYPE html>

<html lang="en">
<head>
  <meta charset="utf-8" />

  <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame 
       Remove this if you use the .htaccess -->
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

  <title>3D旋转效果</title>
  <meta name="description" content="" />
  <meta name="author" content="Administrator" />
  <meta name="viewport" content="width=device-width; initial-scale=1.0" />

  <!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
  <link rel="shortcut icon" href="/favicon.ico" />
  <link rel="apple-touch-icon" href="/apple-touch-icon.png" />
  <style type="text/css">
  *{font-size: 14px;color: #fff; padding:0; margin:0;}
  #container {
position: relative;
height: 300px;
width: 300px;
-webkit-perspective: 500;/*角度*/
margin-top: 200px;
margin-right: auto;
margin-left: auto;
  }
  
  #parent {
margin: 10px;
width: 280px;
height: 280px;
background-color: gray;
opacity: 0.8;
-webkit-transform-style: preserve-3d;
-webkit-animation: spin 5s infinite linear;
}
  #parent > div {
position: absolute;
top: 40px;
left: 40px;
width: 280px;
height: 200px;
padding: 10px;
-webkit-box-sizing: border-box;
}

#parent > :first-child {
background-color: #000;
-webkit-transform: translateZ(-100px) rotateY(45deg);
}
#parent > :last-child {
background-color: #333;
-webkit-transform: translateZ(50px) rotateX(20deg);
-webkit-transform-origin: 50% top;
}

/*执行y轴旋转*/

  @-webkit-keyframes spin {
from {-webkit-transform: rotateY(0);}
to {-webkit-transform: rotateY(360deg);}
}
 
  </style>
</head>
<body>
  <div>
    <header>
      <h1>transform-style的3D效果测试</h1>
    </header>
<div>
<div id="container">
<div id="parent">
<div>3D旋转效果</div>
<div>3D3D3D3D3D</div>
</div>
</div>
</div>
    <footer>
    </footer>
  </div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要产生旋转木马效果,需要使用CSS的transform-style和perspective属性。 1. 首先,在父元素上添加transform-style: preserve-3d;,让子元素位于三维空间中。 2. 然后,在父元素上添加perspective属性,指定透视距离,产生立体效果。 3. 接着,在子元素上设置transform: rotateY(angle),使子元素绕Y轴旋转,产生旋转木马效果。 以下是示例代码: HTML代码: ``` <div class="carousel"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> <div class="item">Item 4</div> <div class="item">Item 5</div> </div> ``` CSS代码: ``` .carousel { width: 400px; height: 300px; margin: 0 auto; position: relative; transform-style: preserve-3d; perspective: 1000px; } .item { position: absolute; width: 300px; height: 200px; background-color: #ccc; text-align: center; line-height: 200px; font-size: 24px; transition: transform 1s; } .item:nth-child(1) { transform: rotateY(0deg) translateZ(200px); } .item:nth-child(2) { transform: rotateY(72deg) translateZ(200px); } .item:nth-child(3) { transform: rotateY(144deg) translateZ(200px); } .item:nth-child(4) { transform: rotateY(216deg) translateZ(200px); } .item:nth-child(5) { transform: rotateY(288deg) translateZ(200px); } .carousel:hover .item { transform: rotateY(36deg); } ``` 在这个示例中,旋转木马的父元素是一个div.carousel,子元素是div.item。在.carousel上设置了transform-style: preserve-3d;和perspective: 1000px;,使子元素位于三维空间中,并产生立体效果。在.item上设置了transform: rotateY(angle)和transition: transform 1s;,使子元素绕Y轴旋转,并且在1秒内完成旋转。在.carousel:hover .item中,设置了旋转的角度为36度,使旋转更加流畅。 通过这种方式,就可以使用CSS的transform-style和perspective属性,结合使用产生旋转木马效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值