Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Drag drop (Newbie)

Featured Replies

Hi All

 

This is my 1st post so apologies if i am posting incorrectly

 

I am completely new to web development -i am learning HTML, CSS and Javascript

 

I have been tasked to created a webpage front page that has some images that float about. When a user drags an image into an area (in my case another div) - i want to open up a link

 

Now so far i have managed to get the animation that shows the images floating

I have also managed to get the image to drag and drop into the div area but i am having a couple of if issues

 

1) when i drag an image, it does not fit nicely into the div area

2) the 3 images that are aligned gets floated - once i drag and drop the image - i want to be able to leave the 2 other images where they are so it doesnt move and leaves an empty space where the image has been dragged from

3) I want to be able to cancel the animation on the image that has been dragged so its not floating anymore and then open up a link (in my case a spreadsheet)

 

 

How can i achieve the above without having to learn Jquery as this is way too advanced for me at the minute

 

This is my code

<!DOCTYPE html>
<html>
<head>
	<title>"Drag and Drop"</title> 
<style>
		#data{
			opacity: 0.3;
			width: 100%;
			height: 100%;
		}
		
		#container {
			position: relative;
			width: 1900px;
			height: 950px;
			top:0px;
			left: 0px;
		}
		
		.WipImage {
			position:absolute;
			width: 1300px;
			height: 300px;
			top:100px;
			left: 320px;
		}
		
		.box2_container {
			position:absolute;
			width: 340px;
			height: 270px;
			top:450px;
			left: 730px;
			border: 1px solid red;
		}
		
		#Image1 {
			width: 350px;
			height: 250px;
			top:0px;
			left: 0px;
			float: left;
			margin: 20px;
			box-shadow: 0 5px 15px 0px rgba(0,0,0,0.6);
			transform: translatey(0px);
			animation-name: float;
			animation-iteration-count: infinite;
			animation-duration: 5s;
			animation-timing-function: ease-in-out;
		}
		
		#Image2 {
			width: 350px;
			height: 250px;
			top:250px;
			left: 250px;
			float: left;
			margin: 20px;
			box-shadow: 0 5px 15px 0px rgba(0,0,0,0.6);
			transform: translatey(0px);
			animation-name: float;
			animation-iteration-count: infinite;
			animation-duration: 5s;
			animation-timing-function: ease-in-out;
			animation-delay: 0.5s; 
			
		
		}
		#Image3 {
			width: 350px;
			height: 250px;
			top:250px;
			left: 250px;
			float: left;
			margin: 20px;
			box-shadow: 0 5px 15px 0px rgba(0,0,0,0.6);
			transform: translatey(0px);
			animation-name: float;
			animation-iteration-count: infinite;
			animation-duration: 5s;
			animation-timing-function: ease-in-out;
		}
		
		
		@keyframes float {
			0% {
				box-shadow: 0 5px 15px 0px rgba(0,0,0,0.6);
				transform: translatey(0px);
			}
			50% {
				box-shadow: 0 25px 15px 0px rgba(0,0,0,0.2);
				transform: translatey(-20px);
			}
			100% {
				box-shadow: 0 5px 15px 0px rgba(0,0,0,0.6);
				transform: translatey(0px);
			}
		} 
</style>

<script type="text/javascript">

	function allowDrop(ev){
		ev.preventDefault(); 
	}
	function allowLeave(ev){
		ev.preventDefault(); 
	}

	function drag(ev){
		ev.dataTransfer.setData("content", ev.target.id);
	}

	function drop(ev){
		ev.preventDefault();
		var image = ev.dataTransfer.getData("content");
		ev.target.appendChild(document.getElementById(image));
		ev.target.style.background = "Red";
	}
	
</script>

</head>
<body>

		
		<div id ="container">
				
			<img src="data.jpeg" id="data"/>
			
			<div class="WipImage">
						<img src="WipImage.png" id="Image1" alt="Wip" draggable="true" ondragstart="drag(event)"/>
						<img src="WipImage.png" id="Image2" alt="Wip" draggable="true" ondragstart="drag(event)"/>
						<img src="WipImage.png" id="Image3" alt="Wip" draggable="true" ondragstart="drag(event)"/>
			</div>
		
			<div class="box2_container" ondrop="drop(event)" ondragover="allowDrop(event)" ondragleave="allowLeave(event)">
			
			</div>
		</div>

</body>
</html>


Edited by mahmed1

  • Author

Hi - im not getting paid for this

 

It is for charity and more my development?

 

Not sure why this question was asked though?

Because if you were getting paid for the work then you should really be reimbursing the developer who helped you.

 

Can you post a link to the site so we can see what happening

  • Author

Hi

 

Its for a local charity that i said il try and see if i can do this for them

 

This is more learning purposes- i have provided the code above. Like i said, it's nothing to do with business and i dont get paid for something i am learning. If you can help me then thank you.

Get some cheap hosting, upload and then it's a lot easier for people to help fix the problem.

 

However, you need scripts for this to work as you describe. I did something similar for a client and there was a lot of code required to manage the resizing and ordering.

I'm assuming this is going to be a live website at some point. If you have the domain name then you go buy some cheap hosting and upload the files.

 

But it does seem a bit of overkill to just link to some excel spreadsheets

  • Author

Hi - yes at some point it will go live and for now im just opening that spreadsheet

 

In the near future that might change

 

Is there sny other way you can help me by looking at the code or by me sending you the code?

 

thanks

I'd do some reading about the Drag and Drop API https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API

Don't do inline JavaScript. Instead target your element (The image) and add an event listener to it. If there's a lot of images or they are dynamically added / removed you will want to use event delegation.

function handleDrag(event) {
 // do stuff
}

document.querySelector('#Image1').addEventListener('dragStart', handleDrag)

Edited by rbrtsmith

  • Author

Thank you

 

How would i amend my code to ensure that when i drag the image - it keeps the other images in the same location and it fits nicely into the div im dragging to?

 

I also need want to apply an animation that i have created using a keystroke called shake onto the image i drag once its been dragged to the dop area

 

Ie

 

Drag image

Drop image (fit nicely into drop div)

Leave other images position the same

Apply shake animation on the drag image once dropped

Learn jquery

 

There is no quick and easy fix for this. If you want all these (unnecessary) features you either need to learn the code or the pay someone to do it for you.

  • Author

Thanks for something I already knew - you could have just said pay me and il do it. Hopefully someone else can actually help me

Honestly the advice is good, you need to spend time learning core fundamenals as well as building things otherwise you are just going to be hacking away without any real knowledge of what you are doing.

I've shown you the documentation for the drag and drop API. To use it you really have to have some JavaScript knowledge, we're all here to help but we lead busy lives, our time is very valuable so we cannot hand hold all the time, but we can point you in the right direction.
MDN is a good place to start for learning the DOM API
There are many good (and bad) places to learn JavaScript. I suggest https://github.com/getify/You-Dont-Know-JS to most people who want to learn the language to a professional level.

I wouldn't say drag and drop is pointless either, there are many applications where it provides a very intuitive user experience. Like anything it can be abused though.

Edited by rbrtsmith

It seems that in this case they want drag and drop in order to access other documents. If this is the case one wonders why they want the resizing, shake and other 'features'. This is the bit that seems pointless.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.