1. 程式人生 > >利用css3做出立體摺疊效果

利用css3做出立體摺疊效果

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Show me the Furry Friend of the Day</title>
  6. <script  type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
  7. <style type="text/css">
  8. *{padding:0;margin:0}
  9. .header{width:100%;background:#000;height:50px;margin-bottom:20px;min-width:1000px}
  10. /*圖片摺疊效果樣式*/
  11. .view{margin:20px auto;display:block;width:640px;height:400px;border: 8px solid #fff;background-color: #EEE;background-image: -webkit-linear-gradient(hsla(0,0%,0%,.025), hsla(0,0%,100%,.05) 33%, hsla(0,0%,0%,.05) 33%, hsla(0,0%,100%,.05) 67%, hsla(0,0%,0%,.05) 67%, hsla(0,0%,100%,.025));-webkit-perspective: 800px;-moz-perspective: 800px;-o-perspective: 800px;-ms-perspective: 800px;perspective: 800px;position:relative;box-shadow: inset 0 0 0 .2em hsla(0,0%,0%,.1),
  12. inset 0 0 2em hsla(0,0%,0%,.05),0 .2em .25em hsla(0,0%,0%,.5);}
  13. .view .slice{
  14.         width:128px;
  15.         height: 100%;
  16.         z-index: 100;
  17.         -webkit-transform-style: preserve-3d;
  18.         -moz-transform-style: preserve-3d;
  19.         -o-transform-style: preserve-3d;
  20.         -ms-transform-style: preserve-3d;
  21.         transform-style: preserve-3d;
  22.         -webkit-transform-origin: left center;
  23.         -moz-transform-origin: left center;
  24.         -o-transform-origin: left center;
  25.         -ms-transform-origin: left center;
  26.         transform-origin: left center;
  27.         -webkit-transition: -webkit-transform 150ms ease-in-out;
  28.         -moz-transition: -moz-transform 150ms ease-in-out;
  29.         -o-transition: -o-transform 150ms ease-in-out;
  30.         -ms-transition: -ms-transform 150ms ease-in-out;
  31.         transition: transform 150ms ease-in-out;}
  32. .view-back{
  33.         width: 50%;
  34.         height: 100%;
  35.         position: absolute;
  36.         right: 0;
  37.         background-color: rgb(0,255,255);
  38.         background-image: -webkit-linear-gradient(hsla(0,0%,0%,.025), hsla(0,0%,100%,.05) 33%, hsla(0,0%,0%,.05) 33%, hsla(0,0%,100%,.05) 67%, hsla(0,0%,0%,.05) 67%, hsla(0,0%,100%,.025));
  39.         z-index: 0;
  40. }
  41. .view .s2, 
  42. .view .s3, 
  43. .view .s4, 
  44. .view .s5 {
  45.         -webkit-transform: translate3d(128px,0,0);
  46.         -moz-transform: translate3d(128px,0,0);
  47.         -o-transform: translate3d(128px,0,0);
  48.         -ms-transform: translate3d(128px,0,0);
  49.         transform: translate3d(128px,0,0);
  50. }
  51. /*為每個切片設定背景圖片位置*/
  52. .view .s1 {
  53.         background-position: 0px 0px;
  54. }
  55. .view .s2 {
  56.         background-position: -128px 0px;
  57. }
  58. .view .s3 {
  59.         background-position: -256px 0px;
  60. }
  61. .view .s4 {
  62.         background-position: -384px 0px;
  63. }
  64. .view .s5 {
  65.         background-position: -512px 0px;
  66. }
  67. .view .overlay {
  68.         width: 128px;
  69.         height: 100%;
  70.         opacity: 0;
  71.         position: absolute;
  72.         -webkit-transition: opacity 150ms ease-in-out;
  73.         -moz-transition: opacity 150ms ease-in-out;
  74.         -o-transition: opacity 150ms ease-in-out;
  75.         -ms-transition: opacity 150ms ease-in-out;
  76.         transition: opacity 150ms ease-in-out;
  77. }
  78. .view:hover .overlay {
  79.         opacity: 1;
  80. }
  81. .view img {
  82.         position: absolute;
  83.         z-index: 0;
  84.         -webkit-transition: left 0.3s ease-in-out;
  85.         -o-transition: left 0.3s ease-in-out;
  86.         -moz-transition: left 0.3s ease-in-out;
  87.         -ms-transition: left 0.3s ease-in-out;
  88.         transition: left 0.3s ease-in-out;
  89. }
  90. .view:hover .s2{
  91.         -webkit-transform: translate3d(127px,0,0) rotate3d(0,1,0,-45deg);
  92.         -moz-transform: translate3d(127px,0,0) rotate3d(0,1,0,-45deg);
  93.         -o-transform: translate3d(127px,0,0) rotate3d(0,1,0,-45deg);
  94.         -ms-transform: translate3d(127px,0,0) rotate3d(0,1,0,-45deg);
  95.         transform: translate3d(127px,0,0) rotate3d(0,1,0,-45deg);
  96. }
  97. .view:hover .s3, 
  98. .view:hover .s5{
  99.         -webkit-transform: translate3d(127px,0,0) rotate3d(0,1,0,90deg);
  100.         -moz-transform: translate3d(127px,0,0) rotate3d(0,1,0,90deg);
  101.         -o-transform: translate3d(127px,0,0) rotate3d(0,1,0,90deg);
  102.         -ms-transform: translate3d(127px,0,0) rotate3d(0,1,0,90deg);
  103.         transform: translate3d(127px,0,0) rotate3d(0,1,0,90deg);
  104. }
  105. .view:hover .s4{
  106.         -webkit-transform: translate3d(127px,0,0) rotate3d(0,1,0,-90deg);
  107.         -moz-transform: translate3d(127px,0,0) rotate3d(0,1,0,-90deg);
  108.         -o-transform: translate3d(127px,0,0) rotate3d(0,1,0,-90deg);
  109.         -ms-transform: translate3d(127px,0,0) rotate3d(0,1,0,-90deg);
  110.         transform: translate3d(127px,0,0) rotate3d(0,1,0,-90deg);
  111. }
  112. /*為每個切片建立漸變效果*/
  113. .view .s1 > .overlay {
  114.         background: -moz-linear-gradient(right, rgba(0,0,0,0.05) 0%, rgba(0,0,0,0) 100%);
  115.         background: -webkit-linear-gradient(right, rgba(0,0,0,0.05) 0%,rgba(0,0,0,0) 100%);
  116.         background: -o-linear-gradient(right, rgba(0,0,0,0.05) 0%,rgba(0,0,0,0) 100%);
  117.         background: -ms-linear-gradient(right, rgba(0,0,0,0.05) 0%,rgba(0,0,0,0) 100%);
  118.         background: linear-gradient(right, rgba(0,0,0,0.05) 0%,rgba(0,0,0,0) 100%);
  119. }
  120. .view .s2 > .overlay {
  121.         background: -moz-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255, 255, 255, 0.2) 100%);
  122.         background: -webkit-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255, 255, 255, 0.2) 100%);
  123.         background: -o-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255, 255, 255, 0.2) 100%);
  124.         background: -ms-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255, 255, 255, 0.2) 100%);
  125.         background: linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255, 255, 255, 0.2) 100%);
  126. }
  127. .view .s3 > .overlay {
  128.         background: -moz-linear-gradient(right, rgba(0,0,0,0.8) 0%, rgba(0,0,0,0.2) 100%);
  129.         background: -webkit-linear-gradient(right, rgba(0,0,0,0.8) 0%,rgba(0,0,0,0.2) 100%);
  130.         background: -o-linear-gradient(right, rgba(0,0,0,0.8) 0%,rgba(0,0,0,0.2) 100%);
  131.         background: -ms-linear-gradient(right, rgba(0,0,0,0.8) 0%,rgba(0,0,0,0.2) 100%);
  132.         background: linear-gradient(right, rgba(0,0,0,0.8) 0%,rgba(0,0,0,0.2) 100%);
  133. }
  134. .view .s4 > .overlay {
  135.         background: -moz-linear-gradient(left, rgba(0,0,0,0.8) 0%, rgba(0,0,0,0) 100%);
  136.         background: -webkit-linear-gradient(left, rgba(0,0,0,0.8) 0%,rgba(0,0,0,0) 100%);
  137.         background: -o-linear-gradient(left, rgba(0,0,0,0.8) 0%,rgba(0,0,0,0) 100%);
  138.         background: -ms-linear-gradient(left, rgba(0,0,0,0.8) 0%,rgba(0,0,0,0) 100%);
  139.         background: linear-gradient(left, rgba(0,0,0,0.8) 0%,rgba(0,0,0,0) 100%);
  140. }
  141. .view .s5 > .overlay {
  142.         background: -moz-linear-gradient(left, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0) 100%);
  143.         background: -webkit-linear-gradient(left, rgba(0,0,0,0.3) 0%,rgba(0,0,0,0) 100%);
  144.         background: -o-linear-gradient(left, rgba(0,0,0,0.3) 0%,rgba(0,0,0,0) 100%);
  145.         background: -ms-linear-gradient(left, rgba(0,0,0,0.3) 0%,rgba(0,0,0,0) 100%);
  146.         background: linear-gradient(left, rgba(0,0,0,0.3) 0%,rgba(0,0,0,0) 100%);
  147. }
  148. </style>
  149. </head>
  150. <body  style="background:url(data/attachment/album/201410/21/205405b2dzgtxt2stq27d7.jpg);min-width:1000px">
  151. <header class="header"></header>
  152. <div id="grid">
  153.     <div class="view">
  154.         <div class="view-back">
  155.             <span style="font:32px 'Microsoft YaHei';position:absolute;right:20px;text-shadow: 2px 2px .3em rgba(255, 255, 255, .6);color:#FFF">HIT FM</span>
  156.             <span style="font:18px 'Microsoft YaHei';position:absolute;right:5px;top:50px;text-shadow: 1px 1px .3em rgba(0, 0, 0, .8);">NEVER <br/>STOP THE BEAT!</span>
  157.         </div>
  158.         <img src="data/attachment/album/201410/21/205406wfuvvuowv1yhdvdv.jpg" /> 
  159.     </div>
  160. <script type="text/javascript">
  161. //jquery外掛程式碼                 
  162. $.fn.hoverfold = function( args ) {
  163.     this.each( function() {
  164.         $( this ).children( '.view' ).each( function() {
  165.             var $item   = $( this ),
  166.                 img     = $item.children( 'img' ).attr( 'src' ),
  167.                 struct  = '<div class="slice s1">';
  168.                     struct  +='<div class="slice s2">';
  169.                         struct  +='<div class="slice s3">';
  170.                             struct  +='<div class="slice s4">';
  171.                                 struct  +='<div class="slice s5">';
  172.                                 struct  +='</div>';
  173.                             struct  +='</div>';
  174.                         struct  +='</div>';
  175.                     struct  +='</div>';
  176.                 struct  +='</div>';
  177.             var $struct = $( struct );
  178.             $item.find( 'img' ).remove().end().append( $struct ).find( 'div.slice' ).css( 'background-image', 'url(' + img + ')' ).prepend( $( '<span class="overlay" ></span>' ) );
  179.           });
  180.        });
  181.     };
  182. //呼叫外掛
  183. $( '#grid' ).hoverfold();
  184. </script>
  185. </div>
  186. </body>
  187. </html>