Drag & Drop with Javascript in 4 minutes

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ก.พ. 2025
  • Learn how to build draggable elements with Javascript in a few simple steps.
    Source Code: codepen.io/den...
    Instructor: / dennisivy11 / / dennis-ivanov
    💬 Follow Appwrite on social media & join our Discord community.
    • Twitter: / appwrite
    • Discord: appwrite.io/di...
    • LinkedIn: / appwrite

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

  • @Appwrite
    @Appwrite  6 หลายเดือนก่อน +2

    Draggable stick notes app video tutorial here: th-cam.com/video/yBThHM2pBbE/w-d-xo.htmlfeature=shared

  • @zaanly
    @zaanly 10 หลายเดือนก่อน +8

    It's great to see you back on TH-cam, Dennis. Your Django tutorials were truly missed. I even tried contacting you through LinkedIn but didn't hear back. Looking forward to your new Django content! ✨

    • @Appwrite
      @Appwrite  9 หลายเดือนก่อน +1

      Hey, sorry, it's tough for me to get back to all messages. I'll take a look now and will see if I can find it. Appreciate your patience and support :) - Dennis Ivy

  • @artipeo816
    @artipeo816 10 หลายเดือนก่อน +14

    This is really under rated

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

      Thank you 🙏 - Dennis Ivy

  • @iantimmis651
    @iantimmis651 10 หลายเดือนก่อน +29

    This channels name and logo make it feel like its a company trying to sell me something. I thought this was going to be an ad for some JS lib.

    • @Appwrite
      @Appwrite  10 หลายเดือนก่อน +8

      Nope, just awesome tools, cool projects and a awesome Backend as a service platform if you want it. - Dennis Ivy

  • @Ja-V-Tuber_M
    @Ja-V-Tuber_M 8 วันที่ผ่านมา

    Just what I was looking for, Thank you very much!!! :D

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

    great!!! thank you so much. js is perfect

  • @willia_music
    @willia_music 4 หลายเดือนก่อน +10

    why did you decide to use mousemove events instead of 'dragstart', 'dragend', 'drag', etc event listeners?

    • @ricardofigueiredo3585
      @ricardofigueiredo3585 8 วันที่ผ่านมา

      i dont usually use the drag api (dragstart, dragend, etc) beacause its not very custumizable. you cant change the cursor style while you're dragging, you dont have a lot of freedom in styling the draggable image, and more things. so unless you dont care about these details or efficiency is very important, building a drag and drop from scratch like this is probably gonna be the better way (just my opinion)

  • @yehuuu-xl3td
    @yehuuu-xl3td 3 วันที่ผ่านมา

    You can use pageX and pageY instead if you have problems on scrollable screens

  • @TANVEERAHMEDTAJIR
    @TANVEERAHMEDTAJIR 3 หลายเดือนก่อน

    Simple and to the point 👍

    • @Appwrite
      @Appwrite  3 หลายเดือนก่อน

      😉

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

    Excellent video, nice explanation!

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

    So nice tutorial video I like your direct explanation without a lots lablabla 👌your earn +1 subscribers

  • @imagineiffcom
    @imagineiffcom 10 หลายเดือนก่อน +1

    I did this using jquery ui for an NBA team's contest page many years ago. Doing this with pure javascript is very interesting and gives a lot more control.

    • @Appwrite
      @Appwrite  10 หลายเดือนก่อน +1

      Yea when it’s this simple, just JS is clean. However, I’m working on the full notes app (as I mention in the beginning) and honestly, a framework like react really does come in handy when the code gets more complicated. - Dennis Ivy

  • @stargazer8718
    @stargazer8718 3 หลายเดือนก่อน

    Daymm that's quick typin sir

  • @COLI_ALVAREZ
    @COLI_ALVAREZ 2 หลายเดือนก่อน

    Hello! This was very helpfull, but came in to an issue. How would you keep the card dragable only inside it's container?. I've use position absolute instead of fixed since I've put this exercise as an other section among sections and when I drag the card, the absolute position makes it dragable all over the place. A hint would be of value. Thanks!

  • @venkatesh1170
    @venkatesh1170 3 หลายเดือนก่อน

    thanks buddy ❤‍🔥

  • @Anonqqq2025
    @Anonqqq2025 3 หลายเดือนก่อน

    Thank you!

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

    How can you stop the card from going over the borders of the viewport. I used
    ((card.offsetTop + card.clientHeight)

  • @garasipemula
    @garasipemula 10 หลายเดือนก่อน +1

    I like it, keep it up ❤

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

    subscribed. this was great!

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

      🙏

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

    It gets a bit more interesting when you want to allow touch-move.

    • @Appwrite
      @Appwrite  10 หลายเดือนก่อน +1

      True, a topic worth covering for sure - Dennis ivy

  • @alexdin1565
    @alexdin1565 10 หลายเดือนก่อน +1

    hi dennis we missed your django tutorials please if you have some times make videos in your channel

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

      Soon, very soon Alex :) - Dennis Ivy

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

    Love it thankyou

  • @dkfailor
    @dkfailor 5 หลายเดือนก่อน +1

    Nice tutorial and easy to understand. But shouldn't you use defer or async instead of putting code between the closing body tag and closing html tag? I could be wrong but I don't believe that is recommended as standard practice.

    • @usethisforproductivity-tg7xq
      @usethisforproductivity-tg7xq หลายเดือนก่อน

      No it’s fine because by end of the document if you place the script before the body closes then the html will have finished parsing so the js isn’t blocking anything

    • @ricardofigueiredo3585
      @ricardofigueiredo3585 8 วันที่ผ่านมา

      @@usethisforproductivity-tg7xq the "defer" attribute on the script tag will make it so that the js file only loads after the html content, and it is best practise to put the script tags on the head tag with defer

  • @user-lc7pn8zp8f
    @user-lc7pn8zp8f 9 หลายเดือนก่อน

    Thanks for video 👍 phoneview the same with touchstart events ?

    • @Appwrite
      @Appwrite  9 หลายเดือนก่อน

      I believe it would require a little more configuration to handle touch events. Something I'll have to take a minute and look at. - Dennis Ivy

  • @mrchineduOfficial
    @mrchineduOfficial 9 หลายเดือนก่อน

    This is new to me, how did you come up with all the calculations? where can I learnt this. Thank you

    • @Appwrite
      @Appwrite  9 หลายเดือนก่อน +1

      I made a quick video explaining this, see if this helps :) x.com/appwrite/status/1783310348453544198

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

    It's very interesting.

  • @darsico
    @darsico 10 หลายเดือนก่อน +1

    how to keep it persistent using appwrite?

    • @Appwrite
      @Appwrite  9 หลายเดือนก่อน

      It's just a matter of updating the note on mouse up. The method I use is to just stringify the X & Y coordinationsa and store then in a attribute called "position". You'll see this in my upcomming video where I build out the entire notes app. Example:
      Save like this:
      JSON.stringify({x:47, y, 98})
      Use like this:
      const position = JSON.parse(note.position)
      console.log(note.x)
      console.log(note.y)

  • @AWM_2023
    @AWM_2023 10 หลายเดือนก่อน +2

    A bit too fast. And in 2024 we cannot refrain ourselves from excellence, so the css should be optimal, changing the cursor to draggable and dragging during the move. Thanks for the video and I hope you improve and grow a lot 💪🏼 congratulations! I’m rooting for you.

    • @Appwrite
      @Appwrite  10 หลายเดือนก่อน +2

      My assumption is that you are referring to the draggable attribute, correct? While both ways work I found that draggable has some drawbacks, but really it's just a preference at this point. Both ways work fine :)
      Appreciate the support 🙏 - Dennis Ivy

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

      @@Appwrite Hey! I found a way to make it a little bit more "appealing" that makes it clear when it's getting dragged.
      in the mouseDown function you can put the following code anywhere inside.
      card.style.cursor = "move";
      and in the mouseUp function you can put the following code inside
      card.style.cursor = "pointer";
      When you hover over the card, it will be pointer. On mouseDown it changes it to the "move" pointer which is two bidirectional arrows (hard to explain easy to see) and on mouseUp it changes it back. Super straight forward and hope it helps!

  • @Mahmoudr-KH-Roshdy
    @Mahmoudr-KH-Roshdy 2 หลายเดือนก่อน +1

    I modified the code so that it works on your phone as well ❤️ by event ( touchStart ) and event ( touchEnd )
    ...
    Code
    ....
    let newX = 0, newY = 0, startX = 0, startY = 0;
    const card = document.getElementById('card');
    card.addEventListener('mousedown', mouseDown);
    card.addEventListener('touchstart', touchStart);
    function mouseDown(e){
    startX = e.clientX;
    startY = e.clientY;
    document.addEventListener('mousemove', mouseMove);
    document.addEventListener('mouseup', mouseUp);
    }
    function mouseMove(e){
    newX = startX - e.clientX;
    newY = startY - e.clientY;
    startX = e.clientX;
    startY = e.clientY;
    card.style.top = (card.offsetTop - newY) + 'px';
    card.style.left = (card.offsetLeft - newX) + 'px';
    }
    function mouseUp(){
    document.removeEventListener('mousemove', mouseMove);
    document.removeEventListener('mouseup', mouseUp);
    }
    function touchStart(e){
    startX = e.touches[0].clientX;
    startY = e.touches[0].clientY;
    document.addEventListener('touchmove', touchMove);
    document.addEventListener('touchend', touchEnd);
    }
    function touchMove(e){
    newX = startX - e.touches[0].clientX;
    newY = startY - e.touches[0].clientY;
    startX = e.touches[0].clientX;
    startY = e.touches[0].clientY;
    card.style.top = (card.offsetTop - newY) + 'px';
    card.style.left = (card.offsetLeft - newX) + 'px';
    }
    function touchEnd(){
    document.removeEventListener('touchmove', touchMove);
    document.removeEventListener('touchend', touchEnd);
    };

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

    How do you generate a new picture for dragging and dropping?

    • @Appwrite
      @Appwrite  8 หลายเดือนก่อน +1

      That will be shown in the full "notes app" tutorial. Currently a work in progress. - Dennis Ivy

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

    how do i do this in react or next ??

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

      That will be in the full tutorial ;)

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

    Hi Dennis

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

      Hey! 👋 - Dennis Ivy

  • @ИванРагозин-я8я
    @ИванРагозин-я8я 10 หลายเดือนก่อน

    Thanks for the link to the code (no)

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

      I’ll add that in a bit

    • @Appwrite
      @Appwrite  9 หลายเดือนก่อน

      codepen.io/dennisivy/pen/VwNEPvY

  • @d.minkov
    @d.minkov 9 หลายเดือนก่อน +1

    You don't need those events.. there is already an event for drag in JavaScript 😂

    • @Appwrite
      @Appwrite  9 หลายเดือนก่อน +3

      You can accomplish the same thing in many different ways 😁And btw, the events you are referring to are good for drag and drop when it comes to sorting, ordering and placing items in a specific predestined position. This method gives more flexibility- Dennis Ivy

    • @renaissancewoman5373
      @renaissancewoman5373 6 หลายเดือนก่อน +1

      @@Appwrite what if I needed the draggable item to be an image instead of "card"?