js 实现关键词球状旋转效果
效果图

html
<!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=gb2312" />
<title>关键字球状效果</title>
<link rel="stylesheet" href="css/style.css" />
<script type="text/javascript" src="js/lrtk.js"></script>
</head>
<body>
<!-- 代码开始 -->
<div id="div1">
<a href="http://www.lanrentuku.com" target="_blank">起名取名</a> <a
href="http://www.lanrentuku.com" target="_blank">宣传策划</a> <a
href="http://www.lanrentuku.com" target="_blank">网游试玩</a> <a
href="http://www.lanrentuku.com" target="_blank">短信表白</a> <a
href="http://www.lanrentuku.com" target="_blank">卡片设计</a> <a
href="http://www.lanrentuku.com" target="_blank">网页设计</a> <a
href="http://www.lanrentuku.com" target="_blank">SEO优化</a> <a
href="http://www.lanrentuku.com" target="_blank">外语翻译</a> <a
href="http://www.lanrentuku.com" target="_blank">产品推广</a> <a
href="http://www.lanrentuku.com" target="_blank">网络营销</a> <a
href="http://www.lanrentuku.com" target="_blank">动漫设计</a> <a
href="http://www.lanrentuku.com" target="_blank">招聘求职</a> <a
href="http://www.lanrentuku.com" target="_blank">家居装修</a> <a
href="http://www.lanrentuku.com" target="_blank">影视创作</a> <a
href="http://www.lanrentuku.com" target="_blank">照片美化</a> <a
href="http://www.lanrentuku.com" target="_blank">产品设计</a> <a
href="http://www.lanrentuku.com" target="_blank">包装设计</a> <a
href="http://www.lanrentuku.com" target="_blank">Logo设计</a> <a
href="http://www.lanrentuku.com" target="_blank">海报设计</a> <a
href="http://www.lanrentuku.com" target="_blank">程序开发</a> <a
href="http://www.lanrentuku.com" target="_blank">网店推广</a> <a
href="http://www.lanrentuku.com" target="_blank">微博营销</a> <a
href="http://www.lanrentuku.com" target="_blank">网站开发</a> <a
href="http://www.lanrentuku.com" target="_blank">宣传品设计</a> <a
href="http://www.lanrentuku.com" target="_blank">配音配词</a> <a
href="http://www.lanrentuku.com" target="_blank">产品推广</a> <a
href="http://www.lanrentuku.com" target="_blank">网络营销</a> <a
href="http://www.lanrentuku.com" target="_blank">动漫设计</a> <a
href="http://www.lanrentuku.com" target="_blank">招聘求职</a> <a
href="http://www.lanrentuku.com" target="_blank">家居装修</a> <a
href="http://www.lanrentuku.com" target="_blank">影视创作</a> <a
href="http://www.lanrentuku.com" target="_blank">照片美化</a>
</div>
</body>
</html>
css
body{background:#09c;}
#div1{height:400px;width:450px; position:relative; margin:10px auto;}
#div1 a{position:absolute;top:0px;left:0px;color:#fff;font-weight:bold;padding:3px 6px;}
#div1 a:hover{border:1px solid #eee;background:#000;border-radius:5px;}
javascript
var radius = 150;
var dtr = Math.PI / 180;
var d = 300; var mcList = [];
var active = false;
var lasta = 1;
var lastb = 1;
var distr = true;
var tspeed = 2;
var size = 250; var mouseX = 0;
var mouseY = 0; var howElliptical = 1; var aA = null;
var oDiv = null; window.onload = function() {
var i = 0;
var oTag = null; oDiv = document.getElementById('div1'); aA = oDiv.getElementsByTagName('a'); for (i = 0; i < aA.length; i++) {
oTag = {}; oTag.offsetWidth = aA[i].offsetWidth;
oTag.offsetHeight = aA[i].offsetHeight; mcList.push(oTag);
} sineCosine(0, 0, 0); positionAll(); oDiv.onmouseover = function() {
active = true;
}; oDiv.onmouseout = function() {
active = false;
}; oDiv.onmousemove = function(ev) {
var oEvent = window.event || ev; mouseX = oEvent.clientX - (oDiv.offsetLeft + oDiv.offsetWidth / 2);
mouseY = oEvent.clientY - (oDiv.offsetTop + oDiv.offsetHeight / 2); mouseX /= 5;
mouseY /= 5;
}; setInterval(update, 30);
}; function update() {
var a;
var b; if (active) {
a = (-Math.min(Math.max(-mouseY, -size), size) / radius) * tspeed;
b = (Math.min(Math.max(-mouseX, -size), size) / radius) * tspeed;
} else {
a = lasta * 0.98;
b = lastb * 0.98;
} lasta = a;
lastb = b; if (Math.abs(a) <= 0.01 && Math.abs(b) <= 0.01) {
return;
} var c = 0;
sineCosine(a, b, c);
for (var j = 0; j < mcList.length; j++) {
var rx1 = mcList[j].cx;
var ry1 = mcList[j].cy * ca + mcList[j].cz * (-sa);
var rz1 = mcList[j].cy * sa + mcList[j].cz * ca; var rx2 = rx1 * cb + rz1 * sb;
var ry2 = ry1;
var rz2 = rx1 * (-sb) + rz1 * cb; var rx3 = rx2 * cc + ry2 * (-sc);
var ry3 = rx2 * sc + ry2 * cc;
var rz3 = rz2; mcList[j].cx = rx3;
mcList[j].cy = ry3;
mcList[j].cz = rz3; per = d / (d + rz3); mcList[j].x = (howElliptical * rx3 * per) - (howElliptical * 2);
mcList[j].y = ry3 * per;
mcList[j].scale = per;
mcList[j].alpha = per; mcList[j].alpha = (mcList[j].alpha - 0.6) * (10 / 6);
} doPosition();
depthSort();
} function depthSort() {
var i = 0;
var aTmp = []; for (i = 0; i < aA.length; i++) {
aTmp.push(aA[i]);
} aTmp.sort(function(vItem1, vItem2) {
if (vItem1.cz > vItem2.cz) {
return -1;
} else if (vItem1.cz < vItem2.cz) {
return 1;
} else {
return 0;
}
}); for (i = 0; i < aTmp.length; i++) {
aTmp[i].style.zIndex = i;
}
} function positionAll() {
var phi = 0;
var theta = 0;
var max = mcList.length;
var i = 0; var aTmp = [];
var oFragment = document.createDocumentFragment(); // 随机排序
for (i = 0; i < aA.length; i++) {
aTmp.push(aA[i]);
} aTmp.sort(function() {
return Math.random() < 0.5 ? 1 : -1;
}); for (i = 0; i < aTmp.length; i++) {
oFragment.appendChild(aTmp[i]);
} oDiv.appendChild(oFragment); for (var i = 1; i < max + 1; i++) {
if (distr) {
phi = Math.acos(-1 + (2 * i - 1) / max);
theta = Math.sqrt(max * Math.PI) * phi;
} else {
phi = Math.random() * (Math.PI);
theta = Math.random() * (2 * Math.PI);
}
// 坐标变换
mcList[i - 1].cx = radius * Math.cos(theta) * Math.sin(phi);
mcList[i - 1].cy = radius * Math.sin(theta) * Math.sin(phi);
mcList[i - 1].cz = radius * Math.cos(phi); aA[i - 1].style.left = mcList[i - 1].cx + oDiv.offsetWidth / 2
- mcList[i - 1].offsetWidth / 2 + 'px';
aA[i - 1].style.top = mcList[i - 1].cy + oDiv.offsetHeight / 2
- mcList[i - 1].offsetHeight / 2 + 'px';
}
} function doPosition() {
var l = oDiv.offsetWidth / 2;
var t = oDiv.offsetHeight / 2;
for (var i = 0; i < mcList.length; i++) {
aA[i].style.left = mcList[i].cx + l - mcList[i].offsetWidth / 2 + 'px';
aA[i].style.top = mcList[i].cy + t - mcList[i].offsetHeight / 2 + 'px'; aA[i].style.fontSize = Math.ceil(12 * mcList[i].scale / 2) + 8 + 'px'; aA[i].style.filter = "alpha(opacity=" + 100 * mcList[i].alpha + ")";
aA[i].style.opacity = mcList[i].alpha;
}
} function sineCosine(a, b, c) {
sa = Math.sin(a * dtr);
ca = Math.cos(a * dtr);
sb = Math.sin(b * dtr);
cb = Math.cos(b * dtr);
sc = Math.sin(c * dtr);
cc = Math.cos(c * dtr);
}
js 实现关键词球状旋转效果的更多相关文章
- js实现关键词高亮显示 正则匹配
html 和ajax 部分就不写了,只需将需要匹配的文字传进去就可以了 比如匹配后台传回的字符串data.content中的关键词:直接调用: data.content = highLightKeyw ...
- Python-JS (JS介绍~JS的基础数据类型)
目录一.JS语言介绍: 1.JS概念 2.JS组成 二.JS的三种存在位置(引入方式): 1.行间式: 2.内联式: 3.外联式: 三.JS出现的具体位置: 四.JS语法规范 五.JS中变量的定义 E ...
- PHP 结合 Boostrap 结合 js 实现学生列表删除编辑以及搜索功能(完结)
这个自己的小项目要先告一段落了.可能还有许多bug.请见谅 删除学生功能 PHP: // 这里是通过前端代码HTML中的 url 传过来的,用 $_GET 来获取(相关HTML代码可以看一下到主页看一 ...
- JavaScipt的秘密花园
对象使用和属性 JavaScript 中所有变量都可以当作对象使用,除了两个例外 null 和undefined. false.toString(); // 'false'[1, 2, 3].toSt ...
- JavaScript学习笔记(一)对象和属性
对象属性的使用 JavaScript中的所有变量都可以当做对象使用,除了null和undefined. false.toString(); // 'false' [1, 2, 3].toString( ...
- JavaScript代码规范
变量名:驼峰命名法(首单词小写,后面每个单词首字母大写) firstName = "John"; lastName = "Doe"; price = 19.90 ...
- 2018-01-15 History in Threads: 火狐插件实现浏览历史按主题显示(树)
History in Threads似乎是唯一一个业余项目里有确认用户的. 大部分JavaScript源码(300+行)也用了中文命名. 插件功能很简单, 就是根据网页点击顺序生成树, 每个树可以认为 ...
- 十款不错的Hybrid App移动开发框架
本文转载至http://www.pureasme.com/blog/2015/0419476.html ionic 是个高级的 HTML5 移动端应用框架,是个很漂亮的使用 HTML5 开发混合移动应 ...
- JavaScript 秘密花园——对象的使用和属性操作
JavaScript 中所有变量都是对象,除了两个例外 null 和 undefined. false.toString(); // 'false' [1, 2, 3].toString(); // ...
随机推荐
- 在Linux下写一个线程池以及线程池的一些用法和注意点
-->线程池介绍(大部分来自网络) 在这个部分,详细的介绍一下线程池的作用以及它的技术背景以及他提供的一些服务等.大部分内容来自我日常生活中在网络中学习到的一些概念性的东西. -->代码 ...
- BZOJ 1015 星球大战
Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...
- Extjs打开window窗口自动加载html网页
Window inherits the autoLoad config option from Panel. Note that I included all config options below ...
- Highways
poj1751:http://poj.org/problem?id=1751 题意:给你n个城市,每个城市的坐标给你,然后为了是每个城市都连通,需要在已经建了一些街道额基础上,再次建一些街道使其连通, ...
- Ubiquitous Religions(并查集)
Description There are so many different religions in the world today that it is difficult to keep tr ...
- http请求方式和状态管理
1. HTTP协议 Internet的基本协议是TCP/IP协议(传输控制协议和网际协议),目前广泛使用的 FTP.HTTP(超文本传输协议).Archie Gopher都是建立在TCP/IP上面的 ...
- 【HDOJ】5128
暴力+计算几何. /* 5128 */ #include <iostream> #include <algorithm> #include <cstdio> #in ...
- bzoj2096
本来也不打算写这道题的解题报告的,因为比较水直接维护两个单调队列(最大值,最小值)随便弄弄就行了但是我开始疯狂不知道为什么的RE,然后实在没办法找root要了数据测了之后……王苍,根本就没有错啊……我 ...
- C语言基础课程 第二课 HelloWorld不为菜鸟所知的秘密
1 愉快的开端hello world 4 1.1 include头文件包含 4 1.2 main函数 4 1.3 注释 4 1.4 {}括号,程序 ...
- Linux企业级开发技术(4)——epoll企业级开发之epoll例程
为了使大家更加深入了解epoll模型在企业应用中的使用,下面给出一段基于epoll的服务器代码,并在代码中添加了详细注释: #include <deque> #include <ma ...