简体   繁体   English

刷新/加载页面时为 CSS(左和上)生成随机值

[英]Generating Random values for CSS (left and top) when refreshing / loading the page

I have several frames in my html that are draggable using a js function.我的 html 中有几个框架可以使用 js function 拖动。 They are scattered around with a unique "left" and "top" values in CSS.它们散布在 CSS 中,具有独特的“左”和“顶”值。

I want to generate random left and top values every time the page is loaded / refreshed.每次加载/刷新页面时,我都想生成随机的左侧和顶部值。 They should range from 10 to 1000 (px).它们的范围应为 10 到 1000 (px)。

div CSS (I have around 7 of them): div CSS(我有大约 7 个):

#mydiv1 {
    position: absolute;
    z-index: 0;
    width: 250px;
    height: 250px;
    top: 400px;
    left: 500px;
    background-color: black;
    border: 2px solid #e8ff26;
    text-align: center;
    box-shadow: 2px 2px 4px #888888;
    cursor: move;
}

Left and top are what I want to randomize for each of the 7 or more divs I have. Left 和 top 是我想要为我拥有的 7 个或更多 div 中的每一个随机化的。

I looked up some JS examples but I have not been successful yet.我查找了一些 JS 示例,但还没有成功。 Below is a not working attempt:下面是一个不工作的尝试:

$(function(){
    var divClass = "div_"+Math.floor((Math.random() * 10) + 1);
    $('body').addClass(divClass);
});

attempt 2:尝试2:

$(document).ready(function() {
    $("body").css("left", "(" + Math.floor(Math.random() * (10)) );
});

Random values can be generated and applied without using the JQuery无需使用 JQuery 即可生成和应用随机值

 const myDiv = document.querySelector("#mydiv1"); const randomLeftValue = Number.parseInt(Math.random() * (1000 - 10) + 10); const randomTopValue = Number.parseInt(Math.random() * (1000 - 10) + 10); myDiv.style.top = `${randomLeftValue}px`; myDiv.style.left = `${randomTopValue}px`;
 #mydiv1 { position: absolute; z-index: 0; width: 250px; height: 250px; background-color: black; border: 2px solid #e8ff26; text-align: center; box-shadow: 2px 2px 4px #888888; cursor: move; }
 <div id="mydiv1"> </div>

document.addEventListener("DOMContentLoaded", function(event) { 
    let item        = document.getElementById('mydiv1');
    let rng_top     = Math.floor(Math.random() * 1000);
    let rng_left    = Math.floor(Math.random() * 1000);
    item.style.top  = `${rng_top}px`;
    item.style.left = `${rng_left}px`;
});
$('document').ready(() => { // will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

const left = Math.floor(Math.random() * 991 + 10); // random int [10 - 1000]

const top = Math.floor(Math.random() * 991 + 10); // random int [10 - 1000]

$('#mydiv1').css({ 'left': `${left}px`, 'top': `${top}px` }); // styling the div 

});

I'm a bit confused with what you are trying to achieve, but from what I understand you need to do this: generate a random integer between 10 and 1000, and assign for each frame wrapper (here named 'frame' ) random left and top values.我对您要实现的目标有些困惑,但据我了解,您需要这样做:生成一个介于 10 到 1000 之间的随机integer ,并为每个帧包装器(此处命名为'frame' )随机分配lefttop值。

const getRandomInteger = (max = 1000, min = 10) => {
  return Math.round(Math.random() * (max - min) + min) + "px";
}

$(function () {
  $('.frame').each((index, element) => {
    $(element).css({top: getRandomInteger(), left: getRandomInteger()});
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM