【发布时间】:2020-07-05 07:03:59
【问题描述】:
我正在尝试让不同尺寸的图像在页面中心一个接一个地随机出现,现在我设法让它们随机出现并让它们具有不同的宽度和高度。但是我确定我可以针对所有类而不是复制每个具有不同名称的代码?我记得有一次看到一个函数以一个类为目标并自动跟随它的数字,比如 .item +(然后是扩展名?)
欢迎提供有关让它们出现在中心周围而不是分散在各处的任何提示。我将它与许多不同的代码一起修补,所以它有点像科学怪人。
$(".grid-item").hide().each(function(i) {
$(this).delay(i*500).fadeIn(500);
});
var pane_width = $(".random-pane").width() - $(".grid-item").width();
var pane_height = $(".random-pane").height() - $(".grid-item").height();
$(".random-pane").children().each( function(){
var x = Math.round(Math.random() * pane_width);
var y = Math.round(Math.random() * pane_height);
$(this).css("top",y);
$(this).css("left",x);
});
var rand=Math.floor(Math.random()*30)+10;
var width=Math.round(($(window).width()/100)*rand);
$('.item-1').width(width);
var rand=Math.floor(Math.random()*30)+10;
var width=Math.round(($(window).width()/100)*rand);
$('.item-2').width(width);
var rand=Math.floor(Math.random()*30)+10;
var width=Math.round(($(window).width()/100)*rand);
$('.item-3').width(width);
var rand=Math.floor(Math.random()*30)+10;
var width=Math.round(($(window).width()/100)*rand);
$('.item-4').width(width);
var rand=Math.floor(Math.random()*30)+10;
var width=Math.round(($(window).width()/100)*rand);
$('.item-5').width(width);
【问题讨论】:
-
关于
Any tips regarding letting them appear around the center instead of spread across are welcome. I patched this together with a lot of different codes, so it's bit of a Frankenstein.你的问题应该集中在一个问题上。单独的解决方案应该有一个单独的问题,以便对其他人有用。
标签: javascript jquery random