0

my goal is to make a webpage where multiple div's with text etc. within, moves on mouse x and y. It should looks like a parallax effect on mouse x and y. All the parallax plugins that i came across are image based so they don't work with the div's. I wrote a little js myself but the div's are jumping all over the page, no css can keep em' in place. Maybe someone with more js experience knows how I can solve this. below the code. hope toe hear from you all.

HTML:

<div class = "main" id ="scene">

    <div class = "container" id = "home">

      <div class = "kop "> HOME </div>

      <div class = "brood "> 
        <p>
          Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia.
        </p>
      </div>

    </div> <!-- end container home -->


    <div class = "container" id = "visie">

      <div class = "kop"> VISIE </div>
      <div class = "brood"> 
        <p>
          Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.
        </p>
      </div>

    </div>
</div>

CSS:

.container{
    margin-right: 20%;
    margin-top: 200px;
    margin-bottom: 200px;
    margin-left: 20%;

    font-size: 16px;

    background-color: yellow;

}

.kop{
    font-family: GTWBold;

}

.brood{
    font-family: GTWMedium;

}


#home{
    margin-top: 20px;

}


#visie{
    margin-right: 40%;

}

JS:

$('#scene2').mousemove(function(e){
    var x = -(e.pageX + this.offsetLeft) / 20;
    var y = -(e.pageY + this.offsetTop) / 20;

    var h = document.getElementById('home');
    h.style.position = "absolute";
    h.style.left = x+'px';
    h.style.top = y+'px';   
    h.style.width = "500px";

});
Bart
  • 5
  • 4
  • I'm guessing you're referring to `#visie` moving when you're mousing over `#scene`? it's because `position: absolute` takes it out of the normal flow. – Jorg Mar 13 '17 at 23:02

1 Answers1

0

position: absolute removes an element from the normal flow of elements, which causes everything else to shift. Also, setting width after rendering causes some shifts because here too you're manipulating the height at which #visie is displayed.

Try this:

$('#scene').mousemove(function(e){
  var x = -(e.pageX + this.offsetLeft) / 20;
  var y = -(e.pageY + this.offsetTop) / 20;

  var h = document.getElementById('home');
  h.style.position = "relative";
  h.style.left = x+'px';
  h.style.top = y+'px';
});

Also here: https://jsfiddle.net/pqur9wxa/1/

Here, they're both moving: https://jsfiddle.net/pqur9wxa/2/

Jorg
  • 7,219
  • 3
  • 44
  • 65
  • Thanks!! this is what I need. I will post the url when it's finished, in case you are interested. – Bart Mar 17 '17 at 12:54
  • Sure, curious to see how parallax text boxes work out :) – Jorg Mar 17 '17 at 13:33
  • @Bart nice! thanks for letting me know how it went :D making it work on mobile would be the first thing I'd do. probably disable the full screen paging based on screen size. Did you intend to put the parallax effect on the page headers? – Jorg May 31 '17 at 04:21
  • no I was using the parallax on the div called #intro basically all the tekst and headers except the menu, and on the background in the opposite direction. but after all we managed to make a sort of fixed page so we have max contol over positioning text on the image. thats also why we used the fullpage scroll and the 'sliding' text block. ps i deleted my previous post because it was a no 1 google search hit when i googled the name of the website – Bart Jun 11 '17 at 21:38