首页 > 语言 > JavaScript > 正文

jQuery动态效果显示人物结构关系图的方法

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

这篇文章主要介绍了jQuery动态效果显示人物结构关系图的方法,涉及jQuery操作json结构数据及鼠标事件的技巧,需要的朋友可以参考下

本文实例讲述了jQuery动态效果显示人物结构关系图的方法。分享给大家供大家参考。具体分析如下:

这是一个人物关系图,可动态展示,效果非常漂亮。点击文字可出现动态关系图的转换效果。

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  3. <html xmlns="http://www.w3.org/1999/xhtml"
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  6. <title>Untitled Document</title> 
  7. <style> 
  8. #box{ 
  9. width:500px; height:500px; 
  10. position: relative 
  11. .host{ 
  12. position:absolute; 
  13. width:100px; height:50px; 
  14. line-height:50px; text-align:center; 
  15. color:#000000; background-color:#eeeeee; 
  16. border:#000000 1px solid; font-weight:bolder 
  17. .guest{ 
  18. position:absolute; 
  19. width:80px; height:40px; 
  20. line-height:40px;text-align:center; 
  21. color: #999999; background-color:#FFFFFF; 
  22. border:#000000 1px solid; cursor:pointer 
  23. .relationship{ 
  24. position:absolute; 
  25. width:60px; height:20px; 
  26. color: #aaa; line-height:20px; 
  27. font-size:12px; text-align:center 
  28. </style> 
  29. <script src="jquery-1.6.2.min.js"></script> 
  30. <script> 
  31. var relationName = [ 
  32. {name:"成龙",friend:[ 
  33. {name:"房祖名",relationship:"父子"}, 
  34. {name:"林凤娇",relationship:"夫妻"}, 
  35. {name:"吴绮莉",relationship:"绯闻"}, 
  36. {name:"徐静蕾",relationship:"激吻"}] 
  37. }, 
  38. {name:"房祖名",friend:[ 
  39. {name:"成龙",relationship:"父子"}, 
  40. {name:"林凤娇",relationship:"母子"}, 
  41. {name:"方大同",relationship:"情敌"}, 
  42. {name:"薛凯琪",relationship:"女友"}, 
  43. {name:"陈坤",relationship:"朋友"}, 
  44. {name:"赵薇",relationship:"朋友"}] 
  45. }, 
  46. {name:"林凤娇",friend:[ 
  47. {name:"成龙",relationship:"夫妻"}, 
  48. {name:"房祖名",relationship:"母子"}, 
  49. {name:"吴绮莉",relationship:"情敌"}] 
  50. }, 
  51. {name:"吴绮莉",friend:[ 
  52. {name:"成龙",relationship:"绯闻"}, 
  53. {name:"林凤娇",relationship:"情敌"}, 
  54. {name:"吴卓林",relationship:"母女"}] 
  55. }, 
  56. {name:"徐静蕾",friend:[ 
  57. {name:"李亚鹏",relationship:"电影"}, 
  58. {name:"韩寒",relationship:"娱乐圈"}, 
  59. {name:"成龙",relationship:"激吻"}, 
  60. {name:"黄立行",relationship:"电影"}] 
  61. }, 
  62. {name:"方大同",friend:[ 
  63. {name:"房祖名",relationship:"情敌"}, 
  64. {name:"薛凯琪",relationship:"否认拍拖"}, 
  65. {name:"林宥嘉",relationship:"歌手"}, 
  66. {name:"韩庚",relationship:"演唱会"}] 
  67. }, 
  68. {name:"薛凯琪",friend:[ 
  69. {name:"方大同",relationship:"否认拍拖"}, 
  70. {name:"房祖名",relationship:"女友"}] 
  71. var relation = { 
  72. radius:150, 
  73. boxW:500, 
  74. boxH:500, 
  75. hostW:100, 
  76. hostH:50, 
  77. guestW:80, 
  78. guestH:40, 
  79. relationW:60, 
  80. relationH:20, 
  81. angle:0, 
  82. id:"box"
  83. init:function(array,n){//传入参数1:数组 参数2:第几个 
  84. this.array = array; 
  85. this.appendHost(this.array,n); 
  86. this.appendQuest(this.array,n); 
  87. this.appendRelationShip(this.array,n); 
  88. }, 
  89. appendHost:function(array,n){ 
  90. var box = $("#"+this.id); 
  91. var host ="<span class='host'>"+array[n].name+"</span>"
  92. box.append(host) 
  93. this.postHost(); 
  94. }, 
  95. postHost:function(){ 
  96. var x = (this.boxW - this.hostW)/2; 
  97. var y = (this.boxH - this.hostH)/2; 
  98. $(".host").css({ 
  99. left:x, 
  100. top:y 
  101. }) 
  102. }, 
  103. appendQuest:function(array,n){ 
  104. var box = $("#"+this.id); 
  105. var guests=""
  106. var that = this
  107. for(var i=0; i<array[n].friend.length; i++){ 
  108. guests+="<span class='guest'>"+array[n].friend[i].name+"</span>"
  109. $(guests).appendTo(box); 
  110. $(".guest").live("click",function(){ 
  111. that.move(that,this); 
  112. }) 
  113. this.postQuest(); 
  114. }, 
  115. postQuest:function(){ 
  116. var guests = $(".guest"); 
  117. var that = this
  118. guests.each(function(i){ 
  119. guests.eq(i).css({ 
  120. left:that.setQuestPose(guests.length,that.radius,i,that.guestW,that.guestH,that.angle).left, 
  121. top:that.setQuestPose(guests.length,that.radius,i,that.guestW,that.guestH,that.angle).top 
  122. }).attr("angle",i/guests.length) 
  123. }) 
  124. }, 
  125. setQuestPose:function(n,r,i,w,h,d){ 
  126. //n:代表共几个对象 r代表周长 i代表第几个对象  
  127. //w代表外面对象的宽带 h代表外面对象的高度 d代表其实角度 
  128. var p = i/n*Math.PI*2+Math.PI*2*d; 
  129. var x = r * Math.cos(p); 
  130. var y = r * Math.sin(p); 
  131. return { 
  132. "left":parseInt(this.boxW/2+ x - w/2), 
  133. "top":parseInt(this.boxH/2 + y - h/2) 
  134. }, 
  135. appendRelationShip:function(array,n){ 
  136. var box = $("#"+this.id); 
  137. var relation=""
  138. for(var i=0; i<array[n].friend.length; i++){ 
  139. relation+="<span class='relationship'>"+array[n].friend[i].relationship+"</span>"
  140. box.append(relation); 
  141. this.postRelationShip(); 
  142. }, 
  143. postRelationShip:function(){ 
  144. var guests = $(".relationship"); 
  145. var that = this
  146. guests.each(function(i){ 
  147. guests.eq(i).css({ 
  148. left:that.setQuestPose(guests.length,that.radius/2,i,that.relationW,that.relationH,that.angle).left, 
  149. top:that.setQuestPose(guests.length,that.radius/2,i,that.relationW,that.relationH,that.angle).top 
  150. }) 
  151. }) 
  152. }, 
  153. move:function(t,i){ 
  154. var n = $(".guest").index($(i)); 
  155. this.angle = parseFloat($(i).attr("angle"))+0.5; 
  156. this.delect(n); 
  157. this.moveHost(i); 
  158. this.moveQuest(i); 
  159. this.moveRelationship(i); 
  160. this.changeClass(); 
  161. setTimeout(function(){t.newAppend(i)},500); 
  162. }, 
  163. newAppend:function(i){ 
  164. this.newAppendGuest(i,"guest","name",this.guestW,this.guestH,this.radius); 
  165. this.newAppendGuest(i,"relationship","relationship",this.relationW,this.relationH,this.radius/2); 
  166. }, 
  167.  
  168. newAppendGuest:function(i,className,name,w,h,r){ 
  169. var host = $(i).html(); 
  170. var guest = $(".guest").html(); 
  171. var box = $("#"+this.id); 
  172. var that = this
  173. var next=0; 
  174. for(var i=0; i<this.array.length; i++){ 
  175. if(host == this.array[i].name){ 
  176. for(var j=0;j<this.array[i].friend.length; j++){ 
  177. if(guest !== this.array[i].friend[j].name){ 
  178. next++; 
  179. var guests ="<span class='"+className+"'>"+this.array[i].friend[j][name]+"</span>"
  180. $(guests).appendTo(box).css({ 
  181. left:that.setQuestPose(this.array[i].friend.length,r,next,w,h,that.angle).left, 
  182. top:that.setQuestPose(this.array[i].friend.length,r,next,w,h,that.angle).top 
  183. }).attr("angle",that.angle+next/this.array[i].friend.length).hide().fadeIn(1000); 
  184. }, 
  185.  
  186. moveHost:function(i){ 
  187. var hLeft = parseInt($(".host").css("left")) + this.hostW/2; 
  188. var hTop = parseInt($(".host").css("top")) + this.hostH/2; 
  189. var gLeft = parseInt($(i).css("left")) + this.guestW/2; 
  190. var gTop = parseInt($(i).css("top")) + this.guestH/2; 
  191. var l = gLeft - hLeft; 
  192. var t = gTop - hTop; 
  193. var left = (hLeft - l - this.guestW/2)+"px"
  194. var top = (hTop - t - this.guestH/2)+"px"
  195. this.animate(".host",left,top); 
  196. }, 
  197. moveRelationship:function(i){ 
  198. var hLeft = parseInt($(".host").css("left")) + this.hostW/2;; 
  199. var hTop = parseInt($(".host").css("top")) + this.hostH/2; 
  200. var gLeft = parseInt($(".relationship").css("left")) + this.relationW/2; 
  201. var gTop = parseInt($(".relationship").css("top")) + this.relationH/2; 
  202. var l = gLeft - hLeft; 
  203. var t = gTop - hTop; 
  204. var left = (hLeft - l - this.relationW/2)+"px"
  205. var top = (hTop - t - this.relationH/2)+"px"
  206. this.animate(".relationship",left,top); 
  207. }, 
  208. moveQuest:function(i){ 
  209. var left = $(".host").css("left"); 
  210. var top = $(".host").css("top"); 
  211. this.animate(i,left,top); 
  212. }, 
  213. delect:function(n){ 
  214. $(".guest").slice(0,n).remove(); 
  215. $(".guest").slice(1).remove(); 
  216. $(".relationship").slice(0,n).remove(); 
  217. $(".relationship").slice(1).remove(); 
  218. }, 
  219. animate:function(i,left,top){ 
  220. $(i).animate({ 
  221. left:left, 
  222. top:top 
  223. },500); 
  224. }, 
  225. changeClass:function(){ 
  226. var that =this
  227. $(".guest").addClass("abcdef").removeClass("guest"); 
  228. $(".host").addClass("guest").removeClass("host").attr("angle",that.angle); 
  229. $(".abcdef").addClass("host").removeClass("abcdef").attr("angle",null); 
  230. $(document).ready(function(){ 
  231. relation.init(relationName,0) 
  232. }) 
  233. </script> 
  234. </head> 
  235. <body> 
  236. <div id="box"></div> 
  237. </body> 
  238. </html> 

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

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

图片精选