Jquery-based floating box (better scalability)

Source: Internet
Author: User

Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> </title>
<Style type = "text/css">
Body
{
Height: 2000px;
}
. Float_mx {
Background: # CCC;
Width: 100px;
Height: 100px;
Display: none;
}
</Style>
</Head>
<Body>
<Div class = "float_mx">
Add content here
</Div>
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
(Function ($ ){
$. Fn. scrollBox = function (options ){
Var defaultOptions = {
Speed: 0.1, // Acceleration
Time: 16, // movement speed
Top: 200, // default top
Align: 'right', // floating position, optional; left or right
Mix: 0 // margin
};
Var options = $. extend (defaultOptions, options );
This. each (function (){
Var obj = $ (this );
Init ();
Function init (){
Obj.css ('display', 'block ');
Obj.css ('position', 'absolute ');
Obj.css (options. align, options. mix );
Obj.css ('top', options. top + 'px ');
Obj.css ('z-Index', '99 ');
Move ();
}
Function back (){
Acceleration = options. speed;
Time = options. time;
Var x1 = 0;
Var y1 = 0;
Var x2 = 0;
Var y2 = 0;
If (document.doc umentElement ){
X1 = document.doc umentElement. scrollLeft | 0;
Y1 = document.doc umentElement. scrollTop | 0;
}
If (document. body ){
X2 = document. body. scrollLeft | 0;
Y2 = document. body. scrollTop | 0;
}
Var x = Math. max (x1, x2 );
Var y = Math. max (y1, y2 );
Var speed = acceleration;
Return {
L: x,
T: y,
S: speed
};
};

Function move (){
Var tip = obj;
Var old = options. top;
Var pos = back (). t;
Pos = pos-$ (tip). Coordinate (). y + options. top;
Pos = $ (tip). Coordinate (). y + pos/10;
If (pos <options. top ){
Pos = options. top;
}
If (pos! = Old ){
Tip.css ('top', pos + "px ");
}
Old = pos;
Window. setTimeout (function () {move () ;}, options. time );
};
});
};
$. Fn. Coordinate = function (){
Var E = $ (this) [0];
Var C = E. offsetTop;
Var B = E. offsetLeft;
Var A = E. offsetWidth;
Var D = E. offsetHeight;
While (E = E. offsetParent ){
C + = E. offsetTop;
B + = E. offsetLeft;
}
Return {
X: B,
Y: C,
W:,
H: D
};
};
}) (JQuery );
$ ('. Float_mx'). scrollBox ();
</Script>
</Body>
</Html>

Put the following proxy in a separate js file. the css code in the code can be deleted and the display is set to none to avoid incorrect display after the page is loaded.
Copy codeThe Code is as follows:
(Function ($ ){
$. Fn. scrollBox = function (options ){
Var defaultOptions = {
Speed: 0.1, // Acceleration
Time: 16, // movement speed
Top: 200, // default top
Align: 'right', // floating position, optional; left or right
Mix: 0 // margin
};
Var options = $. extend (defaultOptions, options );
This. each (function (){
Var obj = $ (this );
Init ();
Function init (){
Obj.css ('display', 'block ');
Obj.css ('position', 'absolute ');
Obj.css (options. align, options. mix );
Obj.css ('top', options. top + 'px ');
Obj.css ('z-Index', '99 ');
Move ();
}
Function back (){
Acceleration = options. speed;
Time = options. time;
Var x1 = 0;
Var y1 = 0;
Var x2 = 0;
Var y2 = 0;
If (document.doc umentElement ){
X1 = document.doc umentElement. scrollLeft | 0;
Y1 = document.doc umentElement. scrollTop | 0;
}
If (document. body ){
X2 = document. body. scrollLeft | 0;
Y2 = document. body. scrollTop | 0;
}
Var x = Math. max (x1, x2 );
Var y = Math. max (y1, y2 );
Var speed = acceleration;
Return {
L: x,
T: y,
S: speed
};
};

Function move (){
Var tip = obj;
Var old = options. top;
Var pos = back (). t;
Pos = pos-$ (tip). Coordinate (). y + options. top;
Pos = $ (tip). Coordinate (). y + pos/10;
If (pos <options. top ){
Pos = options. top;
}
If (pos! = Old ){
Tip.css ('top', pos + "px ");
}
Old = pos;
Window. setTimeout (function () {move () ;}, options. time );
};
});
};
$. Fn. Coordinate = function (){
Var E = $ (this) [0];
Var C = E. offsetTop;
Var B = E. offsetLeft;
Var A = E. offsetWidth;
Var D = E. offsetHeight;
While (E = E. offsetParent ){
C + = E. offsetTop;
B + = E. offsetLeft;
}
Return {
X: B,
Y: C,
W:,
H: D
};
};
}) (JQuery );

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.