Parallax Tutorial - Scrolling Effect using CSS and Javascript

  Рет қаралды 112,357

freeCodeCamp.org

freeCodeCamp.org

6 жыл бұрын

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 KZfaq: / 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 жыл бұрын
I wish I watched this video 5 years ago.. Super simple and clearly explained. Kudos, author
@FlavioSantosdeAndrade
@FlavioSantosdeAndrade 5 жыл бұрын
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 жыл бұрын
Sorry, do not consider the "console.log" line...
@JP-ps8fb
@JP-ps8fb 5 жыл бұрын
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 жыл бұрын
I worked it out I didn't realise that the period referred to class rather than id
@gustavopacheco919
@gustavopacheco919 4 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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... 👌🏻👌🏻
@IshanKesharwani
@IshanKesharwani 4 жыл бұрын
It was so simple and I was banging my head for a simple explanation.
@ToboTheRibbler
@ToboTheRibbler 2 жыл бұрын
Straight to the point. Great tutorial. Thank you very much.
@stephanpetersen3600
@stephanpetersen3600 5 жыл бұрын
Perfect Tutorial, thanks a lot!
@eliasdorigoni
@eliasdorigoni 6 жыл бұрын
Crystal clear explanation. Thanks!
@lucasrsalazar
@lucasrsalazar 4 жыл бұрын
Very nice tutorial, well done in explaining the CSS and JS ways of doing!
@STUPIDYOUTUBE_HIDINGMSGS
@STUPIDYOUTUBE_HIDINGMSGS 3 жыл бұрын
Liked! Simple and easy! Thanks!
@JMSWV
@JMSWV 4 жыл бұрын
Thank you my friend! Easy as pie, great tutorial!
@ItzNea
@ItzNea 5 жыл бұрын
This is a very well explained video, thank you!!
@razaabbas3194
@razaabbas3194 6 жыл бұрын
thank you for the great video, very easy to understand and follow as well!
@ROC4Life96
@ROC4Life96 Ай бұрын
Great explanation. Very concise.
@a.s.7966
@a.s.7966 3 жыл бұрын
Awesome, thank you very much for this tutorial!
@yaldakarimi3772
@yaldakarimi3772 3 жыл бұрын
thank you! a simple and practical tutorial
@kleuner4622
@kleuner4622 11 ай бұрын
Thank you very much! Regards from Bavaria.
@scyfox.
@scyfox. 4 жыл бұрын
Just perfect. Love it
@TheChodex
@TheChodex 6 жыл бұрын
Thanks for video guys!
@shivanichauhan8203
@shivanichauhan8203 3 жыл бұрын
Very well explained ! Thankyou so much.
@jemkem915
@jemkem915 4 жыл бұрын
Great, easy to grasp. Keep it up
@gabreyes3149
@gabreyes3149 4 жыл бұрын
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 ❤️
@fk5404
@fk5404 2 жыл бұрын
thank you very much best tutorial out there
@spaceoverview4268
@spaceoverview4268 4 жыл бұрын
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 😉)
@hannan692
@hannan692 5 жыл бұрын
Finally a video about this that's easy to understand and with short code, thankyou!
@justingreen6561
@justingreen6561 Жыл бұрын
nice concise little demonstration...with an authentic little touch of some city ambience in the background. 🙂 🚗
@JoshuaMusau
@JoshuaMusau 5 жыл бұрын
I LOVE YOU!!!!!! Thanks for this. I have liked and subscribed already...
@megaimnot
@megaimnot Жыл бұрын
great tutorial :)
@philiporunor8557
@philiporunor8557 3 жыл бұрын
your just simply the best.............easy and simple
@DomTheTA
@DomTheTA 5 жыл бұрын
Thanks, very helpful!
@nosnart9453
@nosnart9453 6 жыл бұрын
awesome tutorial!
@rafik4722
@rafik4722 4 жыл бұрын
Great video thank you man
@kfstech
@kfstech 6 жыл бұрын
You explain that so well~~
@melosstudios8135
@melosstudios8135 Жыл бұрын
Very nice tutorial, the content also the presentation, well done
@gautamanand1201
@gautamanand1201 4 жыл бұрын
Your work is wow❤️
@deansolistino
@deansolistino 2 жыл бұрын
Thank you! I am now a huge fan! So clear!!!!!!!!!!!😇😇😇😇😇😇😇😇
@X-ONE_Highlights
@X-ONE_Highlights Жыл бұрын
thnks man ...what a nice explanation
@joseluisgarcia6251
@joseluisgarcia6251 4 жыл бұрын
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 жыл бұрын
OMG!!! Thanks so much! I watched other videos where it didn't work, but your method worked!!! Thanks very much!!
@joshbakit
@joshbakit 3 жыл бұрын
Thanks God. Thanks to you all guys there
@philipplohner5646
@philipplohner5646 4 жыл бұрын
well done tutorial!
@pauloabraga2
@pauloabraga2 2 жыл бұрын
Thank you very much ! CU
@yuvrajkumarshukla7789
@yuvrajkumarshukla7789 3 жыл бұрын
Nice tutorial
@FizzHxC
@FizzHxC 5 жыл бұрын
Cool implement! 📲
@jarryhaider8329
@jarryhaider8329 4 жыл бұрын
Great teacher❤️❤️💓💓
@daniesdb765
@daniesdb765 7 ай бұрын
thanks man.
@obeid_s
@obeid_s 6 жыл бұрын
Thank you 😊
@atulpandey0826
@atulpandey0826 4 жыл бұрын
very helpful video thanks a lot>>> :)
@BryanMutetiMusau
@BryanMutetiMusau 6 жыл бұрын
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
@i_am_a_real_cat1443
@i_am_a_real_cat1443 2 жыл бұрын
very cool bro
@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.
@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?
@huynhhuunhan6318
@huynhhuunhan6318 6 жыл бұрын
Thank a lot
@blackwhite214
@blackwhite214 2 жыл бұрын
Thanks
@TravelerSalauddin
@TravelerSalauddin 3 жыл бұрын
very very helpful, wish your success
@zes7215
@zes7215 2 жыл бұрын
wrr
@vipgames6290
@vipgames6290 4 жыл бұрын
thank you
@stevematin6050
@stevematin6050 4 жыл бұрын
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?
@anthonyarguello5401
@anthonyarguello5401 3 жыл бұрын
Have you find any solutions to this issue?
@senpai3594
@senpai3594 3 жыл бұрын
thanks
@legoguy23451
@legoguy23451 2 жыл бұрын
good tutorial good tutorial good tutorial
@calen4546
@calen4546 11 ай бұрын
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?
@chu1452
@chu1452 3 жыл бұрын
im searching for this for like 2 hours, thankyou 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"; })
@JohnHogue
@JohnHogue Жыл бұрын
How many Parallax effects on one page, in particular the effect of the content gliding over set images?
@donnchadhmcginley3153
@donnchadhmcginley3153 5 жыл бұрын
how do I do it for 2 or more elements on the page?
@dnevnikprogramista5704
@dnevnikprogramista5704 5 жыл бұрын
Спасибо)
@theaxacall
@theaxacall 3 жыл бұрын
Great
@bidondziura6753
@bidondziura6753 4 жыл бұрын
o man, perfect for me:)
@johnnyl7818
@johnnyl7818 5 жыл бұрын
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 3 жыл бұрын
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 🙏🏽
@remi2314
@remi2314 3 жыл бұрын
I did the exact same thing and structure but the parallax for the first div item just doesn't work :(
@kalainesanr5051
@kalainesanr5051 2 жыл бұрын
please kindly using the more video background in parallex, provide the tutorial
@user-qx7lb5qc9m
@user-qx7lb5qc9m 4 жыл бұрын
nice
@ExplorewithZac
@ExplorewithZac Жыл бұрын
This does not work when a background image is positioned center.
@mandaputtra
@mandaputtra 6 жыл бұрын
Thanks for video but you can simplify this thing by just background-fixed without any javascript. Every div can work as expected
@mandaputtra
@mandaputtra 6 жыл бұрын
Reynaldo Navedo yeah I agree.
@masaka222
@masaka222 6 жыл бұрын
Mobile devices also don't support this feature
@softonictech3755
@softonictech3755 5 жыл бұрын
great
@user-ph5xo4bi7s
@user-ph5xo4bi7s 2 жыл бұрын
Thanks1
@aunikahmed9332
@aunikahmed9332 5 жыл бұрын
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 5 жыл бұрын
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
@stefanodomeni
@stefanodomeni 5 жыл бұрын
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;
@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 😱
@zes7215
@zes7215 2 жыл бұрын
wrr
@ermirakajtazi5841
@ermirakajtazi5841 4 жыл бұрын
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"; })
@Tyfeen
@Tyfeen 4 жыл бұрын
3 years of university and now I discover how simple this is... I feel dumb
@gustavopacheco919
@gustavopacheco919 4 жыл бұрын
Don't feel bad. I did the college thing, graduated, and learned this on youtube. Hey, at least we got it, my friend.
@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
@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...
@marklewis1337
@marklewis1337 3 жыл бұрын
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
@SurajPal_15
@SurajPal_15 3 жыл бұрын
If we put parallax effect in middle of a page its not working properly pls help
@sumitraj4282
@sumitraj4282 Жыл бұрын
yep having same problem
@sidarjunful
@sidarjunful 3 жыл бұрын
how to do this in react.js?
@razaulkarim7323
@razaulkarim7323 3 жыл бұрын
const parallax = document.getElementById("banner"); window.addEventListener("scroll", function () { let offset = window.pageYOffset; parallax.style.backgroundPositionY = offset * 0.7 + "px"; })
@richardcastellanosb
@richardcastellanosb 5 жыл бұрын
util gracias
@anthonyarguello5401
@anthonyarguello5401 3 жыл бұрын
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;
@luigivampa9780
@luigivampa9780 5 жыл бұрын
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
@DynamiteProd
@DynamiteProd 2 жыл бұрын
assuming this doesn't work on ipad
@ayaanchopra5755
@ayaanchopra5755 4 жыл бұрын
Thnx...but he part did not work bro
@manuel5943
@manuel5943 5 жыл бұрын
Now div display: flex does not work anymore. You have to use flex instead of flex, inline-flex.
@maganabiyev2111
@maganabiyev2111 2 жыл бұрын
why no source code ;(
@ayaanchopra5755
@ayaanchopra5755 4 жыл бұрын
Js
@hasnathabdullah1253
@hasnathabdullah1253 3 жыл бұрын
this code just not runing correctly .
@hasnathabdullah1253
@hasnathabdullah1253 3 жыл бұрын
sorry code is runing correctly
@senkamatic8448
@senkamatic8448 Жыл бұрын
Best!!!
This Simple Trick Makes Your Website 83% Better Looking
10:57
Web Dev Simplified
Рет қаралды 382 М.
Memory Card Game - JavaScript Tutorial
34:16
freeCodeCamp.org
Рет қаралды 178 М.
MEGA BOXES ARE BACK!!!
08:53
Brawl Stars
Рет қаралды 35 МЛН
DO YOU HAVE FRIENDS LIKE THIS?
00:17
dednahype
Рет қаралды 74 МЛН
THEY WANTED TO TAKE ALL HIS GOODIES 🍫🥤🍟😂
00:17
OKUNJATA
Рет қаралды 18 МЛН
Неприятная Встреча На Мосту - Полярная звезда #shorts
00:59
Полярная звезда - Kuzey Yıldızı
Рет қаралды 7 МЛН
Simple Pure CSS Parallax Scroll Tutorial
26:36
Developer Filip
Рет қаралды 67 М.
Vanilla JavaScript Parallax with just a Few Lines of Code
23:15
DesignCourse
Рет қаралды 100 М.
Layered Parallax Scroll with React & Framer Motion
12:46
Tom Is Loading
Рет қаралды 42 М.
Subtle, yet Beautiful Scroll Animations
5:04
Beyond Fireship
Рет қаралды 1,6 МЛН
The Parallax Effect // 5 Minute WebDev Project
5:01
Fireship
Рет қаралды 600 М.
Top 10 CSS One Liners That Will Blow Your Mind
13:34
developedbyed
Рет қаралды 917 М.
CSS Tips And Tricks I Wish I Knew Before
12:12
Lama Dev
Рет қаралды 440 М.
Tutoriel JavaScript : Effet de parallaxe au défilement
35:27
Grafikart.fr
Рет қаралды 17 М.
Web Development Tutorial - JavaScript,  HTML, CSS - Rock Paper Scissors Game
1:27:17
Incredible scroll-based animations with CSS-only
32:23
Kevin Powell
Рет қаралды 378 М.
MEGA BOXES ARE BACK!!!
08:53
Brawl Stars
Рет қаралды 35 МЛН