Create A Draggable Image Slider in HTML CSS & JavaScript | Mobile Friendly Slider in JavaScript

Create A Draggable Image Slider in HTML CSS & JavaScript | Mobile Friendly Slider in JavaScript

CodingNepal

1 год назад

217,064 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Cameron Fletcher
Cameron Fletcher - 29.09.2023 07:03

Can someone tell me what I would need to change in the javascript if I wanted to duplicate this same slider with different images?

Ответить
Artist
Artist - 24.09.2023 14:27

I hv checked each and every line..I'm unable to scroll.. Its giving cannot read null values(adeventlistener) though
Though I have used script tag at the end...
PLEASE GIVE ME SOLUTION 😥😥

Ответить
Mahas Ceprian
Mahas Ceprian - 23.09.2023 18:16

Where is pagination like on thumnail?

Ответить
Musuh Abadi
Musuh Abadi - 20.09.2023 04:47

why carousel is using "what-space = nowrap" instead using flex ?

Ответить
Студийная фанта
Студийная фанта - 12.09.2023 05:25

Thank you, you are the best

Ответить
All Media In World
All Media In World - 11.09.2023 04:33

very very vey good thank you

Ответить
bo truong
bo truong - 08.09.2023 06:38

Can you make this slider with navigation dots please ?

Ответить
Black Fox
Black Fox - 01.09.2023 16:35

For everyone struggling with the problem that you can't scroll the page anymore on touch devices when touching the slider, change the following to make it work:

From this:
const dragging = (e) => {
if(!isDragStart) return;
e.preventDefault();
isDragging = true;
teamCarousel.classList.add("dragging");
positionDiff = (e.pageX || e.touches[0].pageX) - prevPageX;
teamCarousel.scrollLeft = prevScrollLeft - positionDiff;
showHideIcons();
}


To this:
const dragging = (e) => {
if(!isDragStart) return;

const isTouchDevice = 'ontouchstart' in window || navigator.msMaxTouchPoints;

if(!isTouchDevice){
e.preventDefault();
}

isDragging = true;
teamCarousel.classList.add("dragging");
positionDiff = (e.pageX || e.touches[0].pageX) - prevPageX;
teamCarousel.scrollLeft = prevScrollLeft - positionDiff;
showHideIcons();
}

Ответить
Huyền Phạm
Huyền Phạm - 18.08.2023 05:14

Great tutorial. Thank you very much! I just don't understand why import the font Poppins, as far as I know the font only applies to text. while this carousel only has images. So what role does the font play in this carousel?

Ответить
Mirza Usman
Mirza Usman - 08.08.2023 13:48

Thank you so much Bro Love from Pakistan...❤❤❤❤

Ответить
Zero G
Zero G - 06.08.2023 13:44

I came across 2 problems using this code,

1) The buttons don't disappear when on first/last slide when pressing them.

Fix: Call showHideIcons() at page load and then add an event listener for scrolling.

showHideIcons();
carousel.addEventListener("scroll", showHideIcons);

2) There is a bug tht prevents the MaxWidth calculation from being used when a decimal is present.

Fix:
const scrollLeft = Math.round(carousel.scrollLeft);

Then call upon the variable, scrollLeft.

If there are any other bugs let me know.

Ответить
Amit Bhai
Amit Bhai - 30.07.2023 22:26

r u the coder of amazon??

Ответить
Henryk Matthew
Henryk Matthew - 26.07.2023 06:56

Nice music bro

Ответить
Sardor Abdurasulov
Sardor Abdurasulov - 24.07.2023 00:26

Thank you very much

Ответить
Milos Brajovic
Milos Brajovic - 14.07.2023 16:31

It doesn't work in Firefox, do you know why?

Ответить
DemoF8
DemoF8 - 08.07.2023 19:29

thank you very much

Ответить
Felipe Brocca
Felipe Brocca - 28.06.2023 07:13

Nice tutorial. Im trying to do it, but instead of a carousel of <img /> im trying to do a carousel of <div> with background img or <div><img /></div> and i just cant make it work. I think it should be because of pred styles differences between div and img tags but i cant find them to make it work. Do you know any help?

Ответить
NeCroMrT
NeCroMrT - 22.06.2023 11:04

Best toturial i ever seen

Ответить
Understanding_Code
Understanding_Code - 28.05.2023 20:26

Thanks for you tutorials, did use it as a basis and changed some code to make it simpler.
For example, I use a variation in % to know where to autoslide.
You should properly test it though, there are still a lot of bugs. Only to cite a few :
- If you resize and you are not on the first element, responsive will be broken.
- If you drag / touch / move more than one card, you will be bounced back
- If you press quickly on next element, scroll will be uncorrectly computed
- Etc.

Ответить
Luis Rodrigo Magaña Guadarrama
Luis Rodrigo Magaña Guadarrama - 17.05.2023 02:29

AWESOME!! Thanks 🔥🔥🔥

Ответить
luczztem
luczztem - 17.05.2023 00:37

