js磁力线代码(非压缩,自己在压缩的版本上优化了代码,易于阅读)
拿去白嫖吧:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="referrer" content="origin" />
<meta property="og:description"
content="你可能在很多博客中看到类似当鼠标移动上去的时候会出现线条吸附在鼠标周围的特效,鼠标离开的时候线条就自动散开。" />
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>漂浮磁力线/鼠标吸铁石特效</title>
<script src="https://common.cnblogs.com/scripts/jquery-2.2.0.min.js"></script>
</head> <body>
<div id="page_end_html">
<!--磁力线放射-->
<script>
! function () {
function setNodeAttribute(node, attribute, value) {
return node.getAttribute(attribute) || value
}; function getEleByTagName(name) {
return document.getElementsByTagName(name)
}; function setDefaultAttribute() {
var elements = getEleByTagName("script"),
len = elements.length,
i = elements[len - 1];
return {
len: len,
zIndex: setNodeAttribute(i, "zIndex", -1),
opacity: setNodeAttribute(i, "opacity", .6),
color: setNodeAttribute(i, "color", "32,178,170"),
count: setNodeAttribute(i, "count", 150)
//<!-- opacity 参数设置的是透明程度,数字越小越透明; color 设置颜色; count 设置磁线的数量 -->
}
}; function initSize() {
canvasWidth = canvasEle.width = window.innerWidth || document.documentElement.clientWidth ||
document.body.clientWidth;
canvasHeight = canvasEle.height = window.innerHeight || document.documentElement.clientHeight ||
document.body.clientHeight
}; function start() {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
var node, e, curWidth, deltaX, deltaY, distance;
nodes.forEach(function (item, index) {
item.x += item.xa;
item.y += item.ya;
item.xa *= item.x > canvasWidth || item.x < 0 ? -1 : 1;
item.ya *= item.y > canvasHeight || item.y < 0 ? -1 : 1;
ctx.fillRect(item.x - .5, item.y - .5, 1, 1);
for (var e = index + 1; e < nodesIncludeMouse.length; e++) {
node = nodesIncludeMouse[e];
if (node.x !== null && node.y !== null) {
deltaX = item.x - node.x;
deltaY = item.y - node.y;
distance = deltaX * deltaX + deltaY * deltaY;
if (distance < node.max) {
if (node === mouse && distance >= node.max / 2) {
item.x -= .03 * deltaX;
item.y -= .03 * deltaY;
}
curWidth = (node.max - distance) / node.max;
ctx.beginPath();
ctx.lineWidth = curWidth / 2;
ctx.strokeStyle = "rgba(" + defaultAttr.color + "," + (curWidth + .2) + ")";
ctx.moveTo(item.x, item.y);
ctx.lineTo(node.x, node.y), ctx.stroke();
}
}
}
});
animate(start);
};
var canvasWidth, canvasHeight, nodesIncludeMouse, canvasEle = document.createElement("canvas"),
defaultAttr = setDefaultAttribute(),
ctx = canvasEle.getContext("2d"),
animate = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window
.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function (n) {
window.setTimeout(n, 1e3 / 45)
},
mathRandom = Math.random,
mouse = {
x: null,
y: null,
max: 2e4
};
canvasEle.id = "c_n" + defaultAttr.len;
canvasEle.style.cssText = "position:fixed;top:0;left:0;z-index:" + defaultAttr.zIndex + ";opacity:" +
defaultAttr.opacity;
getEleByTagName("body")[0].appendChild(canvasEle);
initSize();
window.onresize = initSize;
window.onmousemove = function (n) {
n = n || window.event, mouse.x = n.clientX, mouse.y = n.clientY
};
window.onmouseout = function () {
mouse.x = null, mouse.y = null
};
for (var nodes = [], f = 0; defaultAttr.count > f; f++) {
var h = mathRandom() * canvasWidth,
g = mathRandom() * canvasHeight,
v = 2 * mathRandom() - 1,
p = 2 * mathRandom() - 1;
nodes.push({
x: h, //x坐标
y: g, //y坐标
xa: v, //x速度
ya: p, //y速度
max: 6e3 //两点可连线的最大距离
});
}
nodesIncludeMouse = nodes.concat([mouse]);
setTimeout(function () {
start();
}, 100)
}();
</script> <script type="text/javascript">
/* 鼠标特效 */
var a_idx = 0;
jQuery(document).ready(function ($) {
$("body").click(function (e) {
var a = new Array("Laravel", "Wordpress", "公众号", "小程序", "ERP", "CRM", "帝国cms",
"Python", "Magento", "Dedecms", "Linux", "CI", "Thinkphp");
/* 可想而知,这里可以修改显示的字 */
var $i = $("<span />").text(a[a_idx]);
a_idx = (a_idx + 1) % a.length;
var x = e.pageX,
y = e.pageY;
$i.css({
"z-index": 999999999999999999999999999999999999999999999999999999999999999999999,
"top": y - 20,
"left": x,
"position": "absolute",
"font-weight": "bold",
"color": "#ff6651"
});
$("body").append($i);
$i.animate({
"top": y - 180,
"opacity": 0
},
1500,
function () {
$i.remove();
});
});
});
</script>
</div>
</body> </html>
js磁力线代码(非压缩,自己在压缩的版本上优化了代码,易于阅读)的更多相关文章
- "如何用70行Java代码实现深度神经网络算法" 的delphi版本
http://blog.csdn.net/hustjoyboy/article/details/50721535 "如何用70行Java代码实现深度神经网络算法" 的delphi ...
- js分析 快速定位 js 代码, 还原被混淆压缩的 js 代码
-1.目录 0.参考 1.页面表现 2. 慢镜头观察:低速网络请求 3. 从头到尾调试:Fiddler 拦截 index.html 并添加 debugger; 4. 快速定位 js 代码 5. 还原被 ...
- Grunt: 拼接代码,js丑化(压缩),css压缩,html压缩,观察文件,拷贝文件,删除文件,压缩文件
准备工作 grunt 基于nodeJs所以 nodeJs需要的基础配置都需要安装 1.Grunt 安装 npm install -g grunt-cli 这是全局安装 2.在当前文件下npm init ...
- 前端自动化(三) 合并压缩css、压缩js、添加时间戳、打包上线操作
前端自动化(三) 合并压缩css.压缩js.添加时间戳.打包上线操作 此文在前两篇基础上对比参考,会很方便理解 gulp.task("delete",function(){ r ...
- 使用gulp对js、css、img进行合并压缩
1 概述 最新使用AngularJS框架做单页面项目,其中包括了很多库的和自已写的js.css.img文件,这些文件都不大,但是数量众多,导致web请求文件过多,一次性加载时比较慢.有尝试过使用异步加 ...
- require.js+backbone 使用r.js 在本地与生产环境 一键压缩的实现方式
require.js+backbone 使用r.js 在本地与生产环境 一键压缩的实现方式 时间:2017-07-03 17:18:11 阅读:210 评论:0 收藏:0 ...
- node.js中使用imagemagick进行图片裁剪压缩
node.js中使用imagemagick进行图片裁剪压缩 安装imagemagick sudo apt-get install imagemagick or wget http://www.imag ...
- Joomla - 优化(时区、google字体、压缩图片、压缩自定义代码)
Joomla - 优化(时区.google字体.压缩图片.压缩自定义代码) 一.时区 发布文章是往往会发现发布时间和当前时间对不上,原因是 Joomla 用的是国际标准时间,和中国时区大约相差8小时, ...
- 两行代码搞定网站gzip压缩
网站使用gzip压缩的好处就不用多说了吧,自行脑补,来说一下如何使用nodejs实现gzip压缩,只需要两行代码,so ease. 通过nodejs实现gzip 需要用到的模块 compression ...
随机推荐
- mysql笔记(暂时)
2018-05-28 create table cms_user(id int key auto_increment,username varchar(20),password varchar(20) ...
- 深入理解windows 消息机制
深入理解Windows消息机制 今天我们来学一学Windows消息机制,我们知道在传统的C语音程序中,当我们需要打开一个文件时,我们可以调用fopen()函数,这个函数最后又会调用操作系统提供的函数以 ...
- Java并发读书笔记:如何实现线程间正确通信
目录 一.synchronized 与 volatile 二.等待/通知机制 等待 通知 面试常问的几个问题 sleep方法和wait方法的区别 关于放弃对象监视器 三.等待通知典型 生产者消费者模型 ...
- 超长可视化指南!带你理清K8S部署的故障排查思路,让bug无处遁形
本文将帮助你厘清在Kubernetes中调试 deployment的思路.下图是完整的故障排查思路,如果你想获得更清晰的图片,请在公众号后台(RancherLabs)回复"troublesh ...
- java架构之路-(微服务专题)ribbon的基本使用和内部算法的自我实现
上次回归: 上次我们主要说了,我们的注册中心nacos的使用,如我们的命名空间.分组.集群.版本等是如何使用的,如果是这样呢?我们现在有三个用户服务和三个订单服务,我们应该如何分发这些请求呢?都请求到 ...
- 本地开发环境伪装成SSL连接的实现
本地ssl开发测试实现1,在外网服务器上使用测试域名和t.test.cn,用let's encrypt申请 证书并正常运行2,修改本地服务器host文件,将t.kennylee.vip指向127.0. ...
- Angular组件通信
一. 组件间通信(组件间不能互相调用,公共方法放在服务中) (目前项目采用将公共方法直接写在ts文件中没使用服务) ng g service services/服务名 App.module.ts{ 引 ...
- linux中的特殊符号及其含义梳理
1. 重定向符号及含义 注意:箭头流向即是数据的流向. 数字0:标准输入(standard input,简写stdin),数据从右往左方向流动 数字1:标准正确输出(standard output,简 ...
- matplotlib如何显示中文
问题:matplotlib不能渲染中文 想设定为中文字体,网上搜索的方法几乎都是下面这样,已经把字体拷贝到了程序目录下了,然而并没有生效 plt.rcParams [ font.sans-serif' ...
- oracle11g R2数据库的迁移(同windows系统迁移)使用RMAN
实验环境:windows 2008 R2 & windows 2008 R2 Oracle版本:11.2.0.1.0 源数据库端: 为保证在恢复之后的数据库中得到一致的数据,应禁止用户对数据的 ...