full Background image Mouse Move Coverage

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



full Background image Mouse Move Coverage



I have a small script to tilt the background image when mouse is moving. I have tried 3 different images and no matter what size they are there is a white gap when the image moves.



the background image follows the mouse without issue. is just shows white gaps , I have tried setting the image in every what to no avail.




$(function()
// Init
var container = document.getElementById("container"),
inner = document.getElementById("inner");
// Mouse
var mouse =
_x: 0,
_y: 0,
x: 0,
y: 0,
updatePosition: function(event) ,
setOrigin: function(e)
this._x = e.offsetLeft + Math.floor(e.offsetWidth / 2);
this._y = e.offsetTop + Math.floor(e.offsetHeight / 2);
,
show: function()
return "(" + this.x + ", " + this.y + ")";

;
// Track the mouse position relative to the center of the container.
mouse.setOrigin(container);
//-----------------------------------------
var counter = 0;
var updateRate = 10;
var isTimeToUpdate = function()
return counter++ % updateRate === 0;
;
//-----------------------------------------
var onMouseEnterHandler = function(event)
update(event);
;
var onMouseLeaveHandler = function()
inner.style = "";
;
var onMouseMoveHandler = function(event)
if (isTimeToUpdate())
update(event);

;
//-----------------------------------------
var update = function(event)
mouse.updatePosition(event);
updateTransformStyle(
(mouse.y / inner.offsetHeight / 2).toFixed(2),
(mouse.x / inner.offsetWidth / 2).toFixed(2)
);
;
var updateTransformStyle = function(x, y)
var style = "rotateX(" + x + "deg) rotateY(" + y + "deg)";
inner.style.transform = style;
inner.style.webkitTransform = style;
inner.style.mozTransform = style;
inner.style.msTransform = style;
inner.style.oTransform = style;
;
//-----------------------------------------
container.onmouseenter = onMouseEnterHandler;
container.onmouseleave = onMouseLeaveHandler;
container.onmousemove = onMouseMoveHandler;
);


body
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
color: #333;
font-size: 14px;
line-height: 20px;



.container
position: relative;
overflow: hidden;
-webkit-perspective: 50px;
perspective: 50px;



.inner
position: static;
display: block;
width: 100%;
height: 100vh;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
background-image: url('https://www.planwallpaper.com/static/images/6909249-black-hd-background.jpg');
background-position: 50% 50%;
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="container" class="container">
<div id="inner" class="inner"></div>
</div>





@Xufox thanks for that I was actually following a tutorial from a site.
– Codi
Aug 12 at 17:52





@Xufox I was talking about the type error I was following a tutorial here css-tricks.com/… and I dont know much about js thats why I put the () because it had them.
– Codi
Aug 12 at 18:00






@Xufox what I am trying to accomplish is this background effect videinfra.com everything works but the image show what gaps on the side when moving mouse the image is not covering everything
– Codi
Aug 12 at 18:01






But that’s just not the same syntax. You used $(function()), they used (function())(). In this case $(function()) is guaranteed to work.
– Xufox
Aug 12 at 18:08



$(function()


)


(function(


)


)(


)


$(function()


)





One part of the issue is that you need to set margin: 0; to body to remove the default margins. Another part is that you may want to scale the image to make it bigger than 100% width and height. Then the <div> needs to be repositioned somehow to center it.
– Xufox
Aug 12 at 18:30


margin: 0;


body


<div>




2 Answers
2



Your code updated from @Xufox's comments:




$(function()
// Init
var container = document.getElementById("container"),
inner = document.getElementById("inner");
// Mouse
var mouse =
_x: 0,
_y: 0,
x: 0,
y: 0,
updatePosition: function(event) ,
setOrigin: function(e)
this._x = e.offsetLeft + Math.floor(e.offsetWidth / 2);
this._y = e.offsetTop + Math.floor(e.offsetHeight / 2);
,
show: function()
return "(" + this.x + ", " + this.y + ")";

;
// Track the mouse position relative to the center of the container.
mouse.setOrigin(container);
//-----------------------------------------
var counter = 0;
var updateRate = 10;
var isTimeToUpdate = function()
return counter++ % updateRate === 0;
;
//-----------------------------------------
var onMouseEnterHandler = function(event)
update(event);
;
var onMouseLeaveHandler = function()
inner.style = "";
;
var onMouseMoveHandler = function(event)
if (isTimeToUpdate())
update(event);

;
//-----------------------------------------
var update = function(event)
mouse.updatePosition(event);
updateTransformStyle(
(mouse.y / inner.offsetHeight / 2).toFixed(2),
(mouse.x / inner.offsetWidth / 2).toFixed(2)
);
;
var updateTransformStyle = function(x, y)
var style = "rotateX(" + x + "deg) rotateY(" + y + "deg)";
inner.style.transform = style;
inner.style.webkitTransform = style;
inner.style.mozTransform = style;
inner.style.msTransform = style;
inner.style.oTransform = style;
;
//-----------------------------------------
container.onmouseenter = onMouseEnterHandler;
container.onmouseleave = onMouseLeaveHandler;
container.onmousemove = onMouseMoveHandler;
);


