首页 > 语言 > JavaScript > 正文

javascript实现的图片切割多块效果实例

2024-05-06 16:19:34
字体:
来源:转载
供稿:网友

这篇文章主要介绍了javascript实现的图片切割多块效果,涉及javascript操作图片及css样式的技巧,需要的朋友可以参考下

本文实例讲述了javascript实现的图片切割多块效果。分享给大家供大家参考。具体实现方法如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru"
  4. <head> 
  5. <title></title> 
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  7. <style type="text/css"
  8. .line{ 
  9. display:none; 
  10. z-index:1; 
  11. left:0; 
  12. top:0; 
  13. position:absolute; 
  14. #line1{ 
  15. display:block; 
  16. .container{ 
  17. position:relative; 
  18. width:564px; 
  19. height:294px; 
  20. overflow:hidden; 
  21. .border{ 
  22. border:5px solid #000; 
  23. .corner{ 
  24. position:absolute; 
  25. width:282px; 
  26. height:147px; 
  27. background:#ccc; 
  28. overflow:hidden; 
  29. .leftTop,.inLeftTop{ 
  30. position:absolute; 
  31. left:0; 
  32. top:0; 
  33. right:auto; 
  34. bottom:auto; 
  35. .rightTop,.inRightTop{ 
  36. position:absolute; 
  37. right:0; 
  38. top:0; 
  39. left:auto; 
  40. bottom:auto; 
  41. .rightBottom,.inRightBottom{ 
  42. position:absolute; 
  43. right:0; 
  44. bottom:0; 
  45. top:auto; 
  46. left:auto; 
  47. .leftBottom,.inLeftBottom{ 
  48. position:absolute; 
  49. left:0; 
  50. bottom:0; 
  51. top:auto; 
  52. right:auto; 
  53. </style> 
  54. </head> 
  55. <body> 
  56. <script src="jquery-1.6.2.min.js" type="text/javascript"></script> 
  57. <script type="text/javascript"
  58. function crossLine(container,option,callback){ 
  59. var lineX=$("<div style='width:2000px;height:4px;overflow:hidden;position:absolute;background:#FACC41;left:0;top:0;z-index:1'></div>"); 
  60. var lineY=$("<div style='width:4px;height:2000px;overflow:hidden;position:absolute;background:#FACC41;left:0;top:0;z-index:1'></div>"); 
  61. var _option={ 
  62. "display":"none"
  63. "targetPosX":container.width()/2-2, 
  64. "targetPosY":container.height()/2-2, 
  65. "time":500, 
  66. "freq":10 
  67. }; 
  68. $.extend(_option,option); 
  69. option=_option; 
  70. var targetPosX=option.targetPosX; 
  71. var targetPosY=option.targetPosY; 
  72. var time=option.time; 
  73. var freq=option.freq; 
  74. times=time/freq; 
  75. container.append(lineX).append(lineY); 
  76. //开始运动 
  77. var lxt=lineX.position().top; 
  78. var lyl=lineY.position().left; 
  79. xPerTime=targetPosX/times; 
  80. yPerTime=targetPosY/times; 
  81. var count=0; 
  82. var si=setInterval(function(){ 
  83. count++; 
  84. if(count>=times){ 
  85. clearInterval(si); 
  86. if(typeof(callback)=="function"){ 
  87. callback(); 
  88. if(option.display=="none"){ 
  89. lineX.remove(); 
  90. lineY.remove(); 
  91. if(lxt+yPerTime<=targetPosY){ 
  92. lxt += yPerTime; 
  93. lineX.css("top",lxt); 
  94. }else
  95. lxt=targetPosY; 
  96. lineX.css("top",targetPosY); 
  97. if(lyl+xPerTime<=targetPosX){ 
  98. lyl += xPerTime; 
  99. lineY.css("left",lyl); 
  100. }else
  101. lyl=targetPosX; 
  102. lineY.css("left",targetPosX); 
  103. },freq); 
  104. function picSplit(line1,line2,container,option){ 
  105. //begin 
  106. line1.css("z-index",2); 
  107. /* 
  108. var targetX=282; 
  109. var targetY=147; 
  110. */ 
  111. var _option={ 
  112. "targetX":container.width()/2, 
  113. "targetY":container.height()/2, 
  114. "time":500, 
  115. "freq":10 
  116. }; 
  117. $.extend(_option,option); 
  118. option=_option; 
  119. var targetX=option.targetX; 
  120. var targetY=option.targetY; 
  121. var containerWidth=container.width(); 
  122. var containerHeight=container.height(); 
  123. /* 
  124. *复制四个,放入四个容器,置于四角,然后移动 
  125. */ 
  126. //div0-4 容器,放置于四角 
  127. var div0=$("<div></div>").css({"position":"absolute","left":0,"top":0,"right":"auto","bottom":"auto","width":targetX,"height":targetY,"z-index":"2","overflow":"hidden"}).appendTo(container); 
  128. var div1=$("<div></div>").css({"position":"absolute","left":targetX,"top":0,"right":"auto","bottom":"auto","width":containerWidth-targetX,"height":targetY,"z-index":"2","overflow":"hidden"}).appendTo(container); 
  129. var div2=$("<div></div>").css({"position":"absolute","left":targetX,"top":targetY,"right":"auto","bottom":"auto","width":targetX,"height":containerHeight-targetY,"z-index":"2","overflow":"hidden"}).appendTo(container); 
  130. var div3=$("<div></div>").css({"position":"absolute","left":0,"top":targetY,"right":"auto","bottom":"auto","width":targetX,"height":containerHeight-targetY,"z-index":"2","overflow":"hidden"}).appendTo(container); 
  131. //tempL0-4复制出来的层 
  132. var tempL0=line1.clone().css({"position":"absolute","left":0,"top":0,"right":"auto","bottom":"auto","z-index":"2"}).appendTo(div0); 
  133. var tempL1=line1.clone().css({"position":"absolute","left":-targetX,"top":0,"right":"auto","bottom":"auto","z-index":"2"}).appendTo(div1); 
  134. var tempL2=line1.clone().css({"position":"absolute","left":-targetX,"top":-targetY,"right":"auto","bottom":"auto","z-index":"2"}).appendTo(div2); 
  135. var tempL3=line1.clone().css({"position":"absolute","left":0,"top":-targetY,"right":"auto","bottom":"auto","z-index":"2"}).appendTo(div3); 
  136. line1.css("display","none"); 
  137. line2.css("display","block"); 
  138. //开始运动 
  139. var time=option.time; 
  140. var freq=option.freq; 
  141. var times=time/freq; 
  142. var count=0; 
  143. var xLeftPerTime=3; 
  144. var xRightPerTime=3; 
  145. var yTopPerTime=3; 
  146. var yBottomPerTime=3; 
  147. var l0=div0.position().left; 
  148. var t0=div0.position().top; 
  149. var l1=div1.position().left; 
  150. var t1=div1.position().top; 
  151. var l2=div2.position().left; 
  152. var t2=div2.position().top; 
  153. var l3=div3.position().left; 
  154. var t3=div3.position().top; 
  155. var si=setInterval(function(){ 
  156. count++; 
  157. if(count>=times){ 
  158. clearInterval(si); 
  159. div0.remove(); 
  160. div1.remove(); 
  161. div2.remove(); 
  162. div3.remove(); 
  163. l0=l0-xLeftPerTime; 
  164. t0=t0-yTopPerTime; 
  165. l1=l1+xRightPerTime; 
  166. t1=t1-yTopPerTime; 
  167. l2=l2+xRightPerTime; 
  168. t2=t2+yBottomPerTime; 
  169. l3=l3-xLeftPerTime; 
  170. t3=t3+yBottomPerTime; 
  171. div0.css("left",(l0-xLeftPerTime)+"px"); 
  172. div0.css("top",(t0-yTopPerTime)+"px"); 
  173. div1.css("left",(l1+xRightPerTime)+"px"); 
  174. div1.css("top",(t1-yTopPerTime)+"px"); 
  175. div2.css("left",(l2+xRightPerTime)+"px"); 
  176. div2.css("top",(t2+yBottomPerTime)+"px"); 
  177. div3.css("left",(l3-xLeftPerTime)+"px"); 
  178. div3.css("top",(t3+yBottomPerTime)+"px"); 
  179. },freq); 
  180. </script> 
  181. <div class="container" id="container"
  182. <div class="line" id="line1"><img src="http://static.house.sina.com.cn/apileju/cms/110819/1820432103.jpg" alt="" /></div> 
  183. <div class="line" id="line2"><img src="http://static.house.sina.com.cn/apileju/cms/110819/1110183294.jpg" alt="" /></div> 
  184. </div> 
  185. <input type="button" value="GO" onclick="javascript:go()" /> 
  186. <script type="text/javascript"
  187. var line1=$("#line1"); 
  188. var line2=$("#line2"); 
  189. line1.css("display","block"); 
  190. var container=$("#container"); 
  191. function go(){ 
  192. var option={"display":"none"}; 
  193. crossLine(container,option,gopicSplit); 
  194. var container=$("#container"); 
  195. function gopicSplit(){ 
  196. picSplit(line1,line2,container); 
  197. </script> 
  198. </body> 
  199. </html> 


希望本文所述对大家的javascript程序设计有所帮助。

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

图片精选