首页 > 语言 > JavaScript > 正文

JS简单实现动画弹出层效果

2024-05-06 16:19:21
字体:大 中 小
来源:转载
供稿:网友

本文给大家介绍的是是一款javascript弹出层特效,支持点击触发js弹出层,滑过触发js弹出层,带动画效果js弹出层,可自定义函数回调js弹出层。

JS简单实现动画弹出层效果

 

 
  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>动画弹出层</title> 
  5. <style> 
  6. .list{ 
  7. position:relative;; 
  8. background:#eee; 
  9. border:1px #ccc solid; 
  10. margin:10px; 
  11. height:30px; 
  12. width:100px; 
  13. cursor :pointer ; 
  14. } 
  15. .listShow{ 
  16. position:relative; 
  17. background:#eff; 
  18. border:1px #ddd solid; 
  19. margin:10px; 
  20. height:30px; 
  21. width:100px; 
  22. cursor :pointer ; 
  23. } 
  24. .comment{ 
  25. position:absolute; 
  26. left:0; 
  27. display:none; 
  28. position:absolute; 
  29. border:1px #ccc solid; 
  30. background:#fee; 
  31. width:200px; 
  32. height:200px; 
  33. overflow:hidden; 
  34. z-index:100; 
  35. } 
  36. </style> 
  37. </head> 
  38. <body> 
  39. <div class="" id="show"> 
  40. 0 
  41. </div> 
  42. <div class="list" id="list1">1 
  43. <div class="comment" id="comment1">内容显示111<br/> 
  44. </div> 
  45. <div class="list" id="list2">2 
  46. <div class="comment" id="comment2">内容显示222</div> 
  47. </div> 
  48. <div class="list" id="list3">3 
  49. <div class="comment" id="comment3">内容显示333</div> 
  50. </div> 
  51. </body> 
  52. </html> 
  53. <script> 
  54. var zindex=0; 
  55. function $id(id){ 
  56. return document.getElementById(id); 
  57. } 
  58. var Bind = function(object,fun){ 
  59. var args = Array.prototype.slice.call(arguments).slice(2); 
  60. return function(){ 
  61. return fun.apply(object,args); 
  62. } 
  63. } 
  64. function addEventHandler(oTarget, sEventType, fnHandler){  
  65. if(oTarget.addEventListener){oTarget.addEventListener(sEventType, fnHandler, false);} 
  66. else if(oTarget.attachEvent){oTarget.attachEvent('on' + sEventType, fnHandler);} 
  67. else{oTarget['on' + sEventType] = fnHandler;} 
  68. } 
  69. var Shower=function(){ 
  70. this.list=null; 
  71. this.comment=null; 
  72. this.moveLeft=80;  
  73. this.moveTop=20; 
  74. this.height=150; 
  75. this.width=250; 
  76. this.time=800; 
  77. this.init=function(lisObj,comObj){ 
  78. this.list=lisObj; 
  79. this.comment=comObj; 
  80. var _this=this; 
  81. this._fnMove=Bind(this,this.move); 
  82. (function(){ 
  83. var obj=_this; 
  84. addEventHandler(obj.list,"click",obj._fnMove); 
  85. })(); 
  86. }; 
  87. this.move=function(){ 
  88. var _this=this; 
  89. var w=0;  
  90. var h=0;  
  91. var height=0; //弹出div的高 
  92. var width=0; //弹出div的宽 
  93. var t=0; 
  94. var startTime = new Date().getTime();//开始执行的时间 
  95. if(!_this.comment.style.display||_this.comment.style.display=="none"){ 
  96. _this.comment.style.display="block"; 
  97. _this.comment.style.height=0+"px"; 
  98. _this.comment.style.width=0+"px"; 
  99. _this.list.style.zIndex=++zindex; 
  100. _this.list.className="listShow"; 
  101. var comment=_this.comment.innerHTML;  
  102. _this.comment.innerHTML=""; //去掉显示内容 
  103. var timer=setInterval(function(){ 
  104. var newTime = new Date().getTime(); 
  105. var timestamp = newTime - startTime; 
  106. _this.comment.style.left=Math.ceil(w)+"px"; 
  107. _this.comment.style.top=Math.ceil(h)+"px"; 
  108. _this.comment.style.height=height+"px"; 
  109. _this.comment.style.width=width+"px"; 
  110. t++; 
  111. var change=(Math.pow((timestamp/_this.time-1), 3) +1); //根据运行时间得到基础变化量 
  112. w=_this.moveLeft*change; 
  113. h=_this.moveTop*change; 
  114. height=_this.height*change; 
  115. width=_this.width*change; 
  116. $id("show").innerHTML=w; 
  117. if(w>_this.moveLeft){ 
  118. clearInterval(timer); 
  119. _this.comment.style.left=_this.moveLeft+"px"; 
  120.  
  121. _this.comment.style.top=_this.moveTop+"px"; _this.comment.style.height=_this.height+"px"; 
  122.  
  123. _this.comment.style.width=_this.width+"px"; 
  124. _this.comment.innerHTML=comment; //回复显示内容 
  125. } 
  126. },1,_this.comment); 
  127. }else{ 
  128. _this.hidden(); 
  129. } 
  130. } 
  131. this.hidden=function(){ 
  132. var _this=this; 
  133. var flag=1; 
  134. var hiddenTimer=setInterval(function(){ 
  135. if(flag==1){ 
  136. _this.comment.style.height=parseInt(_this.comment.style.height)-10+"px"; 
  137. }else{ _this.comment.style.width=parseInt(_this.comment.style.width)-15+"px"; 
  138. _this.comment.style.left=parseInt(_this.comment.style.left)+5+"px"; 
  139. } 
  140. if(flag==1 && parseInt(_this.comment.style.height)<10){ 
  141. flag=-flag; 
  142. } 
  143. if(parseInt(_this.comment.style.width)<20){ 
  144. clearInterval(hiddenTimer); 
  145. _this.comment.style.left="0px"; 
  146. _this.comment.style.top="0px"; 
  147. _this.comment.style.height="0px"; 
  148. _this.comment.style.width="0px"; 
  149. _this.comment.style.display="none"; 
  150. if(_this.list.style.zIndex==zindex){ 
  151. zindex--; 
  152. }; 
  153. _this.list.style.zIndex=0; 
  154. _this.list.className="list"; 
  155. } 
  156. },1) 
  157. } 
  158. } 
  159. window.onload=function(){ 
  160. //建立各个菜单对象 
  161. var shower1=new Shower(); 
  162. shower1.init($id("list1"),$id("comment1")); 
  163. var shower2=new Shower(); 
  164. shower2.init($id("list2"),$id("comment2")); 
  165. var shower3=new Shower(); 
  166. shower3.init($id("list3"),$id("comment3")); 
  167. } 
  168. </script> 

以上所述就是本文的全部内容了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选