• We just launched and are currently in beta. Join us as we build and grow the community.

Drag and Drop using Javascript

Noname1231

Recurring Income Master
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
80
Likes
150
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial, we are going to learn how to the image into the box using javascript. This is a simple project that you can use in your website or system.

DIRECTIONS

HTML CODE

  1. <div

    id

    =

    "container"

    >
  2. <p

    >

    Drag Image into the squares using HTML5</

    p

    >
  3. <div

    id

    =

    "left"

    >
  4. <div

    id

    =

    "div1"

    ondrop=

    "drop(event)"

    ondragover=

    "allowDrop(event)"

    ></

    div

    >
  5. <br

    >
  6. <div

    id

    =

    "div1"

    ondrop=

    "drop(event)"

    ondragover=

    "allowDrop(event)"

    ></

    div

    >
  7. <br

    >

  8. </

    div

    >
  9. <img

    id

    =

    "drag1"

    src

    =

    "drag.png"

    draggable=

    "true"

    ondragstart=

    "drag(event)"

    width

    =

    "150"

    >
  10. <div

    id

    =

    "right"

    >
  11. <div

    id

    =

    "div1"

    ondrop=

    "drop(event)"

    ondragover=

    "allowDrop(event)"

    ></

    div

    >
  12. <br

    >
  13. <div

    id

    =

    "div1"

    ondrop=

    "drop(event)"

    ondragover=

    "allowDrop(event)"

    ></

    div

    >
  14. <br

    >

  15. </

    div

    >
  16. </

    div

    >

JAVASCRIPT CODE

  1. <

    script>
  2. function

    allowDrop(

    ev)

    {
  3. ev.preventDefault

    (

    )

    ;
  4. }

  5. function

    drag(

    ev)

    {
  6. ev.dataTransfer

    .setData

    (

    "text"

    ,

    ev.target

    .id

    )

    ;
  7. }

  8. function

    drop(

    ev)

    {
  9. ev.preventDefault

    (

    )

    ;
  10. var

    data =

    ev.dataTransfer

    .getData

    (

    "text"

    )

    ;
  11. ev.target

    .appendChild

    (

    document.getElementById

    (

    data)

    )

    ;
  12. }
  13. </

    script>

CSS CODE

  1. <style>
  2. #div1

    {

    width

    :

    150px

    ;

    height

    :

    150px

    ;

    padding

    :

    10px

    ;

    border

    :

    1px

    solid

    #aaaaaa

    ;

    }

  3. #left
  4. {
  5. border

    :

    1px

    solid

    #aaaa

    ;
  6. width

    :

    200px

    ;
  7. float

    :

    left

  8. }

  9. #right
  10. {
  11. border

    :

    1px

    solid

    #aaaa

    ;
  12. width

    :

    200px

    ;
  13. float

    :

    right

    ;

  14. }
  15. #container
  16. {
  17. width

    :

    580px

    ;
  18. margin

    :

    auto

    ;
  19. }
  20. p
  21. {
  22. font-size

    :

    25px

    ;
  23. text-align

    :

    center

    ;
  24. }
  25. </style>

Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

 

452,292

323,341

323,350

Top