函数名可以简化吗 由于要大量的操作DOM,所以getElementById(), getElementsByTagName()用的很多,我想如果能将getElementById()简写作g(),不仅敲代码的速度会快,而且脚本加载的速度也会变快。不知道能否实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以,参照这段代码(非原创)<!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> <title>JS1k: Breathing Galaxies (1013 bytes)</title> <script type="text/javascript">window.onload = function () { C = Math.cos; // cache Math objects S = Math.sin; U = 0; w = window; j = document; d = j.getElementById("c"); c = d.getContext("2d"); W = d.width = w.innerWidth; H = d.height = w.innerHeight; c.fillRect(0, 0, W, H); // resize <canvas> and draw black rect (default) c.globalCompositeOperation = "lighter"; // switch to additive color application c.lineWidth = 0.2; c.lineCap = "round"; var bool = 0, t = 0; // theta d.onmousemove = function (e) { if(window.T) { if(D==9) { D=Math.random()*15; f(1); } clearTimeout(T); } X = e.pageX; // grab mouse pixel coords Y = e.pageY; a=0; // previous coord.x b=0; // previous coord.y A = X, // original coord.x B = Y; // original coord.y R=(e.pageX/W * 999>>0)/999; r=(e.pageY/H * 999>>0)/999; U=e.pageX/H * 360 >>0; D=9; g = 360 * Math.PI / 180; T = setInterval(f = function (e) { // start looping spectrum c.save(); c.globalCompositeOperation = "source-over"; // switch to additive color application if(e!=1) { c.fillStyle = "rgba(0,0,0,0.02)"; c.fillRect(0, 0, W, H); // resize <canvas> and draw black rect (default) } c.restore(); i = 25; while(i --) { c.beginPath(); if(D > 450 || bool) { // decrease diameter if(!bool) { // has hit maximum bool = 1; } if(D < 0.1) { // has hit minimum bool = 0; } t -= g; // decrease theta D -= 0.1; // decrease size } if(!bool) { t += g; // increase theta D += 0.1; // increase size } q = (R / r - 1) * t; // create hypotrochoid from current mouse position, and setup variables (see: http://en.wikipedia.org/wiki/Hypotrochoid) x = (R - r) * C(t) + D * C(q) + (A + (X - A) * (i / 25)) + (r - R); // center on xy coords y = (R - r) * S(t) - D * S(q) + (B + (Y - B) * (i / 25)); if (a) { // draw once two points are set c.moveTo(a, b); c.lineTo(x, y) } c.strokeStyle = "hsla(" + (U % 360) + ",100%,50%,0.75)"; // draw rainbow hypotrochoid c.stroke(); a = x; // set previous coord.x b = y; // set previous coord.y } U -= 0.5; // increment hue A = X; // set original coord.x B = Y; // set original coord.y }, 16); } j.onkeydown = function(e) { a=b=0; R += 0.05 } d.onmousemove({pageX:300, pageY:290})}</script></head><body><canvas id="c"></canvas></body></html>但是楼主如果只是自己玩玩儿还可以,如果你要是在公司老板会允许吗?而切如果更换版本代码进行维护,而楼主又不在不是给维护你代码的人造成很多麻烦吗? function g(id){return document.getElementById(id);}自己封装一下试试 function g(id){ return document.getElementById(id);}function tags(tag){ return document.getElementsByTagName(tag);} jquery之类的做的就是这样的工作$("#id")就代替了document.getElementById("id");的工作.觉得代码长影响性能(传输)可以用代码压缩工具发布。不过开发时候还是要求尽量好理解的变量名以及方法名。可以参考YUI Compressor或者其他的压缩工具。 page-break-before:always 打印问题。。。。 美的日电集团协同商务平台-那家公司做的 div中checkbox事件 用脚本调用另一个页面的脚本的问题(A页面脚本调用 B页面脚本) java权限控制js菜单显示 下拉列表处理时间问题?帮帮帮 如何知道DIV移动到了哪个页面元素上方 eclipse中文乱码MyEclipse没问题 求教: ie打印功能! 有什么办法可以做到拒绝使用 tencent 的浏览器浏览? 【非小白】关于getElementById获取不到对象 js能否识别一定范围的鼠标点击?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JS1k: Breathing Galaxies (1013 bytes)</title>
<script type="text/javascript">
window.onload = function () {
C = Math.cos; // cache Math objects
S = Math.sin;
U = 0;
w = window;
j = document;
d = j.getElementById("c");
c = d.getContext("2d");
W = d.width = w.innerWidth;
H = d.height = w.innerHeight;
c.fillRect(0, 0, W, H); // resize <canvas> and draw black rect (default)
c.globalCompositeOperation = "lighter"; // switch to additive color application
c.lineWidth = 0.2;
c.lineCap = "round";
var bool = 0,
t = 0; // theta
d.onmousemove = function (e) {
if(window.T) {
if(D==9) { D=Math.random()*15; f(1); }
clearTimeout(T);
}
X = e.pageX; // grab mouse pixel coords
Y = e.pageY;
a=0; // previous coord.x
b=0; // previous coord.y
A = X, // original coord.x
B = Y; // original coord.y
R=(e.pageX/W * 999>>0)/999;
r=(e.pageY/H * 999>>0)/999;
U=e.pageX/H * 360 >>0;
D=9;
g = 360 * Math.PI / 180;
T = setInterval(f = function (e) { // start looping spectrum
c.save();
c.globalCompositeOperation = "source-over"; // switch to additive color application
if(e!=1) {
c.fillStyle = "rgba(0,0,0,0.02)";
c.fillRect(0, 0, W, H); // resize <canvas> and draw black rect (default)
}
c.restore();
i = 25; while(i --) {
c.beginPath();
if(D > 450 || bool) { // decrease diameter
if(!bool) { // has hit maximum
bool = 1;
}
if(D < 0.1) { // has hit minimum
bool = 0;
}
t -= g; // decrease theta
D -= 0.1; // decrease size
}
if(!bool) {
t += g; // increase theta
D += 0.1; // increase size
}
q = (R / r - 1) * t; // create hypotrochoid from current mouse position, and setup variables (see: http://en.wikipedia.org/wiki/Hypotrochoid)
x = (R - r) * C(t) + D * C(q) + (A + (X - A) * (i / 25)) + (r - R); // center on xy coords
y = (R - r) * S(t) - D * S(q) + (B + (Y - B) * (i / 25));
if (a) { // draw once two points are set
c.moveTo(a, b);
c.lineTo(x, y)
}
c.strokeStyle = "hsla(" + (U % 360) + ",100%,50%,0.75)"; // draw rainbow hypotrochoid
c.stroke();
a = x; // set previous coord.x
b = y; // set previous coord.y
}
U -= 0.5; // increment hue
A = X; // set original coord.x
B = Y; // set original coord.y
}, 16);
}
j.onkeydown = function(e) { a=b=0; R += 0.05 }
d.onmousemove({pageX:300, pageY:290})
}</script>
</head>
<body>
<canvas id="c"></canvas>
</body>
</html>但是楼主如果只是自己玩玩儿还可以,如果你要是在公司老板会允许吗?而切如果更换版本代码进行维护,而楼主又不在不是给维护你代码的人造成很多麻烦吗?
return document.getElementById(id);
}
自己封装一下试试
return document.getElementById(id);
}
function tags(tag){
return document.getElementsByTagName(tag);
}
$("#id")就代替了document.getElementById("id");的工作.
觉得代码长影响性能(传输)可以用代码压缩工具发布。
不过开发时候还是要求尽量好理解的变量名以及方法名。
可以参考YUI Compressor或者其他的压缩工具。