今天做需求碰到一个类似3D效果图一样的 如下图所示:
这种效果 原理就是用定位 position:absolute;z-index:XX;由于这种效果以前有个同事用jquery做过这样的 所以今天看了他的代码 仿照下做了一个,用Jquery animate这个函数可以做 通过移动图片 不断的给他们定义样式及定位就可以了!但是有个问题要注意下 : 第一代码很撮 代码不能重用,只能实现效果!因为我今天做的需求有两个这样的 所以要重复的写不同的代码,第二:由于这是jquery,那当然要引入Jquery压缩包,很奇怪的是 引入线上的Google API Jquery压缩包 在IE下 点击右箭头时会报错 貌似说jquery压缩包里面 参数无效!在火狐和谷歌游览器都是正常的!所以之后引入了Jquery1.7压缩包就可以正常运行了 没有错误了 可能是jquery新版本增加了一些属性吧!第三要慢慢的理解顺时针旋转和逆时针旋转,可能在理解的过程中 某某同学会晕头啊,我本人也曾经晕过头 呵呵!做不到那种效果!只有注意精神理解下就可以了!下面是HTML代码!
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <style>
- body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td{ margin:0; padding:0; font-size:12px; font-family:"宋体";}
- table{ border-collapse:collapse; border-spacing:0;}
- fieldset,img{ border:0; display:block;}
- address,caption,cite,code,dfn,em,strong,th,var{ font-style:normal; font-weight:normal;}
- ol,ul{ list-style:none;}
- caption,th{ text-align:left;}
- h1,h2,h3,h4,h5,h6{ font-size:100%; font-weight:normal;}
- .fl{ float:left;}
- .fr{ float:right;}
- .marquee1{ width:402px; height:158px; overflow:hidden;}
- .J_btn{width:21px; height:23px; overflow:hidden; margin-top:70px;}
- .prev{ background:url(images/prev.jpg) no-repeat; cursor:pointer;}
- .M_scroll{ position:relative; float:left; margin:0 8px 0 16px; display:inline; width:335px; height:158px;}
- .M_scroll ul{width:335px; height:158px;}
- .next{ background:url(images/next.jpg) no-repeat; cursor:pointer;}
- .num1,.num3{ background:url(images/wbg2.jpg) no-repeat; width:109px; height:109px; overflow:hidden;}
- .num1{ position:absolute; top:25px; left:0; z-index:999;}
- .num1 .conpic{ margin:4px;width:150px; height:101px; overflow:hidden;}
- .num3 .conpic{margin:4px;width:100px; height:101px; overflow:hidden;}
- .num3{ position:absolute; top:25px; left:228px; z-index:1000;}
- .num2{ position:absolute; top:0; left:88px; z-index:1001; background:url(images/wbg.jpg) no-repeat; width:158px; height:158px; overflow:hidden;}
- .num2 .conpic{ margin:4px 4px; width:150px; height:150px; overflow:hidden;}
- </style>
- <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
- </head>
- <body>
- <div class="marquee1 fl">
- <div class="prev J_btn fl"></div>
- <div class="M_scroll">
- <ul>
- <li class="num1">
- <div class="conpic">
- <a href="#" target="_blank"><img src="http://img03.taobaocdn.com/tps/i3/T1xSyJXcBhXXXXXXXX-539-314.jpg" width="150" height="150"/></a>
- </div>
- </li>
- <li class="num2">
- <div class="conpic">
- <a href="#" target="_blank"><img src="images/photo.jpg" width="150" height="150"/></a>
- </div>
- </li>
- <li class="num3">
- <div class="conpic">
- <a href="#" target="_blank"><img src="http://img02.taobaocdn.com/tps/i2/T1H0qHXihkXXXXXXXX-539-314.png" width="150" height="150"/></a>
- </div>
- </li>
- </ul>
- </div>
- <div class="next J_btn fl"></div>
- </div>
- <script type="text/javascript" src="3D.js"></script>
- </body>
- </html>
Javascript代码:
- $(function(){
- var t = 400;
- $(".prev").click(function(){
- if($(".M_scroll li:eq(1)").css("left")=="88px"){
- centerFunction(2);
- rightFunction(3);
- leftFunction(1);
- };
- if($(".M_scroll li:eq(2)").css("left")=="88px"){
- centerFunction(3);
- rightFunction(1);
- leftFunction(2);
- };
- if($(".M_scroll li:eq(0)").css("left")=="88px"){
- centerFunction(1);
- rightFunction(2);
- leftFunction(3);
- };
- });
- $(".next").click(function(){
- if($(".M_scroll li:eq(1)").css("left")=="88px"){
- nextCenter(2);
- nextRight(3);
- nextLeft(1);
- };
- if($(".M_scroll li:eq(2)").css("left")=="88px"){
- nextCenter(3);
- nextRight(1);
- nextLeft(2);
- };
- if($(".M_scroll li:eq(0)").css("left")=="88px"){
- nextCenter(1);
- nextRight(2);
- nextLeft(3);
- };
- })
- /** 顺时针旋转 */
- function centerFunction(num){
- $(".num" +num).animate({"width":"109px","height":"109px","left":"0px","top" : "25px","z-index": "999"},t,function(){
- $(this).css("background-image","url(images/wbg2.jpg)");
- }).children(".conpic").animate({"width":"101px","height":"101px"},t,function(){
- $(this).children("img").css({"width":"150px","height":"101px"});
- });
- }
- function leftFunction(num){
- $(".num"+num).animate({"width":"109px","height":"109px","left":"228px","top":"25px","z-index":"1000"},t,function(){
- $(this).css("background-image","url(images/wbg2.jpg)");
- }).children(".conpic").animate({"width":"100px","height":"101px"},t,function(){
- $(this).children("img").css({"width":"100px","height":"101px"});
- });
- }
- function rightFunction(num){
- $(".num"+num).animate({"width":"158px","height":"158px","left":"88px","top":"0px","z-index":"1001"},t,function(){
- $(this).css("background-image","url(images/wbg.jpg)");
- }).children(".conpic").animate({"width":"150px","height":"150px"},t,function(){
- $(this).children("img").css({"width":"150px","height":"150px"});
- });
- }
- /*逆时针旋转 */
- function nextCenter(num){
- $(".num"+num).animate({"width":"109px","height":"109px","left":"228px","top":"25px","z-index":"999"},t,function(){
- $(this).css("background-image","url(images/wbg2.jpg)");
- }).children(".conpic").animate({"width":"100px","height":"101px"},t,function(){
- $(this).children("img").css({"width":"100px","height":"101px"});
- });
- }
- function nextRight(num){
- $(".num"+num).animate({"width":"109px","height":"109px","left":"0px","top":"25px","z-index":"1000"},t,function(){
- $(this).css("background-image","url(images/wbg2.jpg)");
- }).children(".conpic").animate({"width":"100px;","height":"101px"},t,function(){
- $(this).children("img").css({"width":"100px","height":"101px"});
- });
- }
- function nextLeft(num){
- $(".num" +num).animate({"width":"158px","height":"158px","left":"88px","top" : "0px","z-index": "1001"},t,function(){
- $(this).css("background-image","url(images/wbg.jpg)");
- }).children(".conpic").animate({"width":"150px","height":"150px"},t,function(){
- $(this).children("img").css({"width":"150px","height":"150px"});
- });
- }
- });
下面有附件下载 可以看看!
本文转自 涂根华 51CTO博客,原文链接:http://blog.51cto.com/tugenhua/762839,如需转载请自行联系原作者