2013-08-07 34 views
0

随机化图像位置我有这样的:与jQuery和HTML

<div id="randomp" style="position:absolute;top:3px;left:3px;width:165px;height:29px;background:url(/logo2.png) no-repeat;background-size: 165px;"></div> 

我想的是,propierty“顶”和“左”改变你每次进入页面的时间。我的意思是,有些时候,它出现在右上角,右下角,左上角和左下角..

这是我tryied: http://redzer.com.mx/tabla.html

+0

请添加您的代码。 – Whistletoe

+0

我不太确定这是否可行,但在概念上,有一个阵列的位置(可能是2D,所以它与XY很好地相关),当页面加载时,“top:x_pixel”等等。 –

+0

我该怎么做?即时通讯如此困惑:S – Xcite

回答

0

我可能会与开始DIV风格为position:fixeddisplay:none

<div id="randomp" style="display:none;position:fixed;width:165px;height:29px;background:url(/logo2.png) no-repeat;background-size: 165px;"></div> 

然后使用jQuery来确定位置CSS设置并打开知名度

$(document).ready(function() { 
    // get jQuery object for the div 
    var $randomp = $('#randomp'); 
    // determine whether to show top or bottom, left or right 
    var top = Math.round(Math.random()); // generate 0 or 1 value 
    if (top === 1) { 
     $randomp.css('top', '3px'); 
    } else { 
     $randomp.css('bottom', '3px'); 
    }  
    var left = Math.round(Math.random()); 
    if (left === 1) { 
     $randomp.css('left', '3px'); 
    } else { 
     $randomp.css('right', '3px'); 
    } 
    // show the div 
    $randomp.show(); 
}); 

当然,你也可以使用服务器端代码来做到这一点,但既然你问了你的标签中的javascript/jquery,我建议这个解决方案。

+0

它不工作兄弟,请参阅:[测试] (http://redzer.com.mx/tabla.html) – Xcite

+0

@ Xcite对不起没有意识到我的答案的HTML部分没有足够的空间,所以没有显示出来 –

+0

真棒工作,非常感谢你:) – Xcite

0

我想我得到了你需要的东西。

EXAMPLE

用JavaScript我生成的顶部和图像的左侧定位每次访问页面时的随机数。 现在我将它们设置为0到100之间的随机数字,但您可以将其更改为任何您想要的值。

var random1 = Math.ceil(Math.random() * 100); 
var random2 = Math.ceil(Math.random() * 100); 

$(document).ready(function() { 

    $('#randomp').css('top', random1); 
    $('#randomp').css('left', random2); 

});