使用单选按钮使 Div 在页面上随机移动
Making a Div move across a page randomly with radio buttons?
有点
新编程到一般和弄乱一些JS。我正在尝试获得一些理解并帮助解决此问题。我去过图书馆,试图弄清楚这一点。
我想做的是有 3 个单选按钮。我将它们命名为小、中、大,中间有一个div。div 应该在屏幕中间。人员从单选按钮中选择位移 delta。用户将鼠标悬停在彩色div 上,然后div 将移动到随机的新位置(我在想 random(( 方法(,然后是 [delta -5, delta +5] 之间的位移。
马上。该程序有效。我似乎无法将"div"放在图像的中心,并且 math.random 在页面上移动的div 很慢?我怎样才能让它更快?
所以这些是我不确定的问题。
1( 如何使div 居中?我尝试了两次更改位置,但这导致"中心"不是一个正确的名称,然后尝试了顶部和宽度,不起作用。
2(我怎样才能通过math.random使div移动得更快。
3(有没有办法代替刷新页面,我可以转到另一个"收音机"并且速度减慢而不会使这些变得复杂?
这是我到目前为止的代码。
<html>
<head></head>
<style>
div.moving{
width: 90px;
height:90px;
background-color:black;
position:absolute;}
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
1 px <input type="radio" id="s" name="move" value="1" checked="checked"><br>
10 px <input type="radio" id="m" name="move" value="10"><br>
100 px <input type="radio" id="l" name="move" value="100"><br>
<div class='moving' id="mainDiv" onmouseover=" move();"></div>
</body>
<script type="text/javascript">
$( document ).ready( function(){
$('#mainDiv').mouseover(function(){
var stepSize = parseInt( $('input[type=radio][name=move]:checked').val() );
var pos = $( this ).position();
var left = parseInt( pos.left );
var top = parseInt( pos.top );
var new_left = left + (getRandomIntInclusive(0,stepSize) * [-1,1][Math.floor(Math.random() * 2) * 10] );
var new_top = top + (getRandomIntInclusive(0,stepSize) * [-1,1][Math.floor(Math.random() * 2) * 10] );
$( this ).css('left', new_left + 'px' );
$( this ).css('top', new_top + 'px' );
});});
document.getElementById('mainDiv').style.left="700px";
function getRandomIntInclusive(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
</script>
<html>
<head></head>
<style>
html.body {
width: 100%;
height: 100%;
}
div.moving{
width: 90px;
height:90px;
background-color:black;
position:absolute;
left: 0;
right: 0;
margin: 0 auto;}
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
1 px <input type="radio" id="s" name="move" value="1" checked="checked"><br>
10 px <input type="radio" id="m" name="move" value="10"><br>
100 px <input type="radio" id="l" name="move" value="100"><br>
<div class='moving' id="mainDiv" onmouseover=" move();"></div>
</body>
<script type="text/javascript">
$( document ).ready( function(){
$('#mainDiv').mouseover(function(){
var stepSize = parseInt( $('input[type=radio][name=move]:checked').val() );
var pos = $( this ).position();
var left = parseInt( pos.left );
var top = parseInt( pos.top );
var new_left = left + (getRandomIntInclusive(0,stepSize) * [-1,1][Math.floor(Math.random() * 2) * 10] );
var new_top = top + (getRandomIntInclusive(0,stepSize) * [-1,1][Math.floor(Math.random() * 2) * 10] );
$( this ).css('left', new_left + 'px' );
$( this ).css('top', new_top + 'px' );
});});
document.getElementById('mainDiv').style.left="0px";
function getRandomIntInclusive(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
</script>
相关文章:
- 如何在JS中随机化backgroundPosition而不使用鼠标移动来制作万花筒的动画
- 随机jquery移动/角度内部错误与安卓浏览器
- 如何在Javascript中在桌子周围随机移动图像
- 使用单选按钮使 Div 在页面上随机移动
- 如何让 Div 使用单选按钮随机移动
- 不透明度+随机移动的随机计时
- 使用JavaScript和html将图像和超链接从一个位置随机移动到另一个位置
- 鼠标下移页面X,页面Y和鼠标移动页面X后页面Y不一样,随机发生
- 一些潜水器的起始位置和随机移动
- 如何让数组对象随机移动
- 用鼠标移动随机放置的盒子
- 如何移动一个图像随机当你点击另一个图像使用html &javascript
- 谷歌地图API移动到远标记在每次渲染(似乎随机)
- 使用原型继承模式的随机移动动画
- Kineticjs:画布中图像的随机移动动画
- 精灵图像的随机移动位置
- 使图像随机移动,点击
- 从Javascript中的坐标随机移动
- 随机移动的图像被屏幕的左上角吸引
- 在固定区域中随机移动图像