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


Posted in Javascript onMay 07, 2015

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

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
#box{
width:500px; height:500px;
position: relative
}
.host{
position:absolute;
width:100px; height:50px;
line-height:50px; text-align:center;
color:#000000; background-color:#eeeeee;
border:#000000 1px solid; font-weight:bolder
}
.guest{
position:absolute;
width:80px; height:40px;
line-height:40px;text-align:center;
color: #999999; background-color:#FFFFFF;
border:#000000 1px solid; cursor:pointer
}
.relationship{
position:absolute;
width:60px; height:20px;
color: #aaa; line-height:20px;
font-size:12px; text-align:center
}
</style>
<script src="jquery-1.6.2.min.js"></script>
<script>
var relationName = [
 {name:"成龙",friend:[
 {name:"房祖名",relationship:"父子"},
 {name:"林凤娇",relationship:"夫妻"},
 {name:"吴绮莉",relationship:"绯闻"},
 {name:"徐静蕾",relationship:"激吻"}]
 },
 {name:"房祖名",friend:[
 {name:"成龙",relationship:"父子"},
 {name:"林凤娇",relationship:"母子"},
 {name:"方大同",relationship:"情敌"},
 {name:"薛凯琪",relationship:"女友"},
 {name:"陈坤",relationship:"朋友"},
 {name:"赵薇",relationship:"朋友"}]
 },
 {name:"林凤娇",friend:[
 {name:"成龙",relationship:"夫妻"},
 {name:"房祖名",relationship:"母子"},
 {name:"吴绮莉",relationship:"情敌"}]
 },
 {name:"吴绮莉",friend:[
 {name:"成龙",relationship:"绯闻"},
 {name:"林凤娇",relationship:"情敌"},
 {name:"吴卓林",relationship:"母女"}]
 },
 {name:"徐静蕾",friend:[
 {name:"李亚鹏",relationship:"电影"},
 {name:"韩寒",relationship:"娱乐圈"},
 {name:"成龙",relationship:"激吻"},
 {name:"黄立行",relationship:"电影"}]
 },
 {name:"方大同",friend:[
 {name:"房祖名",relationship:"情敌"},
 {name:"薛凯琪",relationship:"否认拍拖"},
 {name:"林宥嘉",relationship:"歌手"},
 {name:"韩庚",relationship:"演唱会"}]
 },
 {name:"薛凯琪",friend:[
 {name:"方大同",relationship:"否认拍拖"},
 {name:"房祖名",relationship:"女友"}]
 }
]
var relation = {
 radius:150,
 boxW:500,
 boxH:500,
 hostW:100,
 hostH:50,
 guestW:80,
 guestH:40,
 relationW:60,
 relationH:20,
 angle:0,
 id:"box",
 init:function(array,n){//传入参数1:数组 参数2:第几个
 this.array = array;
 this.appendHost(this.array,n);
 this.appendQuest(this.array,n);
 this.appendRelationShip(this.array,n);
 },
 appendHost:function(array,n){
 var box = $("#"+this.id);
 var host ="<span class='host'>"+array[n].name+"</span>";
 box.append(host)
 this.postHost();
 },
 postHost:function(){
 var x = (this.boxW - this.hostW)/2;
 var y = (this.boxH - this.hostH)/2;
 $(".host").css({
  left:x,
  top:y
 })
 },
 appendQuest:function(array,n){
 var box = $("#"+this.id);
 var guests="";
 var that = this;
 for(var i=0; i<array[n].friend.length; i++){
  guests+="<span class='guest'>"+array[n].friend[i].name+"</span>";
 }
 $(guests).appendTo(box);
 $(".guest").live("click",function(){
  that.move(that,this);
 })
 this.postQuest();
 },
 postQuest:function(){
 var guests = $(".guest");
 var that = this;
 guests.each(function(i){
  guests.eq(i).css({
  left:that.setQuestPose(guests.length,that.radius,i,that.guestW,that.guestH,that.angle).left,
  top:that.setQuestPose(guests.length,that.radius,i,that.guestW,that.guestH,that.angle).top
  }).attr("angle",i/guests.length)
 })
 },
 setQuestPose:function(n,r,i,w,h,d){
//n:代表共几个对象 r代表周长 i代表第几个对象 
//w代表外面对象的宽带 h代表外面对象的高度 d代表其实角度
 var p = i/n*Math.PI*2+Math.PI*2*d;
 var x = r * Math.cos(p);
 var y = r * Math.sin(p);
 return {
  "left":parseInt(this.boxW/2+ x - w/2),
  "top":parseInt(this.boxH/2 + y - h/2)
 }
 },
 appendRelationShip:function(array,n){
 var box = $("#"+this.id);
 var relation="";
 for(var i=0; i<array[n].friend.length; i++){
  relation+="<span class='relationship'>"+array[n].friend[i].relationship+"</span>";
 }
 box.append(relation);
 this.postRelationShip();
 },
 postRelationShip:function(){
 var guests = $(".relationship");
 var that = this;
 guests.each(function(i){
  guests.eq(i).css({
  left:that.setQuestPose(guests.length,that.radius/2,i,that.relationW,that.relationH,that.angle).left,
  top:that.setQuestPose(guests.length,that.radius/2,i,that.relationW,that.relationH,that.angle).top
  })
 })
 },
 move:function(t,i){
 var n = $(".guest").index($(i));
 this.angle = parseFloat($(i).attr("angle"))+0.5;
 this.delect(n);
 this.moveHost(i);
 this.moveQuest(i);
 this.moveRelationship(i);
 this.changeClass();
 setTimeout(function(){t.newAppend(i)},500);
 },
 newAppend:function(i){
 this.newAppendGuest(i,"guest","name",this.guestW,this.guestH,this.radius);
 this.newAppendGuest(i,"relationship","relationship",this.relationW,this.relationH,this.radius/2);
 },
 
 newAppendGuest:function(i,className,name,w,h,r){
 var host = $(i).html();
 var guest = $(".guest").html();
 var box = $("#"+this.id);
 var that = this;
 var next=0;
 for(var i=0; i<this.array.length; i++){
  if(host == this.array[i].name){
  for(var j=0;j<this.array[i].friend.length; j++){
   if(guest !== this.array[i].friend[j].name){
   next++;
   var guests ="<span class='"+className+"'>"+this.array[i].friend[j][name]+"</span>";
   $(guests).appendTo(box).css({
    left:that.setQuestPose(this.array[i].friend.length,r,next,w,h,that.angle).left,
    top:that.setQuestPose(this.array[i].friend.length,r,next,w,h,that.angle).top
   }).attr("angle",that.angle+next/this.array[i].friend.length).hide().fadeIn(1000);
   }
  }
  }
 }
 },
 
 moveHost:function(i){
 var hLeft = parseInt($(".host").css("left")) + this.hostW/2;
 var hTop = parseInt($(".host").css("top")) + this.hostH/2;
 var gLeft = parseInt($(i).css("left")) + this.guestW/2;
 var gTop = parseInt($(i).css("top")) + this.guestH/2;
 var l = gLeft - hLeft;
 var t = gTop - hTop;
 var left = (hLeft - l - this.guestW/2)+"px";
 var top = (hTop - t - this.guestH/2)+"px";
 this.animate(".host",left,top);
 },
 moveRelationship:function(i){
 var hLeft = parseInt($(".host").css("left")) + this.hostW/2;;
 var hTop = parseInt($(".host").css("top")) + this.hostH/2;
 var gLeft = parseInt($(".relationship").css("left")) + this.relationW/2;
 var gTop = parseInt($(".relationship").css("top")) + this.relationH/2;
 var l = gLeft - hLeft;
 var t = gTop - hTop;
 var left = (hLeft - l - this.relationW/2)+"px";
 var top = (hTop - t - this.relationH/2)+"px";
 this.animate(".relationship",left,top);
 },
 moveQuest:function(i){
 var left = $(".host").css("left");
 var top = $(".host").css("top");
 this.animate(i,left,top);
 },
 delect:function(n){
 $(".guest").slice(0,n).remove();
 $(".guest").slice(1).remove();
 $(".relationship").slice(0,n).remove();
 $(".relationship").slice(1).remove();
 },
 animate:function(i,left,top){
 $(i).animate({
  left:left,
  top:top
 },500);
 },
 changeClass:function(){
 var that =this;
 $(".guest").addClass("abcdef").removeClass("guest");
 $(".host").addClass("guest").removeClass("host").attr("angle",that.angle);
 $(".abcdef").addClass("host").removeClass("abcdef").attr("angle",null);
 }
}
$(document).ready(function(){
 relation.init(relationName,0)
})
</script>
</head>
<body>
<div id="box"></div>
</body>
</html>

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

