Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to create a container that contains several small boxes inside. The boxes can be moved inside the container, but they cannot be removed from it. Small boxes are placed next to each other and do not collapse. There is a button to add small boxes, which takes the length and width of the box as input and adds it to the container, and also the total length and width of all the small boxes must be less than the length and width of the container, and if when adding Small box, the length or width of the total of small boxes exceeds the length and width of the container, the user will encounter an error. Give me the code in html, css, and javascript.


What I have tried:

This is a simple template for one box in the container, I want to add more boxes inside it till total length and width of all the small boxes are less than the length and width of the container:

HTML
<pre><!DOCTYPE html>
<html>
	<head>
		<style>
			#container {
				position: absolute;
				background-color: blue;
			}
			#elem {
				position: absolute;
				background-color: green;
				-webkit-user-select: none;
				-moz-user-select: none;
				-o-user-select: none;
				-ms-user-select: none;
				-khtml-user-select: none;
				user-select: none;
			}
		</style>
		<script>
			var mydragg = (function () {
				return {
					move: function (divid, xpos, ypos) {
						divid.style.left = xpos + "px";
						divid.style.top = ypos + "px";
					},
					startMoving: function (divid, container, evt) {
						evt = evt || window.event;
						var posX = evt.clientX,
							posY = evt.clientY,
							divTop = divid.style.top,
							divLeft = divid.style.left,
							eWi = parseInt(divid.style.width),
							eHe = parseInt(divid.style.height),
							cWi = parseInt(
								document.getElementById(container).style.width
							),
							cHe = parseInt(
								document.getElementById(container).style.height
							);
						document.getElementById(container).style.cursor =
							"move";
						divTop = divTop.replace("px", "");
						divLeft = divLeft.replace("px", "");
						var diffX = posX - divLeft,
							diffY = posY - divTop;
						document.onmousemove = function (evt) {
							evt = evt || window.event;
							var posX = evt.clientX,
								posY = evt.clientY,
								aX = posX - diffX,
								aY = posY - diffY;
							if (aX < 0) aX = 0;
							if (aY < 0) aY = 0;
							if (aX + eWi > cWi) aX = cWi - eWi;
							if (aY + eHe > cHe) aY = cHe - eHe;
							mydragg.move(divid, aX, aY);
						};
					},
					stopMoving: function (container) {
						var a = document.createElement("script");
						document.getElementById(container).style.cursor =
							"default";
						document.onmousemove = function () {};
					},
				};
			})();
		</script>
	</head>
	<body>
		<div
			id="container"
			style="width: 600px; height: 400px; top: 50px; left: 50px"
		>
			<div
				id="elem"
				onmousedown='mydragg.startMoving(this,"container",event);'
				onmouseup='mydragg.stopMoving("container");'
				style="width: 200px; height: 100px"
			>
				<div style="width: 100%; height: 100%; padding: 10px"></div>
			</div>
		</div>
	</body>
</html>
Posted
Updated 5-Apr-23 23:08pm
Comments
Dave Kreskowiak 6-Apr-23 10:12am    
Nobody is going to write the code for your homework for you. If they did, it wouldn't be helping you, but would only be making the next assignment more difficult.

The point of the assignments are to think about the problems, and nobody can do that for you.

1 solution

Quote:
Give me the code in html, css, and javascript.
Sorry, that is not how this forum operates. You write the code and people here will try to help you when you have a problem. But we do not provide code to order; especially when demanded in such a peremptory fashion.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900