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?