brother I have also come across some bugs with this slider:
1. On mobile, if you scroll vertically (to scroll the page) holding inside the slider, it won't scroll;
2. If you scroll past 1 item (to move like 2, 3 or 4 items), it moves back to slide just 1 item (it's annoying for the user experience);
3. If you keep scrolling to the same side after there are no more images, it gets a weird margin and the images becomes unaligned;

I have fixed it, but had to sacrifice the sliding functionality using mouse (it still works on mobile).
I used pure CSS to manage the sliding, and removed your javascript slider solution (I'm sorry), but I still used your JS for the buttons (they work great). This way is much simpler and I think it works the same. Here's what I did:

1. In the <img> element, set "draggable" back to "true";
2. In the <img> element, added CSS: "scroll-snap-align: start;"
3. In the ".carousel" element, added these CSS styles:
.carousel {
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
scroll-snap-type: x proximity;
scroll-behavior: smooth;
overflow-x: scroll;
}

.carousel::-webkit-scrollbar {
display: none;
/* Hide scrollbar for Chrome, Safari and Opera */
}

To make the left and right buttons disappear when there's no item's left, I just increased the setTimeout() time to 250ms.

Thanks again for this content bro, spared me a LOT of time and hair xD. Godspeed!.

Ответить
luczztem
luczztem - 15.05.2023 21:27

bro this tutorial is just perfect. this is the best carousel I found searching for hours! you helped so many people with this tutorial... may God bless you greatly!

Ответить
Muhammad Ahsan Saifi
Muhammad Ahsan Saifi - 08.05.2023 09:39

nice slider just want the source code

Ответить
Ertürk Yorulmaz
Ertürk Yorulmaz - 01.05.2023 19:33

Trasition does not work in code that I dowloaded?

Ответить
RTX Game Lab
RTX Game Lab - 29.04.2023 20:45

And more thing same work for cubic rotation 😎

Ответить
RTX Game Lab
RTX Game Lab - 29.04.2023 20:43

That's what i realy asking for 😅

Ответить
Al-S amadi
Al-S amadi - 26.04.2023 01:24

Bro that's fantastic but I want to use it more than one time in the same page , when I use it more than one time the first one works only .

Please tell me how can I use it more than one time.

Ответить
Đức Hòa Trần
Đức Hòa Trần - 23.04.2023 10:08

Great instructions, guy.

Ответить
avion 4
avion 4 - 21.04.2023 16:43

what do I have to change if I want div instead of img ? thank you

Ответить
Jay Aqshay
Jay Aqshay - 16.04.2023 18:46

when i put const on the java it shows error the 'const' is reserved what should i do

Ответить
rohit mahajan
rohit mahajan - 15.04.2023 20:35

thank you bro or sis for this i was need this

Ответить
Truong Bang
Truong Bang - 13.04.2023 06:37

Thank you

Ответить
Eelguneezmemmedov
Eelguneezmemmedov - 08.04.2023 13:25

Can u mae those on react js ?

Ответить
Татьяна Безкоровайная
Татьяна Безкоровайная - 07.04.2023 17:50

Hello. Thank you for the video. Need help. I've searched a lot and can't find the right answer. I need to make an endless slider carousel according to a tutorial project.
each new slide contains a pseudo-random set of cards +
cards are not repeated in the current block of the slide +
only one previous state is retained+
every time the page is reloaded, a new sequence of cards is generated+
card sets are generated based on 8 objects with animal data+

in the next block there is no duplication of cards with the current block. - it can't be implemented. the rest I`ve done

Ответить
Sexy Jimmy Comedy
Sexy Jimmy Comedy - 06.04.2023 11:35

what a great tutorial after a long search so far this was the best. thank you.

Ответить
GustavoX
GustavoX - 05.04.2023 17:56

Excellent tutorial, a really nice draggable carousel, and I've learned a some new things that I didn't knew, thank you!

Ответить
vikram Bais
vikram Bais - 05.04.2023 03:01

this helped me clearing my job interview . thank you . i got a package of 10l

Ответить
Muhammad Radifan Azman
Muhammad Radifan Azman - 28.03.2023 09:35

how to create multiple slider, on me its not working?

Ответить
Satish Mallick
Satish Mallick - 21.03.2023 13:22

also add active bullets point

Ответить
yosh
yosh - 10.03.2023 03:31

I copied everything line for line and yet for some reason the arrows will sometimes move the images a full slide's worth, like it's supposed to, and other times it will only move a few pixels. Why does it do this?

Ответить
MrViciousway
MrViciousway - 07.03.2023 01:50

Yo, that was the best tutorial i have found for a responsive carousel 🙌🔥🖤 I tweaked it a little to fit my needs but wow, overall I'm so thankful to your walk-through!!

- Favorite trick I learned was honestly the simple CSS trick for adding width to the .carousel img. I never thought about using width: calc( ) function and divide the 100% width by the amount of pictures you want on the screen. Nothing loses its shape 🙌 loved it. Can't believe I never thought of that.

Ответить
Gabriel Rojo
Gabriel Rojo - 06.03.2023 16:25

Thank u :)

Ответить
Ifal Fahri A
Ifal Fahri A - 16.02.2023 17:06

How to create multiple image slider? I tried to create another slider but it's not working

Ответить
Lucas Uriel
Lucas Uriel - 16.02.2023 15:54

very good video! it helped me a lot!

Ответить
Manuel Enweremchi
Manuel Enweremchi - 11.02.2023 11:18

This is good development. You really did account for all possibilities.

Ответить
Marco Cattaneo
Marco Cattaneo - 08.02.2023 13:38

That's using a lot of very cool tricks! Awesome video, and thanks for uploading.

Ответить
Luiz Meraki ツ
Luiz Meraki ツ - 04.02.2023 02:13

Thanks bro, your video helped me a lot!

Ответить
Mateusz Kujawa
Mateusz Kujawa - 01.02.2023 04:27

Great tutorial! Thx <3

Ответить