Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery游戏按钮生成器_Jquery_Button_Random_Numbers_Coordinates - Fatal编程技术网

JQuery游戏按钮生成器

JQuery游戏按钮生成器,jquery,button,random,numbers,coordinates,Jquery,Button,Random,Numbers,Coordinates,我一直在寻找一个游戏的想法,我有很多年。我刚刚开始使用JQuery,我想知道如何制作一个游戏,你点击一个按钮,它就会在不同的位置重生 我所知道的是,我也会制作一个画布,然后在画布中生成2个数字,然后将其移动到该位置 如何做到这一点?只需操纵应用于按钮的CSS即可做到这一点。以下是一个例子: JavaScript: $(document).ready(function(){ $("#button").on("click", function(){ var widthMax

我一直在寻找一个游戏的想法,我有很多年。我刚刚开始使用JQuery,我想知道如何制作一个游戏,你点击一个按钮,它就会在不同的位置重生

我所知道的是,我也会制作一个画布,然后在画布中生成2个数字,然后将其移动到该位置


如何做到这一点?

只需操纵应用于按钮的CSS即可做到这一点。以下是一个例子:

JavaScript:

$(document).ready(function(){
    $("#button").on("click", function(){
        var widthMax = $(window).width() - $("#button").width();
        var heightMax = $(window).height() - $("#button").height();

        var randomLeft = Math.random() * (widthMax);
        var randomTop = Math.random() * (heightMax);

        $("#button").css("left", randomLeft).css("top", randomTop);
    });
});
<input type="button" style="position: absolute;" id="button" value="Click Me" />
HTML:

$(document).ready(function(){
    $("#button").on("click", function(){
        var widthMax = $(window).width() - $("#button").width();
        var heightMax = $(window).height() - $("#button").height();

        var randomLeft = Math.random() * (widthMax);
        var randomTop = Math.random() * (heightMax);

        $("#button").css("left", randomLeft).css("top", randomTop);
    });
});
<input type="button" style="position: absolute;" id="button" value="Click Me" />


您可以使用javascript
Math.random()
生成你的随机坐标,但我认为你需要充实一下你的想法,并提供你已经尝试过的代码,以便我们可以提供帮助。
$('button')。单击(function(){$(this).css({top:Math.random()*100,left:Math.random()*100})非常感谢大家。这是我第一次使用这项服务,你们帮了我这么多!我也会在这附近帮忙。再次感谢。