我想知道是否有任何方法可以在每次使用时更改 div
。
我想在 div
中更改页面顶部和左侧的位置,但不仅在一个 div 中,在许多 div 中,而且不重新加载页面。
例如
.class{
background-color:black;
color: white;
position:absolute;
top: RANDOM;
left: RANDOM;
}
<div1 class="class"></div> /// this would be positioned top:100px and left 200px
<div2 class="class"></div> /// this would be positioned top:245px and left 298px
<div3 class="class"></div> /// this would be positioned top:190px and left 490px
<div4 class="class"></div> /// this would be positioned top: 89px and left 344px
所有这些都在同一个页面中,这可能吗?
我试图找到类似的东西,但我只找到了一个当我点击它时会更改 div ID 的脚本,同一页面中没有太多的 div。
最佳答案
jQuery,拯救:
$('.class').each(function() {
$(this).css('top', Math.floor(Math.random() * 101));
$(this).css('left', Math.floor(Math.random() * 200));
});
还有(矫枉过正,我知道),一个演示:http://jsfiddle.net/pZ7Kf/10/ .
关于javascript - css 随机修复 JS 和多个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5711723/