免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2233 | 回复: 0
打印 上一主题 下一主题

jQuery Div拖动+键盘控制综合效果 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-20 14:16 |只看该作者 |倒序浏览
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>jQuery Div拖动+键盘控制综合效果</title>
  5. <meta http-equiv="content-type" content="text/html;charset=gb2312">
  6. <script src="/ajaxjs/jquery1.3.2.js"></script>
  7. </head>
  8. <body>
  9. 预览时看不到效果,刷新一下即可
  10. <div id="text" style="width:120px;height:60px;background:#F06;position:absolute;left:500px;top:90px;cursor:move;"></div>
  11. <script type="text/javascript">
  12. var msgObj = document.getElementById('text');
  13. var posx = 0;
  14. var posy = 0;
  15. var moveable = false;
  16. var MouseDownEvent = document.onmousedown;
  17. var MouseMoveEvent = document.onmousemove;
  18. var MouseUpEvent = document.onmouseup;
  19. msgObj.onmousedown = function(evt) {
  20.         var evt = evt||window.event;
  21.         moveable = true;
  22.         posy = evt.clientY-parseInt(msgObj.style.top);
  23.         posx = evt.clientX-parseInt(msgObj.style.left);
  24.         document.onmousemove = function(evt) {
  25.                 if (moveable) {
  26.                         var evt = evt||window.event;
  27.                         msgObj.style.left = evt.clientX - posx + "px";
  28.                         msgObj.style.top = evt.clientY - posy + "px";
  29.                 }
  30.                 change();
  31.         };
  32.         document.onmouseup = function () {
  33.                 if (moveable) {
  34.                         document.onmousemove = MouseMoveEvent;
  35.                         document.onmouseup = MouseUpEvent;
  36.                         moveable = false;
  37.                         posx = 0;
  38.                         posy = 0;
  39.                 }
  40.         };
  41. }
  42. var v=1;
  43. var a=0.9;
  44. var h=document.documentElement.clientHeight;
  45. function scroll(){
  46.         var timer=setInterval(function(){
  47.                 v +=a;
  48.                 var top=(parseInt(msgObj.style.top)||0);
  49.                 if(top+v>h-70){
  50.                         if(v<2)clearInterval(timer);
  51.                         msgObj.style.top=h-70+"px";
  52.                         v=-v*0.5;       
  53.                 }else{
  54.                         msgObj.style.top=top+v+"px";
  55.                 }
  56.                 change();
  57.         },10);
  58. };
  59. $(document).keyup(function(e){
  60.         var e = e || window.event ;
  61.         if(e.which == 13){
  62.                 scroll();       
  63.         }
  64. })
  65. var x = 4 ;
  66. var y = 4 ;
  67. function newpro(){
  68.         var Height = document.documentElement.clientHeight ;
  69.         var Width = document.documentElement.clientWidth ;
  70.         var newobj = document.createElement('div');
  71.         newobj.setAttribute('id','bigbox');
  72.         newobj.style.position = 'absolute';
  73.         newobj.style.left = 20 +"px";
  74.         newobj.style.top = 80 +"px";
  75.         newobj.style.height = Height/x +"px" ;
  76.         newobj.style.width = Width/y +"px" ;
  77.         newobj.style.borderWidth = 1+"px";
  78.         newobj.style.borderColor = "#ff6500";
  79.         newobj.style.borderStyle = 'solid' ;
  80.         newobj.innerHTML='<div id="minbox"></div>';
  81.         document.body.appendChild(newobj);
  82.         change();
  83. }
  84. function change(){
  85.         var minobj = document.getElementById('minbox');
  86.         minobj.style.position = 'absolute';
  87.         minobj.style.left = parseInt(msgObj.style.left)/x +"px";
  88.         minobj.style.top = parseInt(msgObj.style.top)/y +"px";
  89.         minobj.style.height = 20 +"px" ;
  90.         minobj.style.width = 30 +"px" ;
  91.         minobj.style.background = "#F06";
  92. }
  93. window.onload = function(){
  94.         newpro();
  95. }
  96. </script>
  97. </body>
  98. </html>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP