趣味JavaScript效果:会震动的浏览器
以下是产生震动效果的JavaScript代码:
<script>
//配置网页发生地震的可能性,100%表示一定发生
var chance_of_occurence=″50%″
//以下是用来存储10个比例值的变量(以1、3、6……等的形式)
var rectorscale=new Array(1,3,6,9,12,15,18,21,24,27)
chance_of_occurence=parseInt(chance_of_occurence)
function earthquake(){
//随机值(1到10之间)
rectorindex=Math.floor(Math.random()*10)
rector=rectorscale[rectorindex]
if ((document.all||document.layers)&&Math.floor(Math.random()*100)<=chance_of_occurence) {
//根据随机产生的比例数震动浏览器窗口!
for (i=0;i,i<20;i++){
window.moveBy(0,rector)
window.moveBy(rector,0)
window.moveBy(0,-rector)
window.moveBy(-rector,0)
}
//显示地震消息
quakealert()
}
}
if (document.all)
document.write(′<div id=″quakenotice_ie″></div>′)
function quakealert()
{
var quakemessage=′这里放置显示的文本′
if (document.all){
quakemsg_ie=document.all.quakenotice_ie
quakemsg_ie.innerHTML=quakemessage
//将地震消息放置在屏幕中央quakemsg_ie.style.left=document.body.scrollLeft+document.body.clientWidth/2-quakemsg_ie.offsetWidth/2
quakemsg_ie.style.top=document.body.scrollTop+document.body.clientHeight/2-quakemsg_ie.offsetHeight/2
quakemsg_ie.style.visibility=″visible″
setTimeout(″quakemsg_ie.style.visibility=′hidden′″,3000)
}
else if (document.layers){
quakemsg_ns=document.quakenotice_ns
quakemsg_ns.document.write(quakemessage)
quakemsg_ns.document.close()
quakemsg_ns.left=pageXOffset+window.innerWidth/2-quakemsg_ns.document.width/2
quakemsg_ns.top=pageYOffset+window.innerHeight/2-quakemsg_ns.document.height/2
quakemsg_ns.visibility=″show″
setTimeout(″quakemsg_ns.visibility=′hide′″,5000)
//震动大约5秒后停止
}
}
</script>