Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to keep a random occurring botton inside the body(Or inside any div)

I Made a button using html, css and js which occurs randomly on the page.But i want the button inside the body tag and it keeps getting out.

HTML CODE:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="main.js" defer></script>
    <link rel="stylesheet" href="style.css">
    <title>SReflex</title>
</head>
<body>
    <button class="random">1 </button>
    
    
</body>
</html>

CSS CODE:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

*{
    margin:0%;
    padding:0%;
}

body{
    height: 100vh;
    width: 100vw;
    position: fixed;

}

button.random {

    /* looks of the button */
    height: 3rem;
    width: 3rem;
    border: black;
    border-style: solid;
    border-radius: 50%;

  
    display:block;
    position:absolute;
}

JS CODE:

let temp= document.querySelector(".random");
temp.addEventListener("click",change);

function change(){
    let posx = Math.floor(Math.random()*1000);
    let posy = Math.floor(Math.random()*1000);
    let posz = Math.floor(Math.random()*1000);
    temp.style.left= posx + "vw";
    temp.style.top= posy+ "vh";
    temp.style.right= posz+ "vw";
}

How do i make the button to not go off screen, i want the page to not include the scroll bar.

>Solution :

Here is an easy way to do so, it works for the body or for a containing div because it’s related to parent size.
check out the snippet.

let temp= document.querySelector(".random");
temp.addEventListener("click",change);
let maxw = temp.parentElement.clientWidth - 50;
let maxh = temp.parentElement.clientHeight - 50;
function change(){
    let posx = Math.floor(Math.random() * (maxw));
    let posy = Math.floor(Math.random() * (maxh))
    temp.style.left= posx + "px";
    temp.style.top= posy+ "px";
}
*{
    margin:0%;
    padding:0%;
}

body{
    height: 100vh;
    width: 100vw;
    position: fixed;

}

button.random {

    /* looks of the button */
    height: 3rem;
    width: 3rem;
    border: black;
    border-style: solid;
    border-radius: 50%;

  
    display:block;
    position:absolute;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="main.js" defer></script>
    <link rel="stylesheet" href="style.css">
    <title>SReflex</title>
</head>
<body>
    <button class="random">1 </button>
    
    
</body>
</html>

the -50 is to make sure it doesn’t cross the edges, you can edit that to suit your needs.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading