Resize div box horizontally using jquery draggable

This article will explain how to write the code to dynamically resize the two horizontal divs on dragging the center bar between two div’s to make the div’s height arranged by itself. To achieving this dynamic resizing functionality we used JQuery UI draggable to adjust the div’s size.

Let’s start the tutorial, about how to change the two horizontal div’s height automatically when dragging the divider using jquery ui draggable.

Include the dependency

Below js files will be in the download folder.

<script src="js/1.12.4/jquery.min.js"></script>
<script src="js/1.12.4/jquery-ui.js"></script>

Please the below html code where you want to see the demo.

<div id="mainContent" class="mainContent">
		 <div id="mainContentHolder">
		 	<div id="draggableH"></div>
		 	<div id="main-left" class="main-top">
		 		<div class="left-inner-content">
		 			<div class="left-inner-main">
		 				<h2>Top</h2>
		 			</div>
		 		</div>
		 	</div>
		 	<div id="main-right" class="main-bottom">
		 		<div class="right-inner-content">
		 			<div class="right-inner-main">
		 				<h2>Bottom</h2>
		 			</div>
		 		</div>
		 	</div>
	 	</div>
	 </div>

Include the below css code

body,html{
	height: 100%;
	font-size: 12px;
	margin: 0;
	padding: 0;
}
* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
ul{
	margin: 0;
	padding: 0;
}
.header{
	overflow: hidden;
	min-width: 380px;
	width: 100%;
	background: #efefef;
	padding: 10px 0;
    height: 110px;
}
.header ul{
	overflow: hidden;
	margin-left: 10px;
	padding: 8px 0;
}
.header li{
	list-style: none;
	display: inline;
	float: left;
}
.header li.stackH,.header li.stackV{
	background: url("../img/layout_change_icon.png");
	width: 24px;
    height: 24px;
    cursor: pointer;
}
.header li.stackV{
	margin-left: 5px;
}
.header li.stackH {
    background-position: -25px 0px;
}
.mainContent{
    overflow: hidden;
    background: #efefef;
}
.left-inner-content,.right-inner-content{
    padding-left: 10px;
    padding-right: 10px;
}
.left-inner-main,.right-inner-main{
	background: #FFF;
	overflow: auto;
}
.left-inner-main div,.right-inner-main div{
	height: initial;
}
#mainContentHolder{
	position: relative;
}
.main-top,.main-bottom{
	overflow: auto;
	height: 370px;
	background: #fff;
    margin-left: 10px;
    margin-right: 10px;
}
.main-bottom{
	margin-top: 10px;
}
#draggableH{
	position: absolute;
    z-index: 3;
    width: 10px;
    cursor: row-resize;
    background: transparent;
    height: 10px;
    width: 100%;
    background: #efefef;
}

Please the below resizable javascript function in your html file

$( document ).ready(function() {
    		// Initial call
		    resizeWindow();

		    $( window ).resize(function() {
		  		resizeWindow()
			});

			function calculatepercent(position) {
				var a = position;

				$('div.main-top').height(position);
				$('div.main-bottom').height($("#mainContent").height() - (position + 20));
			};

			$( "#draggableH" ).draggable({ 
				axis: "y",
				start: function(a) {
					calculatepercent(a.target.offsetTop);
				},
				drag: function(b) {
					calculatepercent(b.target.offsetTop);
				},
				stop: function(c) {
					calculatepercent(c.target.offsetTop);
				}
			});

			function resizeWindow(){
				$("#mainContent").height($("body").height() - $(".header").height());

				var height = (($("body").height() - $(".header").height())/2)-10;

		    	$("#draggableH").css({
				   'top'	: height
				});

		    	$(".main-top").css({
				   'height'	: height
				});

		    	$(".main-bottom").css({
				   'height'	: height
				});
			};
		});

Download Demo

W3TWEAKS
Latest posts by W3TWEAKS (see all)

Comments

  1. Howardameve Avatar

    Your comment is awaiting moderation.

    Dressing stylishly is essential for making a good impression.
    It helps highlight individuality and boost self-esteem.
    A coordinated look shapes first impressions.
    In daily life, clothing can add confidence.
    https://telegra.ph/Gucci-12-25-8
    Stylish outfits support professional encounters.
    It is important to consider personal preferences and suitability for the occasion.
    Modern styles allow people to refresh their wardrobe.
    In conclusion, dressing stylishly completes your personal image.

  2. общество экспертов России по недропользованию Avatar

    Your comment is awaiting moderation.

    Недропользование — это комплекс работ, связанный с разработкой подземных богатств.
    Оно включает разведку полезных ископаемых и их промышленное освоение.
    Данная сфера регулируется установленными правилами, направленными на безопасность работ.
    Грамотный подход в недропользовании способствует экономическому росту.
    оэрн

  3. оэрн Avatar

    Your comment is awaiting moderation.

    Сфера недропользования — это направление деятельности, связанный с изучением и использованием недр.
    Оно включает разведку природных ресурсов и их рациональное использование.
    Данная сфера регулируется нормативными актами, направленными на охрану окружающей среды.
    Ответственное ведение работ в недропользовании способствует экономическому росту.
    оэрн

Leave a Reply

Your email address will not be published. Required fields are marked *