2013-08-07 36 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); 

});