Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » JavaScript » Draggable Divs

Draggable Divs

Make your divs draggable using JavaScript.

Page Views : 4397
Downloads : 116
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
drag.zip
 
 
Mindcracker MVP Summit 2012
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

In web development today we use more JavaScript/DOM/CSS/DHTML for rich content and user experience.

 

While presenting the content/data we may often use divs for division/section in a document. At some time we may need to add drag and drop features or simply dragging feature for our divs. For that we can use JavaScript frameworks like rico, scriptaculous, moo.fx etc. but for the only reason for adding draggable feature for the div do we really need to add those frameworks into our project/site, where size and managing/maintaining code is a matter.

 

For that I'm using the following code for adding dragging functionality to my divs.

I've found some code in the net and tinkered/added to suit my needs and also removed the complexity of changing the existing code to add dragging feature.

 

If you have an existing page/site and want to add dragging to the divs there is nothing to change other than the following two steps.

 

  • Include the JavaScript in the page
  • Add a simple bit class= "drag" to the existing div

If the div has already a css class then do like this
class="yourexisting_css_class drag"

For Example:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<HTML>

    <HEAD>

        <TITLE></TITLE>

    <style>

    .Text

    {

        font-family: Verdana,Arial,Sans-serif,'Times New Roman';

        font-size: 9pt;

        font-weight: normal;

        font-style: normal;

        color: #666;

        text-decoration: none;

    }

 

    .alertsBoxTitle

    {

        font-family: Verdana,Arial,Sans-serif,'Times New Roman';

        font-size: 9pt;

        font-weight: bold;

        font-style: normal;

        color: #ffffff;

        text-decoration: none;

    }

 

    .alertsBox

    {

        background: #ECF1F9;

        border: 1px #a1bcdf solid;

    }

    body

    {

        overflow:hidden;

    }

    </style>

 

        <script src="drag.js" type="text/javascript">function Button1_onclick()

        {

        }

        function dialog_table_onclick()

        {

        }

        </script>

       

    </HEAD> 

    <BODY>

some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here some content here

    <div id="Dialog" style="width:316px;height:119px;max-width:316px;position:absolute;top:50px;left:140px; z-index:1000;" class="drag">

      <table width="100%" style="width:315px;height:119px" cellspacing="0" cellpadding="0" class="alertsBox" id="dialog_table" onclick="return dialog_table_onclick()">

        <tr style="cursor:move">

          <td class="alertsBoxTitle notselectable" colspan="2" align="left" height="21" style="cursor:move;background-color:#32426F">Drag Me</td>

        </tr>

        <tr>

          <td align="center" colspan="2" height="5"> </td>

        </tr>

        <tr>

          <td align="center" colspan="2">

            <table width="97%"  border="0" cellspacing="0" cellpadding="0" align="center">

              <tr>

                <td valign='top' align="center"></td>

              </tr>

              <tr>

                <td valign='top' colspan="2" class="Text">You can place text here</td>

              </tr>

              <tr>

                <td valign='top' colspan="2" align="center"><br/>

                  <input type="button" value="Ok" id="Button1" onclick="return Button1_onclick()"/>

                </td>

              </tr>

            </table>

          </td>

        </tr>

      </table>

    </div>

  </BODY>

</HTML>

 

 

Figure 1: Output of the script.

 

Note:

 

You can define the bounds of the area in which the div can be dragged. As of now I've done for dragging inside the entire page.

Also you can add any content in the div like image, text etc as we normally do.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Kumar S
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.