Тёмный

Before/After Image Slider Comparison (HTML, CSS, and JavaScript) 

Coding in Public
Подписаться 47 тыс.
Просмотров 22 тыс.
50% 1

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

 

23 сен 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 134   
@h3llloworld
@h3llloworld 21 день назад
this is such a good solution, i attempted to build this on my own before deciding to see how other's are doing it. this is so insightful how much little JavaScript you need to get things done. thanks for doing this, God bless you 🙏🏽
@countercoder260
@countercoder260 6 месяцев назад
This is so great, I've searched through so many different variations and this is by far the most accessible image comparison on the web
@RobertMcGovernTarasis
@RobertMcGovernTarasis 2 года назад
Dang you made that look so easy. Nicely explained and great to see the consideration for accessibility
@CodinginPublic
@CodinginPublic 2 года назад
Thanks for the kind words! Glad it was a help!
@MANIKDENIS
@MANIKDENIS 2 года назад
Absolutely amazing! It's a very creative and smart idea to use an input with the range type here. It was very interesting and informative, thank you, Chris!
@CodinginPublic
@CodinginPublic 2 года назад
So glad you enjoyed it! Thanks for saying something!
@vitaligolev2953
@vitaligolev2953 Год назад
Thank you for this, fantastic! I was doing it quickly, and it wouldn't work, then, of course, in js I found that I used normal single quote marks instead of the back ones. I works like magic :)
@CodinginPublic
@CodinginPublic Год назад
Awesome!
@MarshallSC1
@MarshallSC1 2 года назад
You did it, once again... Just can't stop with the amazement! Jaw-dropping good!
@CodinginPublic
@CodinginPublic 2 года назад
🎶 can’t stop won’t stop 🎶
@AdamBJohnson1
@AdamBJohnson1 2 года назад
Saw this video on the same topic several months ago: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-AZVAAydWTj4.html After watching Chris's video, it's obvious Chris's solution is 10x better. Again, great video. You nailed this one! 👏
@CodinginPublic
@CodinginPublic 2 года назад
Hey, thanks so much! I looked quickly and everything looked way over engineered, so I just came up with my own. Haha I’m sure there are ways to make it even better!
@adnan_niazi
@adnan_niazi 2 года назад
What if we want to change some content based on the after before images like when its after the text will be different and when its before the other div will show?
@CodinginPublic
@CodinginPublic 2 года назад
Yeah, I'd probably use a figure element with a figcaption and then just hide/show the before/after image the same way I show here. Hope that helps!
@MMDZ_05
@MMDZ_05 5 месяцев назад
I want more information on how to use this on a Wix website that I am creating. Ideally, I want to use this code on one page to display multiple before-and-after pictures. I have no experience in coding. Thank you.
@JeremyFukunaga
@JeremyFukunaga 20 дней назад
Thank you for this! Just what I need and works great. I am wondering what the best way is to have it the full width of a page or content area? I tried playing with the CSS, but it does weird things to it. Thank you for any help!
@nexTabDE
@nexTabDE 2 месяца назад
This was a great breakdown! 🙂
@CodinginPublic
@CodinginPublic 2 месяца назад
Glad it was helpful!
@erinhudsondev8958
@erinhudsondev8958 5 месяцев назад
This is great. My only issue is that on mobile (where most of my site users will be interacting with it) scrolling interferes with it. As in, when I am scrolling to this section, it is also moving the slider on the image. Is there a way to disable the image slider during scrolling? of make it so that a tap on the image doesn't do anything, only a horizontal drag does?
@CodinginPublic
@CodinginPublic 4 месяца назад
Hey! Sure, you could disable moving it on scroll using JS. As to the horizontal drag, you'd likely have to write your own element for this as that's the default behavior for our slider.
@BEVILE
@BEVILE Год назад
thanks man the way you are explaining things is very simple and great !
@CodinginPublic
@CodinginPublic Год назад
You’re welcome! Thanks for the kind words!
@thelocolexi
@thelocolexi 7 месяцев назад
Thanks so much for this tutorial! I followed along and also referenced the source code, but for some reason am not able to have the slider button drag. The before and after image input is registered (the images change and are draggable), but the icon I’m using remains centered and unmoving. Do you know why this might be?
@CodinginPublic
@CodinginPublic 7 месяцев назад
Hmm, I don't. I'm sorry! Could you run your CSS through a diff checker and compare it to mine? That would show you where it's different.
@renehinojosa1962
@renehinojosa1962 9 месяцев назад
Love the tutorial but I have one issue when I've added the code into my Divi page. The foreground image zooms in and out when I move the slider handle, any ideas what code I could fix/add?
@CodinginPublic
@CodinginPublic 9 месяцев назад
Hmm…I don't know? Could it be my generic classes are clashing with something you're already using?
@LocalCreative
@LocalCreative Год назад
Thank you SO much! I was able to customize this for my website perfectly.
@CodinginPublic
@CodinginPublic Год назад
Wonderful! Thanks for sharing!
@wajdy2620
@wajdy2620 3 месяца назад
Can you create a vertical version. I’ve been trying but no luck
@yousefmiri7045
@yousefmiri7045 Год назад
I just have one question, I need to make the image size to be like the container size . When I add my images they was to big and I didn’t know what to do . Thanks for your time !
@CodinginPublic
@CodinginPublic Год назад
Thanks for the comment. I may need to see your code to be a help? Mind sharing it somehow? Feel free to email me and I'll get back if I have time. chris@codinginpublic.dev
@jonathanshannon5722
@jonathanshannon5722 11 месяцев назад
Great video, I do have a question though. I'm building a business website that requires me to show before and after pics of jobs I've done, I've added four sliders to my page but I'm only able to use the slider on the first image, the other three can't be moved from left to right. I've tried adding a class to each individual slider and updated the JScript but nothing seems to work. Any help on this would be much appreciated.
@CodinginPublic
@CodinginPublic 11 месяцев назад
I believe someone else asked about this! Here you go! codepen.io/Coding-in-Public/pen/RwyxzGQ
@jonathanshannon5722
@jonathanshannon5722 11 месяцев назад
mate, you are a legend, thanks for the speedy replay. Ill let you know how I get on ;) @@CodinginPublic
@jonathanshannon5722
@jonathanshannon5722 11 месяцев назад
Absolutely brilliant mate, 9 sliders added ;) had to delete the greyscale and a few other bits but it's all sorted. Thanks again brother.
@mohamadybr
@mohamadybr 2 года назад
This is very interesting! I have always wondered how such things were developed.
@CodinginPublic
@CodinginPublic 2 года назад
I needed one and wasn’t sure either, so this is what I came up with. 🤷‍♂️ ha
@mohamadybr
@mohamadybr 2 года назад
@@CodinginPublic Well done, very appreciated effort!
@garyvisionmusicchannel
@garyvisionmusicchannel Год назад
Hi bro ! It was a very interested video and the code works very well. Except that, my before image doesn't act like it should do. My before image's container is actually the space between the left border of the whole container and the slider's vertical bar. So each time I'm sliding, my before image's width is changing, making an unexpected zoom effect. Do you know how to fix it please ?
@TheSalvera
@TheSalvera Год назад
I'm having the same issue. Edit: I put a max-width on the .container, and that seemed to fix it
@CodinginPublic
@CodinginPublic Год назад
Sounds like TheSalvera below found an answer? It's not happening for me and I'd have to see your code to make a suggestion, but hope that helps!
@libero5469
@libero5469 Год назад
same thing happens for me, did you find any solution
@ArasyDesign
@ArasyDesign Год назад
great job brother. but may i ask something, i want put 2 or more this image comparison on one page but it doesnt work, only the first one work, can u help me how to make more than one image comparison on one page? thank GBU
@CodinginPublic
@CodinginPublic Год назад
Sure thing. Glad it was a help. You'd need to change the JS to loop over a series of sliders instead of the single one. You can do this with querySelectorAll and then select the class in question. Hard to explain in a comment, but hopefully that helps point you in the right direction. Like this: codepen.io/Coding-in-Public/pen/RwyxzGQ
@streamx2
@streamx2 2 года назад
Can I please ask that you try to start your projects with empty files. The is the reason behind the most successful coding channels like Traversy Media, NetNinja and even Wes Bos courses. It puts people off when you already have multiple files open and distracts from the tutorial. This tutorial was perfect because a person can follow along from the start.
@CodinginPublic
@CodinginPublic 2 года назад
Thanks for the suggestion! I totally agree that starting from the beginning is usually best. I think this _might_ be the only one I've ever started with three blank files. I'm not totally sure why I did. I'd say 95% or so of my videos start with an empty directory, but I'll keep that in mind going forward. Thanks again!
@muhammadukasha9635
@muhammadukasha9635 2 года назад
What if you have multple image sliders? The code doesn't work if there are more than one sliders. :(
@CodinginPublic
@CodinginPublic 2 года назад
I'll whip up an example here later today if I get a chance. You'd just need to change up the query selector for the container to be a querySelectorAll.
@muhammadukasha9635
@muhammadukasha9635 2 года назад
@@CodinginPublic thank you! Look forward to the solution!
@CodinginPublic
@CodinginPublic 2 года назад
@@muhammadukasha9635 codepen.io/Coding-in-Public/pen/vYjpqXp
@muhammadukasha9635
@muhammadukasha9635 2 года назад
@@CodinginPublic thank you so much!
@arek4820
@arek4820 Год назад
For multple image sliders its a lot of HTML code. Can we make fx. "ul > li" for images and conect this with JS ? There will be less HTML lines :)
@etkosko
@etkosko Год назад
hey bro ı made perfectly but there is only one problem . the before image having a sharpness i dont know why . my images same resulotion but on the left image having an sharpness or kinda filter . how can i solve it, please help??
@CodinginPublic
@CodinginPublic Год назад
Hmm…I'm not sure? I'd have to see some code or something. Anything in the CSS tell you why this is happening?
@libero5469
@libero5469 Год назад
same thing happens for me, did you find any solution
@samuelkuehne
@samuelkuehne Год назад
Hey I tried adding this code to my website with an but the Iframe doesnt load on iphones or on safari do you know a fix for this I would really apreciate a response
@CodinginPublic
@CodinginPublic Год назад
Hmm…I haven't used s in a quite a while. I'm guessing it's blocked by your site? Is there a way to add the code directly to your site?
@samuelkuehne
@samuelkuehne Год назад
@@CodinginPublic I use webflow its kinda hard to add it directly but I think apple is blocking 2nd hand website cookies thats why its not working but havent found a solution yet
@CodinginPublic
@CodinginPublic Год назад
Ah, that would make sense. Apple has really clamped down on security in Safari.
@byronfaux
@byronfaux Год назад
Love the video, just wondering what editor you are using?
@CodinginPublic
@CodinginPublic Год назад
VSCode :)
@jimmyj.6792
@jimmyj.6792 2 года назад
Congratz Chris for this nice content as over and over 🙏 totally crazy and genius 👌
@CodinginPublic
@CodinginPublic 2 года назад
🙌 thanks so much for your kind words. It was fun to put together!
@noelwiggins3583
@noelwiggins3583 Год назад
This is so fantastic. Exactly what I am looking to do. My only challenge is that I would love to have the images go full screen, both height and width. Would this be an easy fix?
@CodinginPublic
@CodinginPublic Год назад
So glad it was a help! You should able to change the .image-container declaration to width: 100vw and height: 100vh; remove the aspect ratio. You may also need to throw an object-fit: cover on the image?
@noelwiggins3583
@noelwiggins3583 Год назад
@@CodinginPublic Thank you so much for your response, your willingness to share your knowledge is amazing!, Thank you, it's almost there... the before image is scaling. I have the initial sider value of 100 instead of 50 because I want the user to pull the slider from the right. When I slide it the before image scales the image to the width of the position of the slider, instead of sticking to how it looks when first loaded.
@noelwiggins3583
@noelwiggins3583 Год назад
Setting the image container height to 100% instead of vh seemed to correct the scaling issue, but now I have an ugly scroll bar., and overflow: hidden doesn't get rid of it. The journey continues.
@CodinginPublic
@CodinginPublic Год назад
@@noelwiggins3583 Almost there! Feel free to send me your code and if I have time I can take a look.
@noelwiggins3583
@noelwiggins3583 Год назад
@@CodinginPublic Thank you for your reply. The 100% height did the trick, and adding overflow to the CSS removed the unwanted scroll bar. Thank you so much for a fantastic tutorial and follow up with my questions.
@SeppyDawg
@SeppyDawg 11 месяцев назад
Thank you!! Is it possible to add a code to track your mouse movement on hover?
@CodinginPublic
@CodinginPublic 11 месяцев назад
You could add that with JavaScript, but you'd still want the touch option for accessibility and mobile.
@nixonmanuel6459
@nixonmanuel6459 3 месяца назад
Thank you immensely!
@CodinginPublic
@CodinginPublic 3 месяца назад
Glad it helped!
@lvekua
@lvekua 2 года назад
As always the best content ❤ thank you!
@CodinginPublic
@CodinginPublic 2 года назад
🙌
@mr.everything1682
@mr.everything1682 Год назад
How Can I import images option instead of adding images every time?
@CodinginPublic
@CodinginPublic Год назад
What framework are you using?
@FoleysWorld
@FoleysWorld Год назад
I learned so much in this video! thxs
@CodinginPublic
@CodinginPublic Год назад
You’re welcome! Glad it was such a help!
@MohammadAhsan107
@MohammadAhsan107 Год назад
How can i add before / after text. Can you help me please?
@CodinginPublic
@CodinginPublic Год назад
You could use an absolutely positioned span or something and set it relative to its parent div where the image is placed.
@katarzynaspikowska
@katarzynaspikowska Год назад
Hi, this is a great video! Thanks a lot :) Would you be able to code it on REACT?
@CodinginPublic
@CodinginPublic Год назад
Sure-just use an onChange handler on the range slider!
@niccolelovespirates
@niccolelovespirates Год назад
Thank you so much for the video-Do you have any videos explaining how to turn this all into HTML? I am a complete beginner to code and for my website it will only let me put in the HTML part. I found websites that explain it but I don't understand what any of it means ':)
@niccolelovespirates
@niccolelovespirates Год назад
Hi I can't believe it but I figured it out! First copy everything in case you mess up. Go to the bottom of the HTML column, put paste all the CSS stuff For the Java, go back to the HTML Column, paste the Java column Then delete everything in the CSS and Java columns and it should still be exactly the same!
@CodinginPublic
@CodinginPublic Год назад
That’s it! Awesome job!!!
@CodinginPublic
@CodinginPublic Год назад
That’s exactly what I would have told you to do!!!
@niccolelovespirates
@niccolelovespirates Год назад
@@CodinginPublic Thank you so much!!
@maneth3448
@maneth3448 2 года назад
Great content! btw what theme do you use? and The Icon Pack
@CodinginPublic
@CodinginPublic 2 года назад
Glad you enjoyed it! My usual theme is here: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-5uETTXxVj8s.html but I was using something else here. I’ll track it down and comment here. And I use the Bearded Icons.
@CodinginPublic
@CodinginPublic 2 года назад
The theme I was using here is called "Palenight Operator." Hope that helps!
@maneth3448
@maneth3448 2 года назад
Thank you very much 🔥
@cjlister8508
@cjlister8508 Год назад
So close but so far. I can get as far as having the images over lap and add the slider. But I cant move it. Its just like a static image I assume that the javascirpt isnt working, but I have no idea why.
@CodinginPublic
@CodinginPublic Год назад
Hmm…that's odd. Yes, I'd assume that's a JS issue. Are you getting any messages in the console?
@alexcoleman1376
@alexcoleman1376 Год назад
@@CodinginPublic I had the same issue and then I realized that I referenced the js file in the header of the html file instead of just above the closing tag
@CodinginPublic
@CodinginPublic Год назад
Ah, sounds like your JS is not picking up your HTML. Move the script to just above the closing body tag. Let me know if that works.
@CodinginPublic
@CodinginPublic Год назад
Another fix here is to use the defer attribute or to set it as a type of module.
@eneriniets4845
@eneriniets4845 Год назад
Oh wow, this video is amazing! How can I use it in my Shopify Shop? Any ideas? Thank´s a lot
@CodinginPublic
@CodinginPublic Год назад
Glad you enjoyed it! Hmm…not sure? If you have access to the code, I think you could mostly drop this into your liquid template file? But I wonder if Shopify doesn't already have an app that does this?
@eneriniets4845
@eneriniets4845 Год назад
@@CodinginPublic yes, there are apps but every app makes the store more slowly that‘s why. So I‘ll try this out. Thank you 🙏🏼
@DailyDeveloperNews
@DailyDeveloperNews 4 месяца назад
Thanks a lot for code!
@CodinginPublic
@CodinginPublic 4 месяца назад
You're welcome!
@oscarjoselopezlucero5474
@oscarjoselopezlucero5474 11 месяцев назад
Thanks man, u really helpful
@CodinginPublic
@CodinginPublic 11 месяцев назад
Glad it was a help!
@Dksupa94
@Dksupa94 Год назад
will this work with a squarespace website?
@CodinginPublic
@CodinginPublic Год назад
I mean, it should? If you insert it in their html block? But I’m assuming they have a way to do something like this?
@somedoga
@somedoga Год назад
see code below for working with multiple sliders ```` const els = document.querySelectorAll(".container") els.forEach(el => { for (let i = 0; i < el.childNodes.length; i++) { if (el.childNodes[i].className === "slider") { let j = el.childNodes[i]; j.addEventListener('input', (e) => { el.style.setProperty('--position', `${e.target.value}%`); }) } } }) const container = document.querySelector('.container'); document.querySelector('.slider') ````
@somedoga
@somedoga Год назад
Hey OP! Can you change the codepen with this code? thank you for good ideas
@kokoze
@kokoze 6 месяцев назад
Thank you so much just ran into a problem with this!
@gronko_chug_butter
@gronko_chug_butter Год назад
could this be done with videos?
@CodinginPublic
@CodinginPublic Год назад
Perhaps? You could have two auto-looping mp4s, I think.
@niteshraut_
@niteshraut_ Год назад
how can we do vertical ?
@CodinginPublic
@CodinginPublic Год назад
you'd just need to switch up the properties the css variable points to-so top and height instead of left and width. Does that make sense?
@abdallahmahmoud8716
@abdallahmahmoud8716 Год назад
Perfect thank you
@CodinginPublic
@CodinginPublic Год назад
you’re welcome!
@juanmoncada1812
@juanmoncada1812 Год назад
amazing, thanks
@CodinginPublic
@CodinginPublic Год назад
Glad it was a help!
@artemisa5076
@artemisa5076 2 года назад
Can someone help me work this out on angular, please? 🙏
@CodinginPublic
@CodinginPublic 2 года назад
Wish I could! But I don’t know angular!
@anhdevga1109
@anhdevga1109 2 года назад
Thank you so much 😍😍
@CodinginPublic
@CodinginPublic 2 года назад
You’re welcome!
@farukborann
@farukborann Год назад
You are the man!
@CodinginPublic
@CodinginPublic Год назад
Glad it was a help!
@indranilhalder6633
@indranilhalder6633 2 года назад
Very helpful
@CodinginPublic
@CodinginPublic 2 года назад
Great! Thanks for saying something!
@sichy77
@sichy77 7 месяцев назад
Great video and follow up, nice oeral, but man the font you use, that is soooo bad :)
@CodinginPublic
@CodinginPublic 7 месяцев назад
Ha! Glad you liked the video. It's been long enough I may be using a different font, but now I'm afraid to check 🫣
@niteshkrsah2222
@niteshkrsah2222 2 года назад
You are awesome Lovely content
@CodinginPublic
@CodinginPublic 2 года назад
🙏 glad it was a help! Thanks for saying something!
@arupde6320
@arupde6320 2 года назад
be regular .
Далее