我想在div中随机显示内容或图片。我能够显示div fadein
和fadout
,但无法在div中显示随机位置(此处为)。
这是我的Fiddle。
var max = $('#line1').children().length;
var ctr = 1;
var i,j,k,t,next;
// items in order (ex. [1,2,3,4,5,6])
var itms=[];
for(i=1;i<=max;i++)
itms.push(i);
// now scramble the items randomly (ex. [6,2,3,1,2,4,5])
for(i=0;i<max;i++)
{
// choose two items randomly
j = Math.floor(Math.random() * max);
k = Math.floor(Math.random() * max);
// swap
t = itms[j];
itms[j] = itms[k];
itms[k] = t;
}
// fade in one item at time (following the scrambled list)
fadeIn(0, max, itms);
setInterval(function(){fadeOut(0, max, itms);},3000);
function fadeIn(current, max, itmsList)
{
$('#it' + itmsList[current]).fadeIn(1000);
if(current<max)
{
setTimeout( function() {fadeIn(current + 1,max,itmsList);}, 1000);//alert(current);
}
}
function fadeOut(current, max, itmsList)
{
$('#it' + itmsList[current]).fadeOut(8000);
if(current<max)
{
setTimeout( function() {fadeOut(current + 1,max,itmsList);}, 8000);//alert(current);
}
}
答案 0 :(得分:0)
你有点模糊,所以这个答案很多都是假设的。
//get height and width of view able area
var height = $(window).height();
var width = $(window).width();
//change these to a random function of your choice.
var randomx = 12; //rand(0, height)
var randomy = 47; //rand(0, width)
//subtract our random number from the view able area's dimensions
$('#it' + itmsList[current]).css({
left: (width-randomy)+"px",
top: (height-randomx)+"px"
});
随机函数示例: Generating random whole numbers in JavaScript in a specific range?