Javascript 如何在网站上随机生成图像
我正在尝试将konami代码添加到我的网站。我有钥匙,所有的东西都在那里工作,但我试图做到的是,当用户输入konami代码并按空格键时,屏幕上会随机生成一个土豆(或不同土豆)的图像。在浏览者刷新之前,应禁用网站 我一直在谷歌上搜索,但我不确定我的描述是否正确。是否有人知道如何做到这一点,以提供JSFIDLE或指南 我更喜欢用Javascript来实现这一点Javascript 如何在网站上随机生成图像,javascript,html,css,Javascript,Html,Css,我正在尝试将konami代码添加到我的网站。我有钥匙,所有的东西都在那里工作,但我试图做到的是,当用户输入konami代码并按空格键时,屏幕上会随机生成一个土豆(或不同土豆)的图像。在浏览者刷新之前,应禁用网站 我一直在谷歌上搜索,但我不确定我的描述是否正确。是否有人知道如何做到这一点,以提供JSFIDLE或指南 我更喜欢用Javascript来实现这一点 fumction konami(){ document.onkeypress=function(){ //user pressed a ke
fumction konami(){
document.onkeypress=function(){
//user pressed a key
//let the magic happen
potato=document.createElement("img");
potato.src="potato.jpg";
potato.style.left=Math.random()*90+"%";
potato.style.top=Math.random()*90+"%";
potato.style.position="fixed";
potato.style.zIndex="100";
//add to body
document.all.body.appendChild(potato);
}
}
您说过您已经实现了konami代码,所以只需执行以下操作:
konami();
要启动马铃薯操作`
` <script type="text/javascript">
if ( window.addEventListener ) {
var state = 0, konami = [38,38,40,40,37,39,37,39,66,65];
window.addEventListener("keydown", function(e) {
if ( e.keyCode == konami[state] ) state++;
else state = 0;
if ( state == 10 )
<!--window.location = "http://www.teroromighty.com/site"; //you can write your own code here-->
<!--Wild Potatoes-->
input=document.getElementById("in");
length=1;
input.onkeypress=function(){
//user pressed a key
if(input.value.split(" ").length==length){
length++;
//input has a space
//let the magic happen
//repeat endless
potato=document.createElement("img");
potato.src="potato.jpg";
potato.style.left=MATH.random(0,90)+"%";
potato.style.top=MATH.random(0,90)+"%";
potato.style.position="fixed";
potato.style.zIndex="100";
//add to body
document.all.body.appendChild(potato);
}
}
}, true);
}
</script>`
如果(window.addEventListener){
var state=0,konami=[38,38,40,40,37,39,37,39,66,65];
addEventListener(“键控”,函数(e){
if(e.keyCode==konami[state])state++;
else状态=0;
如果(状态==10)
输入=document.getElementById(“in”);
长度=1;
input.onkeypress=函数(){
//用户按了一个键
if(input.value.split(“”).length==length){
长度++;
//输入有一个空格
//让魔法发生吧
//没完没了地重复
马铃薯=document.createElement(“img”);
potato.src=“potato.jpg”;
potato.style.left=MATH.random(0,90)+“%”;
potato.style.top=MATH.random(0,90)+“%”;
potato.style.position=“固定”;
马铃薯.style.zIndex=“100”;
//添加到正文中
文件.all.body.appendChild(马铃薯);
}
}
},对);
}
`
服务器上有图像吗?有。我有这个图像。你为什么要无休止地添加土豆?这不会使浏览器崩溃,因为你的while循环永远不会结束。这个想法是在我的一个网站上做一个复活节彩蛋。这是为了好玩。每次你按空格键,就会出现一个土豆,我想如果这个人做得太多,可能会使浏览器崩溃。@user3353882:哦,对不起,modified@RUser98:但是我的代码+谷歌+mozilla开发者网络文档会帮助他。很抱歉,我是超级nooby。我不完全确定我做错了什么。这是代码。我把它放在头上了。我将把它放在一个答案中,并添加:if(state==10){konami()}和使用//在jsp中进行注释这可能非常简单,但我仍然没有理解它。这是目前正在实施的网站。(在移动->调试错误)错误控制台告诉您什么?参数列表后显示:Uncaught SyntaxError:missing)