css3翻转特效-Jquery和CSS33d六面体旋转特效

这是用jquery和CSS3制作的3D旋转六面体效果。 该特效使用GSAP制作动画css3翻转特效,使用CSStransform制作六面体。 该效果与除 IE 之外的其他现代浏览器兼容。

因为IE浏览器不知道自己支持transform-style:preserve3d,所以在IE中听到的不是六面体效果css3翻转特效,而是平面效果。

如何使用

css翻转效果_css旋转代码_css3翻转特效

在页面中引入jquery和TweenMax.min.js文件。


                  
                

HTML结构

3D六面体的HTML结构如下:

css翻转效果_css旋转代码_css3翻转特效

Front
Left
Right
Top
Bottom
Back

CSS 样式

css旋转代码_css翻转效果_css3翻转特效

为六面体添加以下 CSS 样式:

#trans3DDemo1 {
  position:relative;
  display:inline-block;
  width:250px;
  height:250px;
}
#trans3DBoxes1 div {
  position:absolute;
  width:248px;
  height:248px;
  padding:0;
  margin:0;
  background:url(../img/girl.jpg);
  border:solid 2px #FFF;
  display:block;
  text-align:center;
  font-size:36px;
  font-weight:bold;
}                  
                

JavaScript

页面DOM元素加载完成后,通过以下方式初始化六面体效果:

var trans3DDemo1 = $("#trans3DDemo1"), 
    trans3DBoxes1 = $("#trans3DBoxes1"),// div containing all the the boxes
    boxes1 = $("#trans3DBoxes1 div"), // all the boxes
    threeDTimeline = new TimelineMax({onUpdate:updateCube, repeat:-1}),
    stageW = ($(window).width())/2,
    stageH = ($(window).height())/2,
    stageX = (stageW-(trans3DBoxes1.width()/2)),
    stageY = (stageH-(250/2));
//transformPerspective gives the element its own vanishing point
//perspective allows all the child elements (orange boxes) to share the same vanishing point with each other
//transformStyle:"preserve3d" allows the child elements to maintain their 3D position (noticeable only when their parent div is rotated in 3D space)
TweenMax.set(trans3DBoxes1, {css:{transformPerspective:3000, transformStyle:"preserve-3d"}}); //saves a dozen lines of vendor-prefixed css ;)
/*
K, lets build a cube and place it
boxes1[0] = frontside
boxes1[1] = leftside
boxes1[2] = rightside
boxes1[3] = topside
boxes1[4] = bottomside
boxes1[5] = backside
*/
threeDTimeline.set(boxes1[0], {rotationX:0, rotationY:0, x:0, y:0, z:125, opacity:0.85})
              .set(boxes1[1], {rotationX:0, rotationY:-90, x:-125, y:0, z:0, opacity:0.85})
        .set(boxes1[2], {rotationX:0, rotationY:90, x:125, y:0, z:0, opacity:0.85})
        .set(boxes1[3], {rotationX:90, rotationY:0, x:0, y:-125, z:0, opacity:0.85})
        .set(boxes1[4], {rotationX:-90, rotationY:0, x:0, y:125, z:0, opacity:0.85})
        .set(boxes1[5], {rotationX:0, rotationY:180, x:0, y:0, z:-125, opacity:0.85})
        .set(trans3DBoxes1, {x:150, y:150, transformOrigin:"125px 125px 0px"});
// hover events
boxes1.each(function (index, element) {
      $(element).hover(over, out);
      function over(){
        TweenMax.to(element, 0.15, {opacity:0.33});
        //threeDTimeline.pause();
      }
      function out(){
        TweenMax.to(element, 0.15, {opacity:0.85});
        //threeDTimeline.play();
      }
});
threeDTimeline.to(trans3DBoxes1, 15, {css:{rotationY:360, rotationX:-720, transformOrigin:"125px 125px 0px"}, ease:Power0.easeNone});
function updateCube(){
  stageW = ($(window).width())/2;
  stageH = ($(window).height())/2;
  stageX = (stageW-(trans3DBoxes1.width()/2));
  stageY = (stageH-(250/2));
  TweenMax.to(trans3DBoxes1, 1, {css:{x:stageX, y:stageY}});
}