3

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) {
      var e = event || window.event;
      this.x = e.clientX - this._x;
      this.y = (e.clientY - this._y) * -1;
    },
    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>
Codi
  • 438
  • 3
  • 16
  • @Xufox thanks for that I was actually following a tutorial from a site. – Codi Aug 12 '18 at 17:52
  • @Xufox I was talking about the type error I was following a tutorial here https://css-tricks.com/animate-a-container-on-mouse-over-using-perspective-and-transform/ and I dont know much about js thats why I put the () because it had them. – Codi Aug 12 '18 at 18:00
  • @Xufox what I am trying to accomplish is this background effect https://www.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 '18 at 18:01
  • 1
    But that’s just not the same syntax. You used `$(function(){`…`})`, they used `(function(`…`){`…`})(`…`)`. In this case `$(function(){`…`})` is guaranteed to work. – Sebastian Simon Aug 12 '18 at 18:08
  • 1
    One part of the issue is that you need to set `margin: 0;` to `body` to remove the [default margins](https://stackoverflow.com/q/34550467/4642212). Another part is that you may want to scale the image to make it bigger than 100% width and height. Then the `
    ` needs to be repositioned somehow to center it.
    – Sebastian Simon Aug 12 '18 at 18:30
  • @Xufox I have tried all these things even used a 3000x3000 image tried every property in css i know same result. – Codi Aug 12 '18 at 18:37

2 Answers2

1

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

enter image description here

Possible solutions:

  1. 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.

  2. 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.

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) {
      var e = event || window.event;
      this.x = e.clientX - this._x;
      this.y = (e.clientY - this._y) * -1;
    },
    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>
Alex Yokisama
  • 1,021
  • 7
  • 8
1

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) {
      var e = event || window.event;
      this.x = e.clientX - this._x;
      this.y = (e.clientY - this._y) * -1;
    },
    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>
Kosh
  • 16,966
  • 2
  • 19
  • 34