JSFiddle - React, Tailwind, and code Playground
by Jonathan Nascimento
HTML
<div id="container">
<div id="pointer"></div>
</div>
CSS
#container {
background:url(https://www.google.com.au/images/srpr/logo4w.png) no-repeat center center;
/* background-attachment: fixed; */
-webkit-background-size:cover;
-moz-background-size:cover;
-o-background-size:cover;
background-size:cover;
height: 100vh;
width: 100%;
}
#pointer {
margin-left:-10px;
margin-top:-10px;
width:20px;
height:20px;
border-radius:10px;
background-color:#F00;
position: fixed;
}
JavaScript
var image = { width: 550, height: 190 };
var target = { x: 184, y: 88 };
var pointer = $('#pointer');
$(document).ready(updatePointer);
$(window).resize(updatePointer);
function updatePointer() {
var windowWidth = $(window).width();
var windowHeight = $(window).height();
// Get largest dimension increase
var xScale = windowWidth / image.width;
var yScale = windowHeight / image.height;
var scale;
var yOffset = 0;
var xOffset = 0;
if (xScale > yScale) {
// The image fits perfectly in x axis, stretched in y
scale = xScale;
yOffset = (windowHeight - (image.height * scale)) / 2;
} else {
// The image fits perfectly in y axis, stretched in x
scale = yScale;
xOffset = (windowWidth - (image.width * scale)) / 2;
}
pointer.css('top', (target.y) * scale + yOffset);
pointer.css('left', (target.x) * scale + xOffset);
}