邢栋博客
邢栋博客,Action博客,记录工作和生活中的点点滴滴
html5的history新特性
html5的history新特性
html5的history新特性 history.pushState和history.replaceState
history.pushState(state, title, url)
将当前URL和history.state加入到history中,并用新的state和URL替换当前。不会造成页面刷新。
state:对象 –state对象是一个JavaScript对象,它关系到由pushState()方法创建出来的新的history实体。用以存储关于你所要插入到历史 记录的条目的相关信息。State对象可以是任何Json字符串。因为firefox会使用用户的硬盘来存取state对象,这个对象的最大存储空间为640k。如果大于这个数 值,则pushState()方法会抛出一个异常。如果确实需要更多的空间来存储,请使用本地存储。
title:firefox现在回忽略这个参数,虽然它可能将来会被使用上。而现在最安全的使用方式是传一个空字符串,以防止将来的修改。或者可以传一个简短的标题来表示state
url:要跳转到的URL地址,不能跨域。
history.replaceState(state, title, url)
浏览器执行以下包含以下代码的html文件会崩溃,不要尝试
<html>
<body>
<script>
var total="";
for (var i=0;i<1000000;i++)
{
total= total+i.toString();
history.pushState(0,0,total);
}
</script>
</body>
</html>
html5的history新特性 history.pushState和history.replaceState
history.pushState(state, title, url)
将当前URL和history.state加入到history中,并用新的state和URL替换当前。不会造成页面刷新。
state:对象 –state对象是一个JavaScript对象,它关系到由pushState()方法创建出来的新的history实体。用以存储关于你所要插入到历史 记录的条目的相关信息。State对象可以是任何Json字符串。因为firefox会使用用户的硬盘来存取state对象,这个对象的最大存储空间为640k。如果大于这个数 值,则pushState()方法会抛出一个异常。如果确实需要更多的空间来存储,请使用本地存储。
title:firefox现在回忽略这个参数,虽然它可能将来会被使用上。而现在最安全的使用方式是传一个空字符串,以防止将来的修改。或者可以传一个简短的标题来表示state
url:要跳转到的URL地址,不能跨域。
history.replaceState(state, title, url)
浏览器执行以下包含以下代码的html文件会崩溃,不要尝试
<html>
<body>
<script>
var total="";
for (var i=0;i<1000000;i++)
{
total= total+i.toString();
history.pushState(0,0,total);
}
</script>
</body>
</html>
html5表单验证pattern属性修改提示信息
例子
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>html5表单验证pattern属性修改提示信息</title>
<script type="text/javascript">
function validateIt(inputelement){
if(inputelement.validity.patternMismatch){
inputelement.setCustomValidity('只允许输入字母');
}else{
inputelement.setCustomValidity(''); //输入内容符合验证条件
}
return true;
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="username" pattern="[a-zA-Z]+" required oninvalid="validateIt(this)" oninput="validateIt(this)"/>
<input type="submit" />
</form>
</body>
</html>
用canvas画的网站背景(转)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>particle nets</title>
<style>
html,body{
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;}
</style>
</head>
<body>
<canvas id="cas"></canvas>
<script>
var canvas = document.getElementById("cas");
var ctx = canvas.getContext("2d");
resize();
window.onresize = resize;
function resize(){
canvas.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
canvas.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}
var RAF = (function () {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
// 鼠标活动时,获取鼠标坐标
var warea = {x: null, y: null, max: 20000};
window.onmousemove = function(e){
e = e || window.event;
warea.x = e.clientX;
warea.y = e.clientY;
};
window.onmouseout = function(e){
warea.x = null;
warea.y = null;
};
// 添加粒子
// x,y为粒子坐标,xa, ya为粒子xy轴加速度,max为连线的最大距离
var dots = [];
for(var i=0;i<300;i++){
var x = Math.random()*canvas.width;
var y = Math.random()*canvas.height;
var xa = Math.random() * 2 - 1;
var ya = Math.random() * 2 - 1;
dots.push({
x: x,
y: y,
xa: xa,
ya: ya,
max: 6000
})
}
// 延迟100秒开始执行动画,如果立即执行有时位置计算会出错
setTimeout(function(){
animate();
}, 100);
// 每一帧循环的逻辑
function animate(){
ctx.clearRect(0,0,canvas.width, canvas.height);
// 将鼠标坐标添加进去,产生一个用于比对距离的点数组
var ndots = [warea].concat(dots);
dots.forEach(function(dot){
// 粒子位移
dot.x += dot.xa;
dot.y += dot.ya;
// 遇到边界将加速度反向
dot.xa *= (dot.x > canvas.width || dot.x < 0)? -1 : 1;
dot.ya *= (dot.y > canvas.height || dot.y < 0)? -1 : 1;
// 绘制点
ctx.fillRect(dot.x - 0.5, dot.y - 0.5, 1, 1);
// 循环比对粒子间的距离
for (var i = 0; i < ndots.length; i++) {
var d2 = ndots[i];
if (dot === d2 || d2.x === null || d2.y === null) continue;
var xc = dot.x - d2.x;
var yc = dot.y - d2.y;
// 两个粒子之间的距离
var dis = xc * xc + yc * yc;
// 距离比
var ratio;
// 如果两个粒子之间的距离小于粒子对象的max值,则在两个粒子间画线
if(dis < d2.max){
// 如果是鼠标,则让粒子向鼠标的位置移动
if (d2 === warea && dis > (d2.max / 2)) {
dot.x -= xc * 0.03;
dot.y -= yc * 0.03;
}
// 计算距离比
ratio = (d2.max - dis) / d2.max;
// 画线
ctx.beginPath();
ctx.lineWidth = ratio/2;
ctx.strokeStyle = 'rgba(0,0,0,' + (ratio + 0.2) + ')';
ctx.moveTo(dot.x , dot.y);
ctx.lineTo(d2.x , d2.y);
ctx.stroke();
}
}
// 将已经计算过的粒子从数组中删除
ndots.splice(ndots.indexOf(dot), 1);
});
RAF(animate);
}
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>particle nets</title>
<style>
html,body{
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;}
</style>
</head>
<body>
<canvas id="cas"></canvas>
<script>
var canvas = document.getElementById("cas");
var ctx = canvas.getContext("2d");
resize();
window.onresize = resize;
function resize(){
canvas.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
canvas.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}
var RAF = (function () {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
// 鼠标活动时,获取鼠标坐标
var warea = {x: null, y: null, max: 20000};
window.onmousemove = function(e){
e = e || window.event;
warea.x = e.clientX;
warea.y = e.clientY;
};
window.onmouseout = function(e){
warea.x = null;
warea.y = null;
};
// 添加粒子
// x,y为粒子坐标,xa, ya为粒子xy轴加速度,max为连线的最大距离
var dots = [];
for(var i=0;i<300;i++){
var x = Math.random()*canvas.width;
var y = Math.random()*canvas.height;
var xa = Math.random() * 2 - 1;
var ya = Math.random() * 2 - 1;
dots.push({
x: x,
y: y,
xa: xa,
ya: ya,
max: 6000
})
}
// 延迟100秒开始执行动画,如果立即执行有时位置计算会出错
setTimeout(function(){
animate();
}, 100);
// 每一帧循环的逻辑
function animate(){
ctx.clearRect(0,0,canvas.width, canvas.height);
// 将鼠标坐标添加进去,产生一个用于比对距离的点数组
var ndots = [warea].concat(dots);
dots.forEach(function(dot){
// 粒子位移
dot.x += dot.xa;
dot.y += dot.ya;
// 遇到边界将加速度反向
dot.xa *= (dot.x > canvas.width || dot.x < 0)? -1 : 1;
dot.ya *= (dot.y > canvas.height || dot.y < 0)? -1 : 1;
// 绘制点
ctx.fillRect(dot.x - 0.5, dot.y - 0.5, 1, 1);
// 循环比对粒子间的距离
for (var i = 0; i < ndots.length; i++) {
var d2 = ndots[i];
if (dot === d2 || d2.x === null || d2.y === null) continue;
var xc = dot.x - d2.x;
var yc = dot.y - d2.y;
// 两个粒子之间的距离
var dis = xc * xc + yc * yc;
// 距离比
var ratio;
// 如果两个粒子之间的距离小于粒子对象的max值,则在两个粒子间画线
if(dis < d2.max){
// 如果是鼠标,则让粒子向鼠标的位置移动
if (d2 === warea && dis > (d2.max / 2)) {
dot.x -= xc * 0.03;
dot.y -= yc * 0.03;
}
// 计算距离比
ratio = (d2.max - dis) / d2.max;
// 画线
ctx.beginPath();
ctx.lineWidth = ratio/2;
ctx.strokeStyle = 'rgba(0,0,0,' + (ratio + 0.2) + ')';
ctx.moveTo(dot.x , dot.y);
ctx.lineTo(d2.x , d2.y);
ctx.stroke();
}
}
// 将已经计算过的粒子从数组中删除
ndots.splice(ndots.indexOf(dot), 1);
});
RAF(animate);
}
</script>
</body>
</html>
html5红玫瑰
标签:
html5
<!doctype html>
<html><head><title>Love</title><meta charset="utf-8" />
<!--[if IE]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]--></head>
<body><canvas id="c"></canvas><script> var b = document.body; var c = document.getElementsByTagName('canvas')[0]; var a = c.getContext('2d'); document.body.clientWidth; </script><script> // start of submission //
with (m = Math) C = cos, S = sin, P = pow, R = random; c.width = c.height = f = 500; h = -250; function p(a, b, c) { if (c > 60) return [S(a * 7) * (13 + 5 / (.2 + P(b * 4, 4))) - S(b) * 50, b * f + 50, 625 + C(a * 7) * (13 + 5 / (.2 + P(b * 4, 4))) + b * 400, a * 1 - b / 2, a]; A = a * 2 - 1; B = b * 2 - 1; if (A * A + B * B < 1) { if (c > 37) { n = (j = c & 1) ? 6 : 4; o = .5 / (a + .01) + C(b * 125) * 3 - a * 300; w = b * h; return [o * C(n) + w * S(n) + j * 610 - 390, o * S(n) - w * C(n) + 550 - j * 350, 1180 + C(B + A) * 99 - j * 300, .4 - a * .1 + P(1 - B * B, -h * 6) * .15 - a * b * .4 + C(a + b) / 5 + P(C((o * (a + 1) + (B > 0 ? w : -w)) / 25), 30) * .1 * (1 - B * B), o / 1e3 + .7 - o * w * 3e-6] } if (c > 32) { c = c * 1.16 - .15; o = a * 45 - 20; w = b * b * h; z = o * S(c) + w * C(c) + 620; return [o * C(c) - w * S(c), 28 + C(B * .5) * 99 - b * b * b * 60 - z / 2 - h, z, (b * b * .3 + P((1 - (A * A)), 7) * .15 + .3) * b, b * .7] } o = A * (2 - b) * (80 - c * 2); w = 99 - C(A) * 120 - C(b) * (-h - c * 4.9) + C(P(1 - b, 7)) * 50 + c * 2; z = o * S(c) + w * C(c) + 700; return [o * C(c) - w * S(c), B * 99 - C(P(b, 7)) * 50 - c / 3 - z / 1.35 + 450, z, (1 - b / 1.2) * .9 + a * .1, P((1 - b), 20) / 4 + .05] } } setInterval('for(i=0;i<1e4;i++)if(s=p(R(),R(),i%46/.74)){z=s[2];x=~~(s[0]*f/z-h);y=~~(s[1]*f/z-h);if(!m[q=y*f+x]|m[q]>z)m[q]=z,a.fillStyle="rgb("+~(s[3]*h)+","+~(s[4]*h)+","+~(s[3]*s[3]*-80)+")",a.fillRect(x,y,1,1)}', 0) // end of submission //</script>
</body></html>
<html><head><title>Love</title><meta charset="utf-8" />
<!--[if IE]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]--></head>
<body><canvas id="c"></canvas><script> var b = document.body; var c = document.getElementsByTagName('canvas')[0]; var a = c.getContext('2d'); document.body.clientWidth; </script><script> // start of submission //
with (m = Math) C = cos, S = sin, P = pow, R = random; c.width = c.height = f = 500; h = -250; function p(a, b, c) { if (c > 60) return [S(a * 7) * (13 + 5 / (.2 + P(b * 4, 4))) - S(b) * 50, b * f + 50, 625 + C(a * 7) * (13 + 5 / (.2 + P(b * 4, 4))) + b * 400, a * 1 - b / 2, a]; A = a * 2 - 1; B = b * 2 - 1; if (A * A + B * B < 1) { if (c > 37) { n = (j = c & 1) ? 6 : 4; o = .5 / (a + .01) + C(b * 125) * 3 - a * 300; w = b * h; return [o * C(n) + w * S(n) + j * 610 - 390, o * S(n) - w * C(n) + 550 - j * 350, 1180 + C(B + A) * 99 - j * 300, .4 - a * .1 + P(1 - B * B, -h * 6) * .15 - a * b * .4 + C(a + b) / 5 + P(C((o * (a + 1) + (B > 0 ? w : -w)) / 25), 30) * .1 * (1 - B * B), o / 1e3 + .7 - o * w * 3e-6] } if (c > 32) { c = c * 1.16 - .15; o = a * 45 - 20; w = b * b * h; z = o * S(c) + w * C(c) + 620; return [o * C(c) - w * S(c), 28 + C(B * .5) * 99 - b * b * b * 60 - z / 2 - h, z, (b * b * .3 + P((1 - (A * A)), 7) * .15 + .3) * b, b * .7] } o = A * (2 - b) * (80 - c * 2); w = 99 - C(A) * 120 - C(b) * (-h - c * 4.9) + C(P(1 - b, 7)) * 50 + c * 2; z = o * S(c) + w * C(c) + 700; return [o * C(c) - w * S(c), B * 99 - C(P(b, 7)) * 50 - c / 3 - z / 1.35 + 450, z, (1 - b / 1.2) * .9 + a * .1, P((1 - b), 20) / 4 + .05] } } setInterval('for(i=0;i<1e4;i++)if(s=p(R(),R(),i%46/.74)){z=s[2];x=~~(s[0]*f/z-h);y=~~(s[1]*f/z-h);if(!m[q=y*f+x]|m[q]>z)m[q]=z,a.fillStyle="rgb("+~(s[3]*h)+","+~(s[4]*h)+","+~(s[3]*s[3]*-80)+")",a.fillRect(x,y,1,1)}', 0) // end of submission //</script>
</body></html>