开发者

Random position of draggable images on each new reload using jquery

I've figured out how to succes开发者_运维知识库sfully make two images draggable using jquery, as you can see in this example here: http://www.demarconia.com/kingofqueens/kingtest3.html

what I would like to do is to give the two images a unique random position on the page upon each reload. Is this possible? Thanks in advance!

Nick


Just give each element absolute positioning and assign a random value to the left and top attributes:

$('img.draggable').each(function(i,el){

    var tLeft = Math.floor(Math.random()*500),
        tTop  = Math.floor(Math.random()*500);

    $(el).css({position:'absolute', left: tLeft, top: tTop});

});

Example

You can change the multiplier (500) for tLeft and tTop to give the general area you want the images to appear within.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