0

I have seen a website and started thinking how to create a illustration of paper rocket moving in cubic beizer as in https://www.atlassian.com/

website. Help me do it with CSS on scroll. I tried the following code but failed.

$(function() {
    var rotation = 0, 
        scrollLoc = $(document).scrollTop();
    $(window).scroll(function() {
        var newLoc = $(document).scrollTop();
        var diff = scrollLoc - newLoc;
        rotation += diff, scrollLoc = newLoc;
        var rotationStr = "rotate(" + rotation + "deg)";
        $(".plane").css({
            "-webkit-transform": rotationStr,
            "-moz-transform": rotationStr,
            "transform": rotationStr
        });
    });
})
body {
  overflow-x: hidden;
}
.container {
  width: 100%;
  height: 1000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container" id="container">
    <img class="plane" src="https://wac-cdn.atlassian.com/dam/jcr:db49283c-bf71-4ec4-b14c-fbc36a121b2f/plane-2.png?cdnVersion=ll" alt="" data-0="transform: translate3d(0px, 0px, 0)" data-1600="transform: translate3d(1600px, 200px, 0)" class="skrollable skrollable-between" style="transform: translate3d(228px, 28.5px, 0px);" width="168" height="36"> 
</div>
Madhu
  • 13
  • 7

1 Answers1

1

If you want the plane to move around, rather than rotate, then you use use translate3d instead or rotation.

$(function() {

  function updateTranslation(el, x) {
    const y = (Math.cos(x*0.015) * 50) + 100;
    const tStr = `translate3d(${-x}px, ${y}px, 0px)`;

    $(".plane").css({
      "-webkit-transform": tStr,
      "-moz-transform": tStr,
      "transform": tStr
    });
  }
  var translation = 0,
    scrollLoc = $(document).scrollTop();

  updateTranslation($(".plane"), 0);

  $(window).scroll(function() {
    var newLoc = $(document).scrollTop();
    var diff = scrollLoc - newLoc;
    translation += diff, scrollLoc = newLoc;
    updateTranslation($(".plane"), translation);
  });
})
body {
  overflow-x: hidden;
}

.container {
  width: 100%;
  height: 1000px;
}

img {
  position: fixed;
 /* transition: transform 100ms ease;*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container" id="container">
  <img class="plane" src="https://wac-cdn.atlassian.com/dam/jcr:db49283c-bf71-4ec4-b14c-fbc36a121b2f/plane-2.png?cdnVersion=ll" alt="" width="168" height="36">
</div>
zero298
  • 25,467
  • 10
  • 75
  • 100