CSS3 3D transforms系列-3D旋转木马

1,html结构:

<section id="container">
    <div id="carousel">
        <figure>1</figure>
        <figure>2</figure>
        <figure>3</figure>
        <figure>4</figure>
        <figure>5</figure>
        <figure>6</figure>
        <figure>7</figure>
        <figure>8</figure>
        <figure>9</figure>
        <figure>10</figure>
    </div>
</section>

2,css基本样式

我们给#carousel中的每一个面设置一个20像素的间隙,可以通过left:10pxtop:10px来实现它。这样,每一个main的有效宽度为112px

#container{perspective: 1000px;position: relative;left:200px;top:200px;width: 200px;height: 112px;}

#carousel{transform-style: preserve-3d;position: absolute;width: 200px;height: 100px; transition: all 4s;}

#carousel figure{width: 200px;height: 100px;border: 2px #000 solid;opacity: 0.8;text-align: center;display: block;
    font-size: 40px;color: #fff;line-height: 100px;position: absolute;top:10px;left:10px;}

3,接下来,我们要旋转每一个面,确定其位置。

#carousel中有10个面,如果整个旋转木马的面是平均分布的,那么每个面需要在上一个面的基础上多旋转36度(360 / 10);

类似如下图所示算法:

旋转木马俯视图

旋转木马所需参数的计算公式

tan(36/2) = (112px/2) / r

tan(18) =56px/r

r= 56/tan(18)

r= 173px;

/*10张图片,所以每张图片围绕Y轴旋转36度,然后让每个figure都离中心位置173px,173这个数字
是根据图片的宽度来的,具体算法是--图片宽度除以2,再除以tan(18),自己可以画张多边形研究一下*/

#carousel figure:nth-child(1){background-color: peru;transform: rotateX(0deg) translateZ(173px)}
#carousel figure:nth-child(2){background-color: palegoldenrod;transform: rotateX(36deg) translateZ(173px)}
#carousel figure:nth-child(3){background-color: palegreen;transform: rotateX(72deg) translateZ(173px)}
#carousel figure:nth-child(4){background-color: paleturquoise;transform: rotateX(108deg) translateZ(173px)}
#carousel figure:nth-child(5){background-color: palevioletred;transform: rotateX(144deg) translateZ(173px)}
#carousel figure:nth-child(6){background-color: papayawhip;transform: rotateX(180deg) translateZ(173px)}
#carousel figure:nth-child(7){background-color: plum;transform: rotateX(216deg) translateZ(173px)}
#carousel figure:nth-child(8){background-color: powderblue;transform: rotateX(252deg) translateZ(173px)}
#carousel figure:nth-child(9){background-color: deeppink;transform: rotateX(288deg) translateZ(173px)}
#carousel figure:nth-child(10){background-color: deepskyblue;transform: rotateX(324deg) translateZ(173px)}

4,旋转木马效果就是让carousel围绕x轴每次旋转36度

简单实例:鼠标移动旋转

$(function(){
    var i = 0;
    $("#carousel").mousemove(function(){
        i++;
        $(this).css("transform","rotateX("+(i*36)+"deg)");
    });
})


x轴同理





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值