Jquery Div drag simple example

Source: Internet
Author: User

Today, jquery is used to implement a simple drag... the implementation idea is very simple as follows:

 

Event. offsetx eventoffsety: The X coordinate and Y coordinate in the coordinate system of the event source element.

Event. clientx obtains the horizontal position of the mouse.

Event. clienty get the vertical position of the mouse

The ousedown event will trigger the mousemove event after you click the element. Moving the mouse over the element will trigger the mouseout event. After the mouse leaves the element, it will trigger Code

  <  Style  >  
# Drigging
{
Width: 200px;
Background: # CCC;
Border: solid 1px # 666 ;
Height: 80px;
Line - Height: 80px;
Text - Align: center;
Position: absolute;
}
< / Style>
< Script SRC = " ../JS/jquery-1.3.1.js " > < / SCRIPT>
< Script Type = " Text/JavaScript " >
$ ( Function (){
VaR Bool = False ;
VaR Offsetx = 0 ;
VaR Offsety = 0 ;
$ ( " # Drigging " ). Mousedown ( Function (){
Bool = True ;
Offsetx = Event. offsetx;
Offsety = Event. offsety;
$ ( This ).Css ( ' Cursor ' , ' Move ' );
})
. Mouseup ( Function (){
Bool = False ;
})
$ (Document). mousemove ( Function (E ){
If ( ! Bool)
Return ;
VaR X = Event. clientx - Offsetx;
VaR Y = Event. clienty - Offsety;
$ ( " # Drigging " ).Css ( " Left " , X );
$ ( " # Drigging " ).Css ( " Top " , Y );

})
})
< / SCRIPT>

 

 

HtmlCode

 

 
  <Body>
<DivID= "Drigging">You can drag it. It's actually very simple.</Div>
</Body>

 

 

Currently, Firefox is not compatible with Firefox. How can I be compatible with Firefox?

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.