I want to create a button "Skip Intro", similar to the one on Netflix (but with a ridiculous part where a button changes its location randomly as soon as you hover over it). The code for changing to a random location is already done. But I want to keep the location of the button only limited to the inside of a video frame. How can I do this?
var button = document.querySelector(".button");
function randomizePosition() {
var randomX = Math.random() * window.innerWidth;
var randomY = Math.random() * window.innerHeight;
console.log("randomX", randomX, " : randomY", randomY)
button.style.left = randomX + "px";
button.style.top = randomY + "px";
}
randomizePosition();
button.addEventListener("mouseenter", (event) => {
randomizePosition();
});
.button {
stroke: 3px;
display: inline-block;
padding: 20px;
//border-radius: 5px;
position: fixed;
font-family: Arial, Helvetica, sans-serif;
border: solid 3px darkgrey;
}
<div class='button'>SKIP INTRO</div>
<iframe width="560" height="315" src="https://www.youtube.com/embed/g_4aEFYj3QA" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"></iframe>
Here is a link to my work: https://codepen.io/artfsch/pen/ZEjZLgw
I know that it’s possible in JS to limit the variable to a range and set it to be only randomized within a specific range. Unfortunately, I don’t know how to implement it.
2
Answers
Try This
Assuming you want to make the video full-screen, you can accomplish this by limiting the possible new locations for the button — basically creating artificial padding.
I’ve done this by centering your button on its top/left position:
and creating a padding of 100px around your window
https://codepen.io/QuiteQuinn/pen/YzOZjgE