Parallax Tutorial - Scrolling Effect using CSS and Javascript

Поделиться
HTML-код
  • Опубликовано: 6 авг 2018
  • Learn how to create a Parallax effect using CSS and Javascript. We are going to create two different types of parallax effects.
    Tutorial from GTCoding. Check out the GTCoding RUclips: / gtcoding
    --
    Learn to code for free and get a developer job: www.freecodecamp.org
    Read hundreds of articles on programming: medium.freecodecamp.org

Комментарии • 130

  • @galayder
    @galayder 2 года назад +11

    I wish I watched this video 5 years ago.. Super simple and clearly explained. Kudos, author

  • @FlavioSantosdeAndrade
    @FlavioSantosdeAndrade 5 лет назад +73

    First of all, Thanks, I've been looking forever for a simple tutorial like that. Who doesn't love parallax. But I noticed that that it only works to the first image. But I tried a few things and got to make it work in every image we wish the effect. Here's the code:
    const parallax = document.querySelectorAll(".parallax");
    window.addEventListener("scroll", function() {
    let offset = window.pageYOffset;
    parallax.forEach(function(prllx, i) {
    console.log("Parallax " + i + ": " + prllx.offsetTop + " / Window: " + offset);
    prllx.style.backgroundPositionY = (offset - prllx.offsetTop) * 0.7 + "px";
    })
    })

    • @FlavioSantosdeAndrade
      @FlavioSantosdeAndrade 5 лет назад +3

      Sorry, do not consider the "console.log" line...

    • @JP-ps8fb
      @JP-ps8fb 5 лет назад +3

      this needs to be at the top! exactly what i needed thank you sir

    • @stefanodomeni
      @stefanodomeni 5 лет назад

      Thanks man!

    • @graememackenzie4257
      @graememackenzie4257 4 года назад

      I tried this code and it didn't work for me, not sure what it is am I doing wrong? any suggestions

    • @graememackenzie4257
      @graememackenzie4257 4 года назад +1

      I worked it out I didn't realise that the period referred to class rather than id

  • @gustavopacheco919
    @gustavopacheco919 4 года назад +5

    Awesome...awesome tutorial. No query, no plugins, just plain old javascript. I take/took courses on Udemy and all that, this was by far the best little tutorial I've seen in a long, long time. This right here is why I love to practice web design. Thank you.

  • @lynguist
    @lynguist 4 года назад +3

    i watched your tutorial on wordpress theme devolopement 4 times by now - you helped me so much!
    and now this is so simple, well explained, concise and just perfect as well.
    Also it is so funny how i am sitting here at my desk in germany in the morning (it is not even full daylight and there is no sound outside what so ever) and listening to all the foreign traffic sounds in the background - always makes me laugh and wonder thinking about the different worlds we are propably living in - and get connect via such an abstract thing as parallax via js.
    In short: Thank you very much!

  • @lokeshlokie
    @lokeshlokie 5 лет назад +1

    I have watched lot of videos for this effect, but u made my day rock with simple and very means very less css code..... Awesome tutorial... 👌🏻👌🏻

  • @eliasdorigoni
    @eliasdorigoni 5 лет назад

    Crystal clear explanation. Thanks!

  • @ToboTheRibbler
    @ToboTheRibbler 2 года назад

    Straight to the point. Great tutorial. Thank you very much.

  • @IshanKesharwani
    @IshanKesharwani 3 года назад +1

    It was so simple and I was banging my head for a simple explanation.

  • @stephanpetersen3600
    @stephanpetersen3600 4 года назад +2

    Perfect Tutorial, thanks a lot!

  • @gabreyes3149
    @gabreyes3149 4 года назад +3

    Learned so much in just 10min! You rock dude! Thankyou for thiss vid ❤️❤️❤️
    Btw i have a question. Can i use a video instead of image in the parallax background?? Thankyouu ❤️

  • @lucasrsalazar
    @lucasrsalazar 4 года назад

    Very nice tutorial, well done in explaining the CSS and JS ways of doing!

  • @ItzNea
    @ItzNea 5 лет назад +1

    This is a very well explained video, thank you!!

  • @JMSWV
    @JMSWV 4 года назад

    Thank you my friend! Easy as pie, great tutorial!

  • @razaabbas3194
    @razaabbas3194 5 лет назад

    thank you for the great video, very easy to understand and follow as well!

  • @spaceoverview4268
    @spaceoverview4268 4 года назад +1

    Perfect tuto and explanations. Very simple way for this effect but not for responsive. Thx a lot man. (if you have a responsive solution I take it 😉)

  • @higherpurpose1212
    @higherpurpose1212 3 года назад

    Liked! Simple and easy! Thanks!

  • @yaldakarimi3772
    @yaldakarimi3772 3 года назад

    thank you! a simple and practical tutorial

  • @a.s.7966
    @a.s.7966 3 года назад

    Awesome, thank you very much for this tutorial!

  • @TheChodex
    @TheChodex 5 лет назад

    Thanks for video guys!

  • @jemkem915
    @jemkem915 4 года назад

    Great, easy to grasp. Keep it up

  • @scyfox.
    @scyfox. 3 года назад +1

    Just perfect. Love it

  • @ROC4Life96
    @ROC4Life96 11 дней назад

    Great explanation. Very concise.

  • @philiporunor8557
    @philiporunor8557 3 года назад

    your just simply the best.............easy and simple

  • @JoshuaMusau
    @JoshuaMusau 4 года назад

    I LOVE YOU!!!!!! Thanks for this. I have liked and subscribed already...

  • @shivanichauhan8203
    @shivanichauhan8203 3 года назад

    Very well explained ! Thankyou so much.

  • @DomTheTA
    @DomTheTA 5 лет назад

    Thanks, very helpful!

  • @nosnart9453
    @nosnart9453 5 лет назад

    awesome tutorial!

  • @kfstech
    @kfstech 5 лет назад

    You explain that so well~~

  • @gautamanand1201
    @gautamanand1201 4 года назад

    Your work is wow❤️

  • @kleuner4622
    @kleuner4622 10 месяцев назад

    Thank you very much! Regards from Bavaria.

  • @BryanMutetiMusau
    @BryanMutetiMusau 5 лет назад +1

    Are there any browser compatibility issues using this method as compared to using Jquery or other libraries?? Otherwise, thanks for the awesome tutorial loved it

  • @justingreen6561
    @justingreen6561 Год назад

    nice concise little demonstration...with an authentic little touch of some city ambience in the background. 🙂 🚗

  • @rafik4722
    @rafik4722 4 года назад

    Great video thank you man

  • @joshbakit
    @joshbakit 3 года назад

    Thanks God. Thanks to you all guys there

  • @melosstudios8135
    @melosstudios8135 Год назад

    Very nice tutorial, the content also the presentation, well done

  • @philipplohner5646
    @philipplohner5646 4 года назад

    well done tutorial!

  • @deansolistino
    @deansolistino 2 года назад

    Thank you! I am now a huge fan! So clear!!!!!!!!!!!😇😇😇😇😇😇😇😇

  • @joseluisgarcia6251
    @joseluisgarcia6251 4 года назад +1

    does anybody know how to add text below the "DIV 1" section withouot breaking the parallax effect? i tried
    insert text
    but the effect breaks when it is out of the tag and throws all the other out of order. when i put the tag inside the div the text appears to the side of the text and not below even when i try to break it with

  • @milap8989
    @milap8989 4 года назад +1

    OMG!!! Thanks so much! I watched other videos where it didn't work, but your method worked!!! Thanks very much!!

  • @fk5404
    @fk5404 2 года назад

    thank you very much best tutorial out there

  • @jarryhaider8329
    @jarryhaider8329 3 года назад

    Great teacher❤️❤️💓💓

  • @hannan692
    @hannan692 5 лет назад +1

    Finally a video about this that's easy to understand and with short code, thankyou!

  • @FizzHxC
    @FizzHxC 5 лет назад

    Cool implement! 📲

  • @svm4754
    @svm4754 Год назад

    thnks man ...what a nice explanation

  • @atulpandey0826
    @atulpandey0826 4 года назад

    very helpful video thanks a lot>>> :)

  • @megaimnot
    @megaimnot Год назад

    great tutorial
    :)

  • @obeid_s
    @obeid_s 5 лет назад

    Thank you 😊

  • @stevematin6050
    @stevematin6050 3 года назад +1

    Very simple and helpful, but how do you make it less jittery if you're using a large background image or have a lot of content?

  • @TMFitachi
    @TMFitachi 2 года назад

    Awesome! Made my webpage look so much better. Could anyone help me with just one thing? Is there a way to disable this specific code for mobile devices?

  • @froststorm77
    @froststorm77 5 лет назад

    Still haw questions, with this function I used to make new function for each Id, because getElementByClass returns array, so how to make it for class ? And strange thing , I have exact same code and my parallax moves in backward direction so I added -before multiplier ? but still have unanswered questions.

  • @jakestoneamuzant
    @jakestoneamuzant 4 года назад

    How do i make this work on only specific sections ( section ID="") etc. This tutorial is only for 4 specific DIVs, which isn't as convenient when working with multiple sections that are prebuilt. When I start this tutorial and get to the ( div:nth-child(1) ) , this changes every div background in my document.

  • @yuvrajkumarshukla7789
    @yuvrajkumarshukla7789 3 года назад

    Nice tutorial

  • @johnnyl7818
    @johnnyl7818 4 года назад +1

    Thank you so much for sharing this amazing tutorial :) I was wondering how can I also make the background image not to repeat itself while scrolling? I once tried to place the parallax div in the middle of the page, however, when I apply the js to it, the background image always repeats itself, and when I set background-repeat: no-repeat in the CSS file. The background then disappears :(

    • @mrtvakOnaj
      @mrtvakOnaj 2 года назад

      Same :( Did you find the solution

    • @alishahrose2076
      @alishahrose2076 2 года назад

      Try this:
      parallax.style.backgroundPositionY = (offset - parallax.offsetTop) * 0.7 + "px";

    • @mrtvakOnaj
      @mrtvakOnaj Год назад

      @C7_ GHoST_YT damn sure i did but i don't remember. I just remember that it was something simple and stupid. Sorry for not being much of a help, but i hope you find your answer 🙏🏽

  • @pauloabraga2
    @pauloabraga2 2 года назад

    Thank you very much ! CU

  • @huynhhuunhan6318
    @huynhhuunhan6318 5 лет назад

    Thank a lot

  • @TravelerSalauddin
    @TravelerSalauddin 3 года назад

    very very helpful, wish your success

  • @daniesdb765
    @daniesdb765 6 месяцев назад

    thanks man.

  • @bidondziura6753
    @bidondziura6753 4 года назад

    o man, perfect for me:)

  • @vipgames6290
    @vipgames6290 4 года назад

    thank you

  • @i_am_a_real_cat1443
    @i_am_a_real_cat1443 2 года назад

    very cool bro

  • @1DiSun
    @1DiSun 4 года назад

    did the same as in the tutorial but I got an error of 'const'. It says:
    "expected an identifier and instead saw 'const"
    and
    "Error: Parsing error: The keyword 'const' is reserved"
    I tried to put it online and it works despite the error but will the error have an influence on my website after a certain amount of time?
    Can you please help me remove that error?
    My code in the 'js-file':
    const parallax = document.getElementById("frontpage");
    window.addEventListener("scroll",function()
    {
    let offset=window.pageYOffset;
    parallax.style.backgroundPositionY = offset * 0.7 + "px";
    })

  • @dnevnikprogramista5704
    @dnevnikprogramista5704 5 лет назад

    Спасибо)

  • @calen4546
    @calen4546 10 месяцев назад

    I've tried using this for my project using your div 1 and 2 only. However, the image seems to immediately zoom in when scrolled and gets stuck at that area unless the page has beem refreshed. Only then, will it display the full image. Would you happen to know what went wrong with my coding?

  • @donnchadhmcginley3153
    @donnchadhmcginley3153 5 лет назад

    how do I do it for 2 or more elements on the page?

  • @JohnHogue
    @JohnHogue Год назад

    How many Parallax effects on one page, in particular the effect of the content gliding over set images?

  • @aunikahmed9332
    @aunikahmed9332 5 лет назад +2

    How could you also create the same parallax effect for the second picture with making the picture repeat when you scroll down?

    • @FlavioSantosdeAndrade
      @FlavioSantosdeAndrade 5 лет назад

      I had the same question, but solved that. You just need to reset each image offsetTop to the scrolling.
      const parallax = document.querySelectorAll(".parallax");
      window.addEventListener("scroll", function() {
      let offset = window.pageYOffset;
      parallax.forEach(function(prllx, i) {
      console.log("Parallax " + i + ": " + prllx.offsetTop + " / Window: " + offset);
      prllx.style.backgroundPositionY = (offset - prllx.offsetTop) * 0.7 + "px";
      })
      })

    • @FlavioSantosdeAndrade
      @FlavioSantosdeAndrade 5 лет назад

      Sorry, do not consider the "console.log" line... :D

    • @JoshuaMusau
      @JoshuaMusau 4 года назад

      Go to the css for that div and add the following: background-repeat: no-repeat;

    • @sumitraj4282
      @sumitraj4282 Год назад

      @@FlavioSantosdeAndrade bro I have tried your code it works but sometimes images just out of the place

  • @legoguy23451
    @legoguy23451 2 года назад

    good tutorial good tutorial good tutorial

  • @blackwhite214
    @blackwhite214 2 года назад

    Thanks

  • @softonictech3755
    @softonictech3755 5 лет назад

    great

  • @mandaputtra
    @mandaputtra 5 лет назад +1

    Thanks for video but
    you can simplify this thing by just background-fixed without any javascript. Every div can work as expected

    • @mandaputtra
      @mandaputtra 5 лет назад

      Reynaldo Navedo yeah I agree.

    • @masaka222
      @masaka222 5 лет назад

      Mobile devices also don't support this feature

  • @remi2314
    @remi2314 3 года назад

    I did the exact same thing and structure but the parallax for the first div item just doesn't work :(

  • @JoEx2k11
    @JoEx2k11 4 года назад

    I have a non related question... How are your code suggestions so fast? For mine I have to wait a bit (sometimes more) before they show up 😱

  • @senpai3594
    @senpai3594 3 года назад

    thanks

  • @stefanodomeni
    @stefanodomeni 5 лет назад +1

    How do you get this to work for 2 image on the same page?

    • @harukogaki
      @harukogaki 5 лет назад

      in this tutorial he applied the parallax effect to the dom elements with the parallax id. So maybe if you would want to have the same effect to work on multiple elements then instead of using id as a selector use a more common selector? class? element type?

  • @DiviTivi
    @DiviTivi 3 года назад

    My background always jumps right at the beginning when I start scrolling and then works fine...any suggestions how to get rid of that jump at the beginning?

    • @ivanyosifov2629
      @ivanyosifov2629 3 года назад

      you probably gave it background-position: center.

    • @sumitraj4282
      @sumitraj4282 Год назад

      don't give background-position: center;

  • @user-qx7lb5qc9m
    @user-qx7lb5qc9m 4 года назад

    nice

  • @The_Drewid
    @The_Drewid 5 лет назад

    I keep getting "Cannot set property 'backgroundPositionY' of undefined" whenever I try the parallax. What's going on?

    • @freecodecamp
      @freecodecamp  5 лет назад

      Do you have a link to your code?

    • @kogarashi9413
      @kogarashi9413 5 лет назад

      1.) did you set the Id?
      2.) did you link the js at the end of the body?

    • @emmanuelojong225
      @emmanuelojong225 4 года назад

      copy the first line of code that grabs the id at document.getElementById into the function that should fix the problem

  • @theaxacall
    @theaxacall 3 года назад

    Great

  • @Tyfeen
    @Tyfeen 4 года назад +5

    3 years of university and now I discover how simple this is... I feel dumb

    • @gustavopacheco919
      @gustavopacheco919 4 года назад +2

      Don't feel bad. I did the college thing, graduated, and learned this on youtube. Hey, at least we got it, my friend.

  • @kalainesanr5051
    @kalainesanr5051 2 года назад

    please kindly using the more video background in parallex, provide the tutorial

  • @user-ph5xo4bi7s
    @user-ph5xo4bi7s 2 года назад

    Thanks1

  • @chu1452
    @chu1452 3 года назад

    im searching for this for like 2 hours, thankyou bro

  • @zac_vaughn
    @zac_vaughn Год назад

    This does not work when a background image is positioned center.

  • @SurajPal_15
    @SurajPal_15 3 года назад

    If we put parallax effect in middle of a page its not working properly pls help

  • @richardcastellanosb
    @richardcastellanosb 5 лет назад

    util gracias

  • @razaulkarim7323
    @razaulkarim7323 3 года назад

    const parallax = document.getElementById("banner");
    window.addEventListener("scroll", function () {
    let offset = window.pageYOffset;
    parallax.style.backgroundPositionY = offset * 0.7 + "px";
    })

  • @ermirakajtazi5841
    @ermirakajtazi5841 4 года назад +2

    index.html----------------
    Div 1
    Div 2
    Div 3
    Div 4
    CSS-------------
    * {
    border: 0;
    padding: 0;
    margin: 0;
    }
    div {
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    width: 100%;
    height: 100vh;
    }
    h1 {
    font-size: 60px;
    color: black;
    padding: 8px 24px;
    border-radius: 16px;
    }
    div:nth-child(1) {
    background-image: url(../images/1.jpg);
    background-size: cover;
    }
    div:nth-child(2) {
    background: brown;
    }
    div:nth-child(3) {
    height: 400px;
    background-image: url(../images/4.jpg);
    background-size: cover;
    background-attachment: fixed;
    }
    div:nth-child(4) {
    background: aquamarine;
    }
    main.js---------------------------
    const parallax=document.getElementById("parallax");
    window.addEventListener("scroll",function(){
    let offset = window.pageYOffset;
    parallax.style.backgroundPositionY=offset * 0.7 + "px";
    })

  • @sidarjunful
    @sidarjunful 3 года назад

    how to do this in react.js?

  • @themiataboy909
    @themiataboy909 Год назад

    the parallax doesn't work. i've copied the code letter by letter aside from the font and the image, and everything else works except for the top parallax. for reference i use chrome, but it doesn't work on explorer either.

    • @sumitraj4282
      @sumitraj4282 Год назад

      have you linked your files properly,
      give me your code link maybe i can help...

  • @luigivampa9780
    @luigivampa9780 5 лет назад +1

    Thanks for tutorial. However you should be more consistent- You are using both single and double quotes for strings etc. Parallax although very easy to implement can ruin website’s performance

  • @anthonyarguello5401
    @anthonyarguello5401 3 года назад +1

    Anyone having issues with a choopy scrollling effect?

    • @sumitraj4282
      @sumitraj4282 Год назад

      it's probably because of your images size are too large, resize and compress them according to your use..
      and don't give background-position: center;

  • @marklewis1337
    @marklewis1337 2 года назад

    I had to use nth of type, not nth child, to get it to assign the right image or colour to the right div, not sure why... I copied exactly, it always treated div 1 as the third child. My understanding of it maybe poor, like why is the first div the Parent? I could beleive it treats maybe head or body as the parent, but that clearly didn't happen for the tut and I didn't see anyone else report it

  • @DynamiteProd
    @DynamiteProd 2 года назад

    assuming this doesn't work on ipad

  • @maganabiyev2111
    @maganabiyev2111 Год назад

    why no source code ;(

  • @manuel5943
    @manuel5943 5 лет назад

    Now div display: flex does not work anymore. You have to use flex instead of flex, inline-flex.

  • @ayaanchopra5755
    @ayaanchopra5755 4 года назад

    Thnx...but he part did not work bro

  • @ayaanchopra5755
    @ayaanchopra5755 4 года назад

    Js

  • @hasnathabdullah1253
    @hasnathabdullah1253 3 года назад

    this code just not runing correctly .

  • @senkamatic8448
    @senkamatic8448 Год назад

    Best!!!