How to create parallax effect like this?

前端 未结 4 644
盖世英雄少女心
盖世英雄少女心 2020-12-17 05:55

I\'ve been trying to get build a website with a parallax effect like the one on this website: http://www.sparksandhoney.com/the-open-agency-index/ or http://www.sparksandhon

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 06:17

    You can do this:

    .wraper
      width: 100%
      height: auto
    
    .box
      overflow: hidden
      position: relative
      width: 100%
      padding-bottom: 40%
    
    .object1
      position: absolute
      right: 15%
      top: 8%
      width: 13%
      height: 60%
      background:
        size: contain
        repeat: no-repeat
        image: url(https://openclipart.org/image/2400px/svg_to_png/213897/black-android-phone.png)
    

    You can add more objects if you like.

    Then in JS:

    $(window).scroll(function(){
      var curentPosition = Math.round($(this).scrollTop());
      console.log(curentPosition);
      $('.object1').css({
        'transform': 'translate(0px, -' + (curentPosition / 5) + '%)'
      });
    });
    

    Codepen: http://codepen.io/GlupiJas/pen/yOxRxG

    CSS only: http://codepen.io/GlupiJas/pen/BKqxyE

    Background Parallax: http://codepen.io/GlupiJas/pen/YqJdJg?editors=1010

    JS/JQUERY CLASS: http://codepen.io/GlupiJas/debug/YqJdJg

提交回复
热议问题