JavaScript Interface Challenge: Click and Drag to Scroll -

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ธ.ค. 2024
  • Today we make a pretty neat click and drag to scroll interface where you will learn a whole lot about JavaScript events!

ความคิดเห็น • 101

  • @neelammishra8135
    @neelammishra8135 4 ปีที่แล้ว +9

    Thanks so much!
    I created such a page with touch events, but most of the people visting the page were on desktop, so i needed to add the drag functionality as fast as I could.
    You saved my life!!!

  • @heavydirtysoul1491
    @heavydirtysoul1491 4 ปีที่แล้ว +3

    when i got to console.log(walk) - i was like 'WOOOOW HOW COOL IS THAT'.
    it was so rewarding in emotional point.
    thank you for sharing your knowlege!

  • @pablotruffa588
    @pablotruffa588 10 หลายเดือนก่อน

    Hi wes, i just landed here and it works like a charm. Im preparing an angular 17 app and i adapted your code. Thanks for the tutorial, i lorn a lot!

  • @Stefandb
    @Stefandb 3 ปีที่แล้ว +2

    Awesome tutorial man! wish you also covered the tag part of the cards. Since it kind of breaks it.

  • @mrSargi7
    @mrSargi7 3 ปีที่แล้ว +3

    thank you a lot for this!
    i've been struggling to implement this on react and your way of explaining really made everything so much easy.

  • @valentino4407
    @valentino4407 2 ปีที่แล้ว

    i was struggling to get the math right and this simplyfied my task so much, thank you!!!

  • @jemjem8902
    @jemjem8902 4 หลายเดือนก่อน

    this saved me thanks! I translated it in react using useEffect and useRef

  • @CarloNyte
    @CarloNyte 8 หลายเดือนก่อน

    Super helpful! thank you for breaking this down the way you did!

  • @radovansurlak7445
    @radovansurlak7445 7 ปีที่แล้ว

    Thank you so much for these videos and blog posts Wes, I really enjoy them not being so damn nerdy and dry.

  • @BaljinderKaur-hd4dj
    @BaljinderKaur-hd4dj 6 หลายเดือนก่อน

    it really helped me to understand the concept. it works perfectly in desktop view but do not know why it is not working in responsive view.

  • @allisontpowell
    @allisontpowell 6 ปีที่แล้ว +2

    This is awesome, was looking for this exactly. Thanks

  • @klauskuhlmann5856
    @klauskuhlmann5856 6 หลายเดือนก่อน

    You're great, thanks for this masterpiece

  • @marielatihova2077
    @marielatihova2077 2 ปีที่แล้ว +2

    Great video! Can we also find the source code in Github or somewhere else?

  • @aurelianobuendia24
    @aurelianobuendia24 6 ปีที่แล้ว +4

    Great tutorial. I´ve implement this using touch events

  • @victordiniz245
    @victordiniz245 2 ปีที่แล้ว

    Thank you so much!!!
    Just to add, I needed to add a preventDefault to my mousedown event listener too, due to it sometimes not properly seeing the mouseup event otherwise.

    • @bigk2367
      @bigk2367 2 ปีที่แล้ว

      dude can you please share css of this??

  • @rudrakshjain8492
    @rudrakshjain8492 ปีที่แล้ว

    around 8:00, you subtracted the offsetleft value from pageX, can you explain why

  • @jackchilders4858
    @jackchilders4858 2 ปีที่แล้ว

    We can reduce the code by using `slider.scrollLeft += -e.movementX * 3` instead.

  • @Dipenparmar12
    @Dipenparmar12 ปีที่แล้ว

    Amazing tutorial WesBos. Please keep it up.

  • @alperensozen9525
    @alperensozen9525 4 ปีที่แล้ว

    the job that made you +1 follower in 2020.

  • @havefun5519
    @havefun5519 ปีที่แล้ว

    Cool, BTW, could I use with scroll snap? I tried mouse scroll will be conflict with scroll snap,,,, it won't scroll smooth but jumpy

  • @lijovijayan
    @lijovijayan 5 ปีที่แล้ว +2

    you saved my life !

  • @omkarp9943
    @omkarp9943 2 ปีที่แล้ว

    Thank you Mr. Wes Bos!!

  • @nnivxix
    @nnivxix 5 ปีที่แล้ว +1

    nice, that's really what i want, thank you buddy, keep it up 👍👌

  • @a4yster
    @a4yster 3 ปีที่แล้ว

    Bos is Boss. each and every single time.

  • @meth-method
    @meth-method 7 ปีที่แล้ว +4

    Hey Wes!
    I wanted to share a tip and that is you can do this with only using the `mousemove` event. By checking event.buttons, you can know if the mouse is pressed, that makes mousedown, mouseup and mouseleave obsolete. To calculate how far to move you can store previous event and compare the previous with current events `clientX` property.
    Then you can get away with only this:
    ```
    let prevEvent;
    scrollable.addEventListener('mousemove', event => {
    if (event.buttons === 1) {
    scrollable.scrollLeft -= event.clientX - prevEvent.clientX;
    }
    prevEvent = event;
    });
    ```

    • @meth-method
      @meth-method 7 ปีที่แล้ว +6

      Gotta backpedal here a little. Turns out macOS doesn't have an API for button presses so this doesn't work on Safari, so pretty bad advice from me. Always learning. :)

  • @أحمدالنجار-و3ي
    @أحمدالنجار-و3ي 2 ปีที่แล้ว

    you make it very simple , thank you

  • @demian5511
    @demian5511 4 ปีที่แล้ว +4

    Nice. I did the same thing with movementX. It tracks the previous and current x offset and calculates the difference for you.
    scrollContainer.addEventListener('mousemove', function(e){
    if(isDragging){
    e.preventDefault();
    scrollContainer.scrollLeft -= e.movementX;
    }
    });
    May be helpful for someone...

  • @MrGuidogr95
    @MrGuidogr95 3 ปีที่แล้ว

    You're the best man

  • @wirandhikablogs
    @wirandhikablogs 7 ปีที่แล้ว

    Hi Wes thank you so much for this awesome screencast.
    by the way how could i achieve the same resut with my scroll wheel ?
    i am able to drag to scroll but not able to use my mouse wheel for this horizontal layout items ..

  • @isroiljohntolibov6057
    @isroiljohntolibov6057 5 ปีที่แล้ว +2

    Thanks for nice video. By the way I found little issue with scrolling.
    [inside "mousemove" function] after assigning
    "slider.scrollLeft = slider.scrollLeft - walk"
    you have to re-assign
    "startX = e.pageX - slider.offsetLeft"
    then scrolling works smoothly

  • @gamegang6112
    @gamegang6112 2 ปีที่แล้ว

    How do you make the scrollbar o when not scrolling?

  • @keyzuniverse
    @keyzuniverse 4 ปีที่แล้ว

    thanks dude i was almost looking for the same tut

  • @parasarora5869
    @parasarora5869 6 ปีที่แล้ว +1

    sir can you plz tell me why at the end we need to subtract walk from scrollLeft?...I am a bit confused with this. How do you figured out that you have to subtract the walk from scrollLeft?

  • @sofie3313
    @sofie3313 4 ปีที่แล้ว

    Why does it not work if you have two of these .items sections beloweach other?!???????
    I want two scrollable bars on the same page but only the opper one work with the click and drag effect.

  • @snehasamuel7426
    @snehasamuel7426 ปีที่แล้ว

    i am getting scrollLeft as 0 everytime. Anyidea whyand what i should do?

  • @ilkayalparslan
    @ilkayalparslan 3 ปีที่แล้ว

    where do you get the css for it ?

  • @kingwindie
    @kingwindie 7 ปีที่แล้ว

    what text editor do you use,your workflow is perfect

    • @Glinkis
      @Glinkis 6 ปีที่แล้ว

      kingwindie Visual Studio Code. It's free.

  • @kingsvilleafricang2823
    @kingsvilleafricang2823 6 ปีที่แล้ว +1

    please, can you do one for a vertical div..and also show the css file

    • @Gutto_o
      @Gutto_o 6 ปีที่แล้ว

      Check my comment, i have one example at codepen with both scrolls working

    • @cristighn
      @cristighn 5 ปีที่แล้ว +1

      @@Gutto_o Thank you for the codepen!

    • @yashvarshney4072
      @yashvarshney4072 5 ปีที่แล้ว

      @@Gutto_o can we do this add the second new class same a function

  • @fajrinmahyuddin2618
    @fajrinmahyuddin2618 3 ปีที่แล้ว

    why margin on first number item not same with the last item ? the first item more space in its margin than last..

  • @heavydirtysoul1491
    @heavydirtysoul1491 4 ปีที่แล้ว

    it follows a link after its block was dragged. how to prevent that?

  • @hieuminh9402
    @hieuminh9402 3 ปีที่แล้ว

    Thank Bro. I did. Very helpful

  • @weshuiz1325
    @weshuiz1325 5 ปีที่แล้ว

    really helpfull
    finnaly something good

  • @SebastianPerezG
    @SebastianPerezG 7 ปีที่แล้ว

    It will be nice if you can add as a plus an infinite drag.

  • @realDominik586
    @realDominik586 5 ปีที่แล้ว +2

    Why not just use Mouse​Event​.movementX
    ?

  • @YunusParvezKhan
    @YunusParvezKhan 4 ปีที่แล้ว

    Thank You very muchh.. I appreciate your work

  • @amitmehta28
    @amitmehta28 ปีที่แล้ว

    Thankyou so much ❤

  • @satyampandey2222
    @satyampandey2222 ปีที่แล้ว

    Nice vide obrah

  • @IdanRozin
    @IdanRozin 7 ปีที่แล้ว +1

    Can u please share the css?

  • @rafaglanc7137
    @rafaglanc7137 7 ปีที่แล้ว +1

    how to add touch events to this ?

  • @owltrades
    @owltrades 3 ปีที่แล้ว

    Muchas Gracias 👍👍

  • @meghanarajappa1854
    @meghanarajappa1854 4 ปีที่แล้ว

    can you make this scroll to loop, i mean continuous scroll

  • @hmmm2686
    @hmmm2686 5 หลายเดือนก่อน

    Why i always got scroll left value 0

  • @papodedevcomjuliancio7599
    @papodedevcomjuliancio7599 3 ปีที่แล้ว

    Thanks bro!

  • @ericellison2413
    @ericellison2413 6 ปีที่แล้ว

    wes bos you just saved my ass ......

  • @TheMariazapata
    @TheMariazapata 4 ปีที่แล้ว +1

    Gracias.

  • @moisescastillo3447
    @moisescastillo3447 6 ปีที่แล้ว +2

    share the css please

  • @DurBeats
    @DurBeats 4 ปีที่แล้ว

    Hi there, thank you for that great Tutorial! I just wondered how I could do it to always scroll a specific amount on scroll. So exactly one element is in frame. I figured to check wether the scroll was positive or negative, and that works, but how could I animated the new set scrollLeft property?

    • @Annak942
      @Annak942 3 ปีที่แล้ว

      Disclaimer: this will not actually answer your question but if you need to do it for a project there’s a cool little js library called Swiper that does all that for u and it’s super easy, you can set slidesPerView at breakpoints and cool swipe effects :)

  • @igoroliveira9146
    @igoroliveira9146 4 ปีที่แล้ว

    Mine does work with the finger on phone or tablet. But it doesn't work when I use my mouse on desktop unless I have the console opened. :/

    • @uzair004
      @uzair004 4 ปีที่แล้ว

      does it work on touch phone using mousedown, and mousemove ?

    • @igoroliveira9146
      @igoroliveira9146 4 ปีที่แล้ว

      @@uzair004 It works on my phone if I drag with my finger, but I don't need to do all of this for that. However, the mouse-drag part doesn't work on the computer, I need to use the horizontal scroll bar.

  • @mreddygi
    @mreddygi 4 ปีที่แล้ว +1

    thanks a lot ,, im using this in vue js,, anyone who wish the same let me know!

  • @vishalj845
    @vishalj845 5 ปีที่แล้ว

    Thank you.

  • @tacocatgamer
    @tacocatgamer 3 ปีที่แล้ว

    can't get it to work for me :/ not sure if anyone is available to help me out?

  • @alexalannunes
    @alexalannunes 5 ปีที่แล้ว

    font name?

  • @xxsaifxx2450
    @xxsaifxx2450 4 ปีที่แล้ว

    can any one explain to me about the srollLeft how could we add a variable to an element and it moves

    • @uzair004
      @uzair004 4 ปีที่แล้ว +1

      scrollLeft is property not just variable, it shows how much element is scrolled from left side.
      maybe variable name should be changed to scrolledPart to avoid confusion.

    • @user-ct5oo3do7b
      @user-ct5oo3do7b 3 ปีที่แล้ว

      The Element.scrollLeft property gets or sets the number of pixels that an element's content is scrolled from its left edge.

  • @hasanmithawala1045
    @hasanmithawala1045 2 ปีที่แล้ว

    I didn't get it please can anyone help me

  • @kishorpustake6682
    @kishorpustake6682 ปีที่แล้ว

    3:19 js

  • @Yohoyboy
    @Yohoyboy 4 ปีที่แล้ว

    nice intro

    • @Yohoyboy
      @Yohoyboy 4 ปีที่แล้ว

      im ontario too

  • @syedtaqi732
    @syedtaqi732 6 ปีที่แล้ว

    i need the coding for this

  • @ArmandoHinojosaCcamaSARC
    @ArmandoHinojosaCcamaSARC 2 ปีที่แล้ว

    I can't do it in ReactJs xD T.T

  • @dileepnagendra8199
    @dileepnagendra8199 2 ปีที่แล้ว

    hey, put the source code please

  • @TheOlian04
    @TheOlian04 7 ปีที่แล้ว +4

    That's one horrible font you got for comments 0_o what's it called?

  • @MW3GlitchSA
    @MW3GlitchSA 2 ปีที่แล้ว

    Please post the code mate

    • @MW3GlitchSA
      @MW3GlitchSA 2 ปีที่แล้ว

      Its like a crime to post a coding tutorial without a link to the full code

    • @MW3GlitchSA
      @MW3GlitchSA 2 ปีที่แล้ว

      And this thing scrolls wrong way

    • @bigk2367
      @bigk2367 2 ปีที่แล้ว +1

      @@MW3GlitchSA I am struggling with css can you share your css?

    • @MW3GlitchSA
      @MW3GlitchSA 2 ปีที่แล้ว

      @@bigk2367 I use Bootstrap studio to auto generate all css

  • @MehmetKeskin-pc6oo
    @MehmetKeskin-pc6oo ปีที่แล้ว

    Kimi
    Yrr

  • @Dave-rd5bb
    @Dave-rd5bb 4 ปีที่แล้ว

    omg

  • @codingtraps
    @codingtraps ปีที่แล้ว

    neither you showed the css and nor the code, whats the point of making videos like that?