JSFiddle - React, Tailwind, and code Playground
HTML
<div class='v'></div>
<div class='h'></div>
CSS
.v, .h {
position: fixed;
background-color: red;
}
.v {
top: 0px;
bottom: 0px;
width: 3px;
}
.h {
left: 0px;
right: 0px;
height: 3px;
}
JavaScript
$(function(){
function sign(x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
var speed = 20;
var dx = speed * Math.random(), dy = speed * Math.random();
var $v = $('.v'), $h = $('.h');
var x = $h.offset().top, y = $v.offset().left;
setInterval(function(){
if (x + dx > $(window).width() || x + dx < 0)
dx = -sign(dx) * speed * Math.random();
if (y + dy > $(window).height() || y + dy < 0)
dy = -sign(dy) * speed * Math.random();
y = y + dy;
x = x + dx;
$h.offset({top: y});
$v.offset({left: x});
}, 20);
});