Тёмный

Parallax Tutorial - Scrolling Effect using CSS and Javascript 

freeCodeCamp.org
Подписаться 10 млн
Просмотров 113 тыс.
50% 1

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 RU-vid: / gtcoding
--
Learn to code for free and get a developer job: www.freecodeca...
Read hundreds of articles on programming: medium.freecod...

Опубликовано:

 

26 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 132   
@galayder
@galayder 2 года назад
I wish I watched this video 5 years ago.. Super simple and clearly explained. Kudos, author
@fsassiv
@fsassiv 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"; }) })
@fsassiv
@fsassiv 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 5 лет назад
I tried this code and it didn't work for me, not sure what it is am I doing wrong? any suggestions
@graememackenzie4257
@graememackenzie4257 5 лет назад
I worked it out I didn't realise that the period referred to class rather than id
@imanqoly
@imanqoly 2 месяца назад
This is best and most straight forward parallax tut I've ever seen
@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 5 лет назад
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 6 лет назад
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.
@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 ❤️
@ROC4Life96
@ROC4Life96 4 месяца назад
Great explanation. Very concise.
@kleuner4622
@kleuner4622 Год назад
Thank you very much! Regards from Bavaria.
@justingreen6561
@justingreen6561 2 года назад
nice concise little demonstration...with an authentic little touch of some city ambience in the background. 🙂 🚗
@philiporunor8557
@philiporunor8557 3 года назад
your just simply the best.............easy and simple
@ToboTheRibbler
@ToboTheRibbler 2 года назад
Straight to the point. Great tutorial. Thank you very much.
@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 😉)
@stephanpetersen3600
@stephanpetersen3600 5 лет назад
Perfect Tutorial, thanks a lot!
@fk5404
@fk5404 3 года назад
thank you very much best tutorial out there
@melosstudiosEt
@melosstudiosEt 2 года назад
Very nice tutorial, the content also the presentation, well done
@yaldakarimi3772
@yaldakarimi3772 3 года назад
thank you! a simple and practical tutorial
@shivanichauhan8203
@shivanichauhan8203 3 года назад
Very well explained ! Thankyou so much.
@lucasrsalazar
@lucasrsalazar 4 года назад
Very nice tutorial, well done in explaining the CSS and JS ways of doing!
@ItzNea
@ItzNea 5 лет назад
This is a very well explained video, thank you!!
@hannan692
@hannan692 6 лет назад
Finally a video about this that's easy to understand and with short code, thankyou!
@scyfox.
@scyfox. 4 года назад
Just perfect. Love it
@joshbakit
@joshbakit 3 года назад
Thanks God. Thanks to you all guys there
@joseluisgarcia6251
@joseluisgarcia6251 5 лет назад
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
@X-ONE_Highlights
@X-ONE_Highlights Год назад
thnks man ...what a nice explanation
@jemkem915
@jemkem915 4 года назад
Great, easy to grasp. Keep it up
@rohanvania6059
@rohanvania6059 21 день назад
Best explanation
@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
@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?
@razaabbas3194
@razaabbas3194 6 лет назад
thank you for the great video, very easy to understand and follow as well!
@milap8989
@milap8989 4 года назад
OMG!!! Thanks so much! I watched other videos where it didn't work, but your method worked!!! Thanks very much!!
@yuvrajkumarshukla7789
@yuvrajkumarshukla7789 3 года назад
Nice tutorial
@megaimnot
@megaimnot 2 года назад
great tutorial :)
@jarryhaider8329
@jarryhaider8329 4 года назад
Great teacher❤️❤️💓💓
@a.s.7966
@a.s.7966 3 года назад
Awesome, thank you very much for this tutorial!
@JMSWV
@JMSWV 4 года назад
Thank you my friend! Easy as pie, great tutorial!
@gautamanand1201
@gautamanand1201 4 года назад
Your work is wow❤️
@steve_matin
@steve_matin 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?
@agamemnon-Fr
@agamemnon-Fr 2 года назад
Thank you very much ! CU
@TravelerSalauddin
@TravelerSalauddin 4 года назад
very very helpful, wish your success
@zes7215
@zes7215 2 года назад
wrr
@JohnHogue
@JohnHogue Год назад
How many Parallax effects on one page, in particular the effect of the content gliding over set images?
@kalainesanr5051
@kalainesanr5051 2 года назад
please kindly using the more video background in parallex, provide the tutorial
@daniesdb765
@daniesdb765 10 месяцев назад
thanks man.
@rafik4722
@rafik4722 5 лет назад
Great video thank you man
@blackwhite214
@blackwhite214 2 года назад
Thanks
@JoshuaMusau
@JoshuaMusau 5 лет назад
I LOVE YOU!!!!!! Thanks for this. I have liked and subscribed already...
@calen4546
@calen4546 Год назад
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?
@philipplohner5646
@philipplohner5646 4 года назад
well done tutorial!
@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"; })
@legoguy23451
@legoguy23451 3 года назад
good tutorial good tutorial good tutorial
@i_am_a_real_cat1443
@i_am_a_real_cat1443 3 года назад
very cool bro
@projectDominic
@projectDominic 6 лет назад
Thanks, very helpful!
@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.
@kfstech
@kfstech 6 лет назад
You explain that so well~~
@ExplorewithZac
@ExplorewithZac Год назад
This does not work when a background image is positioned center.
@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.
@aunikahmed9332
@aunikahmed9332 6 лет назад
How could you also create the same parallax effect for the second picture with making the picture repeat when you scroll down?
@fsassiv
@fsassiv 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"; }) })
@fsassiv
@fsassiv 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 Год назад
@@fsassiv bro I have tried your code it works but sometimes images just out of the place
@FizzHxC
@FizzHxC 6 лет назад
Cool implement! 📲
@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
@obeid_s
@obeid_s 6 лет назад
Thank you 😊
@vipgames6290
@vipgames6290 4 года назад
thank you
@remi2314
@remi2314 3 года назад
I did the exact same thing and structure but the parallax for the first div item just doesn't work :(
@TheChodex
@TheChodex 6 лет назад
Thanks for video guys!
@1DiSun
@1DiSun 5 лет назад
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"; })
@atulpandey0826
@atulpandey0826 4 года назад
very helpful video thanks a lot>>> :)
@huynhhuunhan6318
@huynhhuunhan6318 6 лет назад
Thank a lot
@chu1452
@chu1452 3 года назад
im searching for this for like 2 hours, thankyou bro
@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
@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;
@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.
@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?
@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 3 года назад
Try this: parallax.style.backgroundPositionY = (offset - parallax.offsetTop) * 0.7 + "px";
@mrtvakOnaj
@mrtvakOnaj 2 года назад
@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 🙏🏽
@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
@theaxacall
@theaxacall 3 года назад
Great
@razaulkarim7323
@razaulkarim7323 4 года назад
const parallax = document.getElementById("banner"); window.addEventListener("scroll", function () { let offset = window.pageYOffset; parallax.style.backgroundPositionY = offset * 0.7 + "px"; })
@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...
@STUPIDYOUTUBE_HIDINGMSGS
@STUPIDYOUTUBE_HIDINGMSGS 3 года назад
Liked! Simple and easy! Thanks!
@אילוןמעיין
@אילוןמעיין 2 года назад
Thanks1
@donnchadhmcginley3153
@donnchadhmcginley3153 5 лет назад
how do I do it for 2 or more elements on the page?
@Aydos-s3h
@Aydos-s3h 5 лет назад
nice
@DiviTivi
@DiviTivi 4 года назад
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;
@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
@The_Drewid
@The_Drewid 6 лет назад
I keep getting "Cannot set property 'backgroundPositionY' of undefined" whenever I try the parallax. What's going on?
@freecodecamp
@freecodecamp 6 лет назад
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
@softonictech3755
@softonictech3755 6 лет назад
great
@sidarjunful
@sidarjunful 3 года назад
how to do this in react.js?
@DynamiteProd
@DynamiteProd 3 года назад
assuming this doesn't work on ipad
@bidondziura6753
@bidondziura6753 5 лет назад
o man, perfect for me:)
@dnevnikprogramista5704
@dnevnikprogramista5704 5 лет назад
Спасибо)
@ayaanchopra5755
@ayaanchopra5755 4 года назад
Thnx...but he part did not work bro
@maganabiyev2111
@maganabiyev2111 2 года назад
why no source code ;(
@luigivampa9780
@luigivampa9780 6 лет назад
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
@richardcastellanosb
@richardcastellanosb 5 лет назад
util gracias
@hasnathabdullah1253
@hasnathabdullah1253 3 года назад
this code just not runing correctly .
@hasnathabdullah1253
@hasnathabdullah1253 3 года назад
sorry code is runing correctly
@manuel5943
@manuel5943 6 лет назад
Now div display: flex does not work anymore. You have to use flex instead of flex, inline-flex.
@ayaanchopra5755
@ayaanchopra5755 4 года назад
Js
@senkamatic8448
@senkamatic8448 Год назад
Best!!!
@senpai3594
@senpai3594 3 года назад
thanks
@deansolistino
@deansolistino 2 года назад
Thank you! I am now a huge fan! So clear!!!!!!!!!!!😇😇😇😇😇😇😇😇
@eliasdorigoni
@eliasdorigoni 6 лет назад
Crystal clear explanation. Thanks!
Далее
JavaScript Visualized - Closures
11:34
Просмотров 40 тыс.
7 ways to deal with CSS
6:23
Просмотров 1,1 млн
Award Winning Animation With Only 20 Lines Of CSS?
6:59
This Simple Trick Makes Your Website 83% Better Looking
10:57
These CSS PRO Tips & Tricks Will Blow Your Mind!
8:48
Просмотров 335 тыс.
Coding Was HARD Until I Learned These 5 Things...
8:34
Simple Pure CSS Parallax Scroll Tutorial
26:36
Просмотров 69 тыс.
CSS Flexbox Course
48:43
Просмотров 329 тыс.
Top 10 CSS One Liners That Will Blow Your Mind
13:34
Просмотров 960 тыс.
CSS Tips And Tricks I Wish I Knew Before
12:12
Просмотров 480 тыс.