基于jquery的3d效果实现代码


Posted in Javascript onMarch 23, 2011
<html> 
<head> 
<script type="text/javascript" src="http://demo.3water.com/jslib/jquery/jquery-1.4.2.min.js"></script><style> 
.s_c{ 
position:relative; 
width:800px; 
height:300px; 
border:1px solid #000; 
overflow:hidden; 
} 
</style> 
<script type="text/javascript"> 
var Sl=Sl||{}; 
(function(){ 
var undefined; 
function box(o){ 
var d=$.extend({},{ 
'w':40, 
'h':30, 
'secne':null, 
'data':'' 
},o); 
if(d.secne==null||!d.secne) return; 
this.div=$("<div></div>").css({'position': 'absolute','border':'1px solid #cde','left':'0px','top':'0px'}).html(d.data); 
this.w(d.w); 
this.h(d.h); 
this._ow=this.w(); 
this._oh=this.h(); 
$(d.secne).append(this.div); 
return this; 
} 
box.prototype.zoomw=function(v){ 
if(v==undefined){ 
this._zw=this._zw||1; 
return this._zw; 
} 
this.w(this.ow()*v,false); 
this._zw=v; 
return this; 
} 
box.prototype.zoomh=function(v){ 
if(v==undefined){ 
this._zh==this._zh||1; 
return this._zh; 
} 
this.h(this.oh()*v,false); 
this._zh=v; 
return this; 
} 
box.prototype.x=function(x){ 
if(x==undefined){ 
this._x=this._x||0; 
return this._x; 
} 
this.div.css({ 
left:x-(this.w())/2 
}) 
this._x=x; 
return this; 
} 
box.prototype.y=function(y){ 
if(y==undefined){ 
this._y=this._y||0; 
return this._y; 
} 
this.div.css({ 
top:y-(this.h())/2 
}) 
this._y=y; 
return this; 
} 
box.prototype.ow=function(){ 
return this._ow||0; 
} 
box.prototype.oh=function(){ 
return this._oh||0; 
} 
box.prototype.w=function(w,rs){ 
if(w==undefined){ 
w=this.div.css('width'); 
w=w=='auto'?this.div.width():w; 
return parseInt(w); 
} 
if(rs!==false) 
this._ow=w; 
this.mx(-(w-this.w())/2); 
this.div.css({'width':w}); 
return this; 
} 
box.prototype.h=function(h,rs){ 
if(h==undefined){ 
h=this.div.css('height'); 
h=h=='auto'?this.div.height():h; 
return parseInt(h); 
} 
if(rs!==false) 
this._oh=h; 
this.my(-(h-this.h())/2); 
this.div.css({'height':h}); 
return this; 
} 
box.prototype.mx=function(x){ 
var div=this.div; 
div.css({'left': parseInt(div.css('left'))+x}); 
} 
box.prototype.my=function(y){ 
var div=this.div; 
div.css({'top':parseInt(div.css('top'))+y}); 
} 
box.prototype.z=function(z){ 
if(z==undefined) 
return this.div.css('z-index'); 
this.div.css("z-index",z); 
return this; 
} 
//3d box 
function box3d(o){ 
if(!$(o.secne))return; 
secne=o.secne; 
var secne=this.secne=$(secne); 
this.vx= parseInt(secne.css('width')=='auto'?secne.width():secne.css('width'))/2; 
this.vy= parseInt(secne.css('height')=='auto'?secne.height():secne.css('height'))/2; 
this.fl=o.fl||250; 
this.box=new box(o).z(0); 
this._set3d(); 
return this; 
} 
box3d.prototype.x=function(x){ 
if(x==undefined) 
return this._x||0; 
this._x=x||0; 
this._set3d(); 
return this; 
} 
box3d.prototype.y=function(y){ 
if(y==undefined) 
return this._y||0; 
this._y=y||0; 
this._set3d(); 
return this; 
} 
box3d.prototype.z=function(z){ 
if(z==undefined) 
return this._z||0; 
this._z=z||0; 
this._set3d(); 
return this; 
} 
box3d.prototype._set3d=function(){ 
var boxo=this.box; 
var fl=this.fl; 
var zomx=fl/(fl+this.z()); 
var x=this.vx+zomx*this.x(); 
var y=this.vy+zomx*this.y(); 
boxo.zoomw(zomx).zoomh(zomx); 
boxo.x(x); 
boxo.y(y); 
} 

// 
function slide(o){ 
var d=$.extend({},{ 
'z':100, 
'secne':null 
},o); 
if(d.secne==null||!$(d.secne))return; 
this.cecne=d.secne; 
this.z=d.z; 
this.sleep=0.02; 
var k={'secne':d.secne,'w':80,'h':50,fl:1000}; 
this.box=[]; 
var c=8; 
for(var i=0;i<c;i++){ 
var t= parseInt(i-c/2); 
var a=$('<div></div>').css({ 
width:'100%', 
height:'100%', 
background:'#777' 
}) 
k.data=a; 
var ubox=new box3d(k).z(300); 
this.r(ubox,t/2); 
this.box.push(ubox); 
} 
this.re_index(); 
} 
slide.prototype._moveaction=function(){ 
var _this=this; 
var s=this.sleep; 
for(var i=0;i<this.box.length;i++){ 
this.r(this.box[i],s); 
} 
this.re_index(); 
} 
slide.prototype.r=function(rbox,s){ 
if(!(rbox instanceof box3d)) 
return; 
var X=rbox.x(); 
var Y=rbox.y() 
var Z=rbox.z(); 
var New_X = X*Math.cos(s) - Z*Math.sin(s); 
var New_Z = X*Math.sin(s) + Z*Math.cos(s); 
var New_Y = Y; 
rbox.x(New_X); 
rbox.y(New_Y); 
rbox.z(New_Z); 
} 
slide.prototype.re_index=function(){ 
var arr=this.box; 
var s=arr.length; 
for(var j=0;j<s-1;j++){ 
for(var i=0;i<s-j-1;i++){ 
if(arr[i].z()<arr[i+1].z()){ 
var c=arr[i]; 
arr[i]=arr[i+1]; 
arr[i+1]=c; 
} 
} 
} 
for(var i=0;i<arr.length;i++){ 
arr[i].box.z(i) 
} 
} 
slide.prototype.start=function(){ 
this.run=1; 
this._move(); 
} 
slide.prototype.stop=function(){ 
this.run=0; 
} 
slide.prototype._move=function(s){ 
var _this=this; 
setTimeout(function(){ 
if(!_this.run){ 
return; 
} 
_this._moveaction(); 
_this._move(); 
},50) 
} 
Sl.slide=slide; 
})() 
$(function(){ 
var s=new Sl.slide({secne:'.s_c'}); 
$('#start').click(function(){s.start()}) 
$('#stop').click(function(){s.stop()}) 
}) 
</script> 
</head> 
<body> 
<div class='s_c'></div> 
<button id="start">start</button> 
<button id="stop">stop</button> 
</body> 
</html>
Javascript 相关文章推荐
js实现缓冲运动效果的方法
Apr 10 Javascript
jQuery插件制作之参数用法实例分析
Jun 01 Javascript
jQuery中的AjaxSubmit使用讲解
Sep 25 Javascript
javascript匀速动画和缓冲动画详解
Oct 20 Javascript
Angular.Js的自动化测试详解
Dec 09 Javascript
详解vue.js2.0父组件点击触发子组件方法
May 10 Javascript
ES6中javascript实现函数绑定及类的事件绑定功能详解
Nov 08 Javascript
vue + vuex todolist的实现示例代码
Mar 09 Javascript
基于Vue 服务端Cookies删除的问题
Sep 21 Javascript
详解Vue 动态组件与全局事件绑定总结
Nov 11 Javascript
JavaScript基于遍历操作实现对象深拷贝功能示例
Mar 05 Javascript
node.js使用express-fileupload中间件实现文件上传
Jul 16 Javascript
jquery 操作表格实现代码(多种操作打包)
Mar 20 #Javascript
jQuery实现的Email中的收件人效果(按del键删除)
Mar 20 #Javascript
jquery图片上下tab切换效果
Mar 18 #Javascript
javascript一些实用技巧小结
Mar 18 #Javascript
jquery获取下拉列表的值为null的解决方法
Mar 18 #Javascript
iframe的onload在Chrome/Opera中执行两次Bug的解决方法
Mar 17 #Javascript
javascript定时保存表单数据的代码
Mar 17 #Javascript
You might like
PHP连接SQLServer2005 的问题解决方法
2010/07/19 PHP
php/js获取客户端mac地址的实现代码
2013/07/08 PHP
php获取用户浏览器版本的方法
2015/01/03 PHP
基于php实现的php代码加密解密类完整实例
2016/10/12 PHP
Laravel 的数据库迁移的方法
2017/07/31 PHP
JavaScript之Getters和Setters 平台支持等详细介绍
2012/12/07 Javascript
js创建对象的区别示例介绍
2014/07/24 Javascript
node.js中的console.assert方法使用说明
2014/12/10 Javascript
node.js中的fs.readdir方法使用说明
2014/12/17 Javascript
jquery+css实现的红色线条横向二级菜单效果
2015/08/22 Javascript
js clearInterval()方法的定义和用法
2015/11/11 Javascript
详解AngularJS Filter(过滤器)用法
2015/12/28 Javascript
js编写选项卡效果
2017/05/23 Javascript
jQuery Json数据格式排版高亮插件json-viewer.js使用方法详解
2017/06/12 jQuery
关于js对textarea换行符的处理方法浅析
2018/08/03 Javascript
微信小程序如何访问公众号文章
2019/07/08 Javascript
IntelliJ IDEA编辑器配置vue高亮显示
2019/09/26 Javascript
vue中的mescroll搜索运用及各种填坑处理
2019/10/30 Javascript
vue设置动态请求地址的例子
2019/11/01 Javascript
jQuery 移除事件的方法
2020/06/20 jQuery
element-ui tree结构实现增删改自定义功能代码
2020/08/31 Javascript
[01:32]寻找你心中的那团火 DOTA2 TI9火焰传递活动今日开启
2019/05/16 DOTA
Python中用max()方法求最大值的介绍
2015/05/15 Python
Python中字符串格式化str.format的详细介绍
2017/02/17 Python
Python使用add_subplot与subplot画子图操作示例
2018/06/01 Python
完美解决Python 2.7不能正常使用pip install的问题
2018/06/12 Python
教你如何编写、保存与运行Python程序的方法
2019/07/12 Python
Python使用configparser读取ini配置文件
2020/05/25 Python
scrapy处理python爬虫调度详解
2020/11/23 Python
大学生自我鉴定书
2014/03/24 职场文书
实习协议书范本
2014/04/22 职场文书
餐饮食品安全责任书
2015/01/29 职场文书
中学生运动会广播稿
2015/08/19 职场文书
《三国志》赏析
2019/08/27 职场文书
SpringBoot集成Druid连接池连接MySQL8.0.11
2021/07/02 Java/Android
Spring Boot优化后启动速度快到飞起技巧示例
2022/07/23 Java/Android