jquery Implementation div Drag-and-drop width sample code

Source: Internet
Author: User
This example for everyone to demonstrate a relatively simple div drag, in addition to their own needs, add the corresponding code to achieve their desired effect, specific as follows, like please support under

This example is a very simple div drag, the need for friends can be based on their own needs, add the appropriate code. Welcome to the Brick

Copy Code code as follows:




<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >


<html style= "height:100%;" >


<head>


<meta http-equiv= "Content-type" content= "text/html"; Charset=utf-8 ">


<title>div width resize</title>


<!--reference jquery-->


<script src= "Http://code.jquery.com/jquery-1.8.0.min.js"


type= "Text/javascript" ></script>


<script type= "Text/javascript" >


function Bindresize (EL)


{


//Initialization Parameters


var els = document.getElementById (' menu '). style;


//mouse X and Y axis coordinates


x = 0;


//Evil index finger


$ (EL). MouseDown (function (e)


{


//When the element is pressed, calculates the coordinates of the current mouse and object


x = e.clientx-el.offsetwidth-$ ("#menu"). width ();


//In support of setcapture to do some stuff


el.setcapture? (


//Capture Focus


el.setcapture (),


//Set event


el.onmousemove = function (EV)


{


mouseMove (EV | | | event);


},


el.onmouseup = mouseUp


) : (


//Binding event


$ (document). Bind ("MouseMove", MouseMove). Bind ("MouseUp", MouseUp)


);


//Prevent default events from occurring


E.preventdefault ();


});


//Mobile event


function MouseMove (e)


{


//Universe Super Invincible operation ...


els.width = e.clientx-x + ' px ';


}


//Stop event


function mouseUp ()


{


In support of releasecapture do some stuff


el.releasecapture? (


//Release Focus


el.releasecapture (),


//Removal event


El.onmousemove = El.onmouseup = null


) : (


//Uninstall Event


$ (document). Unbind ("MouseMove", MouseMove). Unbind ("MouseUp", MouseUp)


);


}


}


var divresize=function () {


var totalheight=$ ("html"). Height ();


Console.log (totalheight);


var topheight=$ ("#top"). Height ()


$ ("#menu"). Height (totalheight-topheight);


$ ("#rightbar"). Height (totalheight-topheight);


}


$ (function () {


divresize ();


$ (window). Resize (divresize);





bindresize (document.getElementById (' Rightbar '));


});


</script>


<style type= "Text/css" >


. Content {


width:200px;


background: #f1f1f1;


Text-align:center;


Border-color: #CCCCCC;


Border-style:solid;


border-width:0 1px;


}


</style>


</head>


<body style= "padding:0; margin:0; " >


<%--


<table style= "height:100%" >


<tr>


<td id= "menu" class= "Content" ></td>


<td id= "Rightbar"


style= "WIDTH:2PX; Background: #cccccc; Cursor:e-resize; " ></td>


</tr>


</table>


--%>


<div>


<div id= "Top" style= "width:100%; height:80px; " ></div>


<div style= "Float:left" id= "menu" class= "Content" >


<span> div</span>
to be dragged

</div>


<div id= "Rightbar"


style= "WIDTH:2PX; Background: #cccccc; Cursor:e-resize; Float:left; " ></div>


</div>


</body>


</html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.