Javascript 相关文章推荐
JQuery CSS样式控制 学习笔记
Jul 23 Javascript
js实现的跟随鼠标移动的时钟效果(中英文日期显示)
Jan 17 Javascript
JavaScript实现点击按钮后变灰避免多次重复提交
Jul 15 Javascript
javaScript函数中执行C#代码中的函数方法总结
Aug 07 Javascript
javascript实现网页端解压并查看zip文件
Dec 15 Javascript
JS中跨页面调用变量和函数的方法(例如a.js 和 b.js中互相调用)
Nov 01 Javascript
基于LayUI实现前端分页功能的方法
Jul 22 Javascript
vue通过指令(directives)实现点击空白处收起下拉框
Dec 06 Javascript
解决echarts的多个折现数据出现坐标和值对不上的问题
Dec 28 Javascript
ES6 Object.assign()的用法及其使用
Jan 18 Javascript
js实现简单的无缝轮播效果
Sep 05 Javascript
浅析JavaScript 函数柯里化
Sep 08 Javascript
js显示文本框提示文字的方法
May 07 #Javascript
js实现模拟计算器退格键删除文字效果的方法
May 07 #Javascript
浅谈document.write()输出样式
May 07 #Javascript
js库Modernizr的介绍和使用
May 07 #Javascript
详谈LABJS按需动态加载js文件
May 07 #Javascript
JavaScript改变CSS样式的方法汇总
May 07 #Javascript
js实现非常简单的焦点图切换特效实例
May 07 #Javascript
You might like
php实例分享之二维数组排序
2014/05/15 PHP
php操作mongoDB实例分析
2014/12/29 PHP
PHP实现根据密码长度显示安全条
2017/07/04 PHP
php empty 函数判断结果为空但实际值却为非空的原因解析
2018/05/28 PHP
jQuery $.each的用法说明
2010/03/22 Javascript
在UpdatePanel内jquery easyui效果失效的解决方法
2010/04/11 Javascript
在表单提交前进行验证的几种方式整理
2013/07/31 Javascript
JS动态调用方法名示例介绍
2013/12/18 Javascript
模拟用户点击弹出新页面不会被浏览器拦截
2014/04/08 Javascript
jQuery中focus事件用法实例
2014/12/26 Javascript
基于JavaScript实现div层跟随滚动条滑动
2016/01/12 Javascript
Bootstrap组件学习之导航、标签、面包屑导航(精品)
2016/05/17 Javascript
js记录点击某个按钮的次数-刷新次数为初始状态的实例
2017/02/15 Javascript
xmlplus组件设计系列之选项卡(Tabbar)(5)
2017/05/03 Javascript
微信小程序开发之好友列表字母列表跳转对应位置
2017/09/26 Javascript
javascript填充默认头像方法
2018/02/22 Javascript
剖析Angular Component的源码示例
2018/03/23 Javascript
使用vue-cli(vue脚手架)快速搭建项目的方法
2018/05/21 Javascript
vue检测对象和数组的变化分析
2018/06/30 Javascript
原生js实现移动端Touch轮播图的方法步骤
2019/01/03 Javascript
IE11下处理Promise及Vue的单项数据流问题
2019/07/24 Javascript
Vue数据双向绑定原理实例解析
2020/05/15 Javascript
微信小程序实现多行文字滚动
2020/11/18 Javascript
[49:31]DOTA2-DPC中国联赛 正赛 Elephant vs LBZS BO3 第二场 1月29日
2021/03/11 DOTA
Python编写合并字典并实现敏感目录的小脚本
2019/02/26 Python
Python字符串对象实现原理详解
2019/07/01 Python
Python类中self参数用法详解
2020/02/13 Python
tensorflow 20:搭网络,导出模型,运行模型的实例
2020/05/26 Python
Nike挪威官网:Nike.com (NO)
2018/11/26 全球购物
黄色火烈鸟:De Gele Flamingo
2019/03/18 全球购物
新闻专业毕业生英文求职信
2014/03/19 职场文书
师德师风自查总结
2014/10/14 职场文书
2015年党员岗位承诺书
2015/04/27 职场文书
团组织关系介绍信
2019/06/24 职场文书
win11系统中dhcp服务异常什么意思? Win11 DHCP服务异常修复方法
2022/04/08 数码科技
六个好看实用的 HTML + CSS 后台登录入口页面
2022/04/28 HTML / CSS