How to Dynamically Change Profile Image by Dragging in JavaScript

How to Dynamically Change Profile Image by Dragging in JavaScript

Introduction

In this tutorial we will create a How to Dynamically Change Profile Image by Dragging in JavaScript. This tutorial purpose is to teach you how to change your profile image. This will thoroughly show you the simple way of applying drag and drop function for changing image . I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that use drag and drop to update your image. I will give my best to provide you the easiest way of creating this program Change Profile Image by Dragging. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the image list and profile frame. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Dynamically Change Profile Image by Dragging</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-8">
  17. <center><h2>My Profile Background<//center>
  18. <div style="border:1px solid #000; height:250px; width:350px;" ondrop="drop(event)" ondragover="dragOver(event)"></div>
  19. </div>
  20. <div class="col-md-4">
  21. <h5>My Image</h5>
  22. <img src="images/image1.jpg" id="images/image1.jpg" height="100" width="100" draggable="true" ondragstart="drag(event)"/>
  23. <img src="images/image2.jpg" id="images/image2.jpg" height="100" width="100" draggable="true" ondragstart="drag(event)"/>
  24. <img src="images/image3.jpg" id="images/image3.jpg" height="100" width="100" draggable="true" ondragstart="drag(event)"/>
  25. <img src="images/image4.jpg" id="images/image4.jpg" height="100" width="100" draggable="true" ondragstart="drag(event)"/>
  26. </div>
  27. </div>
  28. <script src="script.js"></script>
  29. </body>
  30. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow you to change your profile image by dragging the image to the frame. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function dragOver(e){
  2. e.preventDefault();
  3. }
  4.  
  5. function drop(e){
  6. e.preventDefault();
  7. var data = e.dataTransfer.getData("data");
  8. e.target.innerHTML = "";
  9. e.target.style.backgroundImage = "url('"+data+"')";
  10. e.target.style.backgroundRepeat = "no-repeat";
  11. e.target.style.backgroundSize = "cover";
  12. e.target.style.backgroundPosition = "Center";
  13.  
  14. }
  15.  
  16. function drag(e){
  17. e.dataTransfer.setData("data", e.target.id);
  18. }

In the code above we just create only 3 important methods calleddrop, drag, dragOver. The drag() function will bind the data of the dragged image object. The dragOver() function will just only prevent the web browser from defaulting the drag event. Lastly the drop() function will transfer the dragged image data into the target zone and set the image property to display the image.

Output:

The How to Dynamically Change Profile Image by Dragging in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Dynamically Change Profile Image by Dragging in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment