[英]random css position jquery
使用math.random,我可以將div隨機放置在左側。 但是我需要隨機設置“左”和“右”位置。
jQuery('#popup-container').css({'left' : (Math.floor(Math.random() * 15) + 3) + '%'})
<div id="popup-container" style="background:red;width:30px; height:30px;"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
例如,在屬性“ .css({left”中,有一個帶有“ left或right”的randon。具有這些位置的數組。我該怎么做?
只需創建一個數組,然后將“ left”和“ right”推入其中即可。
var positions = [];
positions.push('left');
positions.push('right');
現在,使用Math.random選擇positions數組的索引。
jQuery('#popup-container').css(positions[Math.floor(Math.floor(Math.random()%2))], (Math.floor(Math.random() * 15) + 3) + '%')
希望這可以幫助。
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.