JavaScript Interface Challenge: Click and Drag to Scroll -

Поделиться
HTML-код
  • Опубликовано: 18 окт 2024

Комментарии • 101

  • @neelammishra8135
    @neelammishra8135 3 года назад +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!

  • @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.

  • @pablotruffa588
    @pablotruffa588 8 месяцев назад

    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!

  • @BaljinderKaur-hd4dj
    @BaljinderKaur-hd4dj 4 месяца назад

    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.

  • @valentino4407
    @valentino4407 Год назад

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

  • @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. :)

  • @jemjem8902
    @jemjem8902 2 месяца назад

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

  • @CarloNyte
    @CarloNyte 6 месяцев назад

    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.

  • @klauskuhlmann5856
    @klauskuhlmann5856 4 месяца назад

    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?

  • @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??

  • @aurelianobuendia24
    @aurelianobuendia24 5 лет назад +4

    Great tutorial. I´ve implement this using touch events

  • @rudrakshjain8492
    @rudrakshjain8492 11 месяцев назад

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

  • @allisontpowell
    @allisontpowell 6 лет назад +2

    This is awesome, was looking for this exactly. Thanks

  • @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

  • @Dipenparmar12
    @Dipenparmar12 Год назад

    Amazing tutorial WesBos. Please keep it up.

  • @jackchilders4858
    @jackchilders4858 2 года назад

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

  • @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

  • @omkarp9943
    @omkarp9943 Год назад

    Thank you Mr. Wes Bos!!

  • @demian5511
    @demian5511 3 года назад +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...

  • @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 ..

  • @أحمدالنجار-و3ي
    @أحمدالنجار-و3ي 2 года назад

    you make it very simple , thank you

  • @nnivxix
    @nnivxix 5 лет назад +1

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

  • @alperensozen9525
    @alperensozen9525 4 года назад

    the job that made you +1 follower in 2020.

  • @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?

  • @lijovijayan
    @lijovijayan 5 лет назад +2

    you saved my life !

  • @keyzuniverse
    @keyzuniverse 4 года назад

    thanks dude i was almost looking for the same tut

  • @SebastianPerezG
    @SebastianPerezG 7 лет назад

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

  • @a4yster
    @a4yster 3 года назад

    Bos is Boss. each and every single time.

  • @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..

  • @gamegang6112
    @gamegang6112 2 года назад

    How do you make the scrollbar o when not scrolling?

  • @kingwindie
    @kingwindie 6 лет назад

    what text editor do you use,your workflow is perfect

    • @Glinkis
      @Glinkis 6 лет назад

      kingwindie Visual Studio Code. It's free.

  • @MrGuidogr95
    @MrGuidogr95 3 года назад

    You're the best man

  • @heavydirtysoul1491
    @heavydirtysoul1491 4 года назад

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

  • @realDominik586
    @realDominik586 5 лет назад +2

    Why not just use Mouse​Event​.movementX
    ?

  • @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

  • @satyampandey2222
    @satyampandey2222 Год назад

    Nice vide obrah

  • @hieuminh9402
    @hieuminh9402 3 года назад

    Thank Bro. I did. Very helpful

  • @hmmm2686
    @hmmm2686 2 месяца назад

    Why i always got scroll left value 0

  • @IdanRozin
    @IdanRozin 7 лет назад +1

    Can u please share the css?

  • @meghanarajappa1854
    @meghanarajappa1854 4 года назад

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

  • @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?

  • @rafaglanc7137
    @rafaglanc7137 7 лет назад +1

    how to add touch events to this ?

  • @YunusParvezKhan
    @YunusParvezKhan 4 года назад

    Thank You very muchh.. I appreciate your work

  • @moisescastillo3447
    @moisescastillo3447 6 лет назад +2

    share the css please

  • @TheMariazapata
    @TheMariazapata 4 года назад +1

    Gracias.

  • @amitmehta28
    @amitmehta28 Год назад

    Thankyou so much ❤

  • @ilkayalparslan
    @ilkayalparslan 3 года назад

    where do you get the css for it ?

  • @tacocatgamer
    @tacocatgamer 3 года назад

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

  • @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.

  • @papodedevcomjuliancio7599
    @papodedevcomjuliancio7599 3 года назад

    Thanks bro!

  • @owltrades
    @owltrades 2 года назад

    Muchas Gracias 👍👍

  • @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 2 года назад

      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 :)

  • @weshuiz1325
    @weshuiz1325 4 года назад

    really helpfull
    finnaly something good

  • @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.

  • @ericellison2413
    @ericellison2413 6 лет назад

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

  • @vishalj845
    @vishalj845 5 лет назад

    Thank you.

  • @mreddygi
    @mreddygi 4 года назад +1

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

  • @hasanmithawala1045
    @hasanmithawala1045 2 года назад

    I didn't get it please can anyone help me

  • @ArmandoHinojosaCcamaSARC
    @ArmandoHinojosaCcamaSARC 2 года назад

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

  • @syedtaqi732
    @syedtaqi732 6 лет назад

    i need the coding for this

  • @alexalannunes
    @alexalannunes 5 лет назад

    font name?

  • @Yohoyboy
    @Yohoyboy 4 года назад

    nice intro

    • @Yohoyboy
      @Yohoyboy 4 года назад

      im ontario too

  • @MehmetKeskin-pc6oo
    @MehmetKeskin-pc6oo Год назад

    Kimi
    Yrr

  • @kishorpustake6682
    @kishorpustake6682 Год назад

    3:19 js

  • @dileepnagendra8199
    @dileepnagendra8199 Год назад

    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?

  • @Dave-rd5bb
    @Dave-rd5bb 4 года назад

    omg

  • @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

  • @codingtraps
    @codingtraps Год назад

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