只是我最初的想法,但是您可以为 3 个不同的位置类编写 CSS,并将它们称为“位置 1”、“位置 2”、“位置 3”。如果需要,然后在 javascript(或 PHP)中加载页面时,生成 1 到 3 之间的随机数,将类添加"position"+randomNumber
到元素中,然后它将位于其中一个位置。这类似于我用于随机背景图像的技术。
更新
Also, if you want to use more descriptive class names for the locations, you could keep a mapping of a number to a class name (or use something like position in an array), to relate a a random number to the class to apply.
Code
CSS:
<style>
.position1 {
// Whatever style you want for position 1
}
.position2 {
// Whatever style you want for position 2
}
.position3 {
// Whatever style you want for position 3
}
</style>
JS:
$(document).ready( function () {
var randomIndex = Math.floor(Math.random()*3)+1; //3 is the number of options you have; +1 makes the range 1 - 3 instead of 0 - 2
$('#my-form').addClass('position'+randomIndex); //Adds the new class the element with id = 'my-form'
}