html, body margin:0 ;padding:0

body
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
color: #333;
font-size: 14px;
line-height: 20px;



.container
position: relative;
overflow: hidden;
-webkit-perspective: 50px;
perspective: 50px;



.inner
position: static;
display: block;

width: 120vw;
height: 120vh;
margin:-10vh 0 0 -10vw;
transition:.5s;


-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
background-image: url('https://www.planwallpaper.com/static/images/6909249-black-hd-background.jpg');
background-position: 50% 50%;
background-size: cover;
background-repeat: no-repeat;
bbackground-attachment: fixed;


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="container" class="container">
<div id="inner" class="inner"></div>
</div>





thanks for the help, is it possible to smooth out when mouse goes out of browser?
– Codi
Aug 12 at 20:42





@Codi, yes it's possible e.g. with transition property. I've updated my answer.
– Kosh Very
Aug 12 at 20:50


transition



Resizing the background picture won't help. Those gaps are caused by how your div.inner is rotated. Here is a simple graphical explanation:


div.inner



enter image description here



Possible solutions:



Scale div when rotated. You'll need to scale by (1 / cos(rotation_angle)). But that is not very good approach. It will not look nice enough.


(1 / cos(rotation_angle))



Scale div.inner once depending on maximum possible angle of rotation. Don't forget to shift it to the desired position with negative margins or position: absolute.


div.inner


position: absolute



Here is an example of how it works, based on your code. Note, that it is not full working code, I just modified width and margin. To get exactly what you want you need to set scale and positioning by yourself.




$(function()
// Init
var container = document.getElementById("container"),
inner = document.getElementById("inner");
// Mouse
var mouse =
_x: 0,
_y: 0,
x: 0,
y: 0,
updatePosition: function(event) ,
setOrigin: function(e)
this._x = e.offsetLeft + Math.floor(e.offsetWidth / 2);
this._y = e.offsetTop + Math.floor(e.offsetHeight / 2);
,
show: function()
return "(" + this.x + ", " + this.y + ")";

;
// Track the mouse position relative to the center of the container.
mouse.setOrigin(container);
//-----------------------------------------
var counter = 0;
var updateRate = 10;
var isTimeToUpdate = function()
return counter++ % updateRate === 0;
;
//-----------------------------------------
var onMouseEnterHandler = function(event)
update(event);
;
var onMouseLeaveHandler = function()
inner.style = "";
;
var onMouseMoveHandler = function(event)
if (isTimeToUpdate())
update(event);

;
//-----------------------------------------
var update = function(event)
mouse.updatePosition(event);
updateTransformStyle(
(mouse.y / inner.offsetHeight / 2).toFixed(2),
(mouse.x / inner.offsetWidth / 2).toFixed(2)
);
;
var updateTransformStyle = function(x, y)
var style = "rotateX(" + x + "deg) rotateY(" + y + "deg)";
inner.style.transform = style;
inner.style.webkitTransform = style;
inner.style.mozTransform = style;
inner.style.msTransform = style;
inner.style.oTransform = style;
;
//-----------------------------------------
container.onmouseenter = onMouseEnterHandler;
container.onmouseleave = onMouseLeaveHandler;
container.onmousemove = onMouseMoveHandler;
);


body
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
color: #333;
font-size: 14px;
line-height: 20px;



.container
position: relative;
overflow: hidden;
-webkit-perspective: 50px;
perspective: 50px;



.inner
position: static;
display: block;
width: 130%; margin-left: -40px;
height: 100vh;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
background-image: url('https://www.planwallpaper.com/static/images/6909249-black-hd-background.jpg');
background-position: 50% 50%;
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="container" class="container">
<div id="inner" class="inner"></div>
</div>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard