Tip. js
Copy codeThe Code is as follows:
Function tips (obj, tag ){
Var tip = document. createElement ('div '), arg = arguments [2], left, top; // create tipbox
Var bodywid = document.doc umentElement. clientWidth; // You can also replace it with the container width $ (id). outerWidth ();
Var abs = obj. getElementsByTagName (tag );
Tip. className = "tip_bd ";
Obj. appendChild (tip );
For (var I = 0, len = abs. length; I <len; I ++ ){
Hover (abs [I], function (){
Var content = arg | this. getAttribute ('tip '), text;
Left = position (this). left, top = position (this). top;
Content? Tip. innerHTML = content: tip. innerHTML = "NO content! ";
If (left + parseInt (getStyle (tip, 'width')> bodywid) // you can determine whether the current position exceeds the maximum width.
Text = 'right: '+ (bodywid-left) + 'px; left: auto ;';
Else
Text = 'left: '+ (left + this. offsetWidth) + 'px ;';
Text + = 'top: '+ (top + this. offsetHeight) + 'px ;';
Tip.style.css Text = text;
Text = '';
Tip. style. display = 'block ';
}, Function (){
Tip. style. display = 'none ';
});
}
}
Function hover (el, fnOver, fnOut) {// move the mouse over the function
AddEvent (el, 'mouseover', fnOver );
AddEvent (el, 'mouseout', fnOut );
}
Function addEvent (el, type, fn) {// bind an event
If (el. attachEvent ){
El ['E' + type + fn] = fn; // copy element references under IE to point this to the el object instead of the window
El [type + fn] = function () {el ['E' + type + fn] (window. event );}
El. attachEvent ('on' + type, el [type + fn]);
} Else
El. addEventListener (type, fn, false );
}
Function position (el) {// absolute position of the dom Node
If (el & el. nodeType = 1)
Return {'left': el.getboundingclientrect().left?document.doc umentElement. scrollLeft, 'top': el.getboundingclientrect().top;document.doc umentElement. scrollTop };
}
Function getStyle (obj, styleName) {// get the current style attribute
If (obj. currentStyle) // ie
Return obj. currentStyle [styleName];
Else {// ff
Var $ arr = obj. ownerDocument. defaultView. getComputedStyle (obj, null );
Return $ arr [styleName];
}
}
Tips (document. getElementById ('tids'), 'A ');
A simple title-like tip effect, but the actual content can be very rich. The above js is saved as tip. js, And the demo is used below.
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Meta http-equiv = "X-UA-Compatible" content = "IE = EmulateIE7"/>
<Meta name = "copyright" content = ""/>
<Meta name = "keywords" content = ""/>
<Meta name = "description" content = ""/>
<Title> prompt box </title>
<Link rel = "stylesheet" type = "text/css" href = "style/css/tip.css"/>
<Style>
# Wrap {line-height: 22px; padding: 20px ;}
# Tips. tip_bd {border: 1px solid green; width: 100px; position: absolute; background: # fff; z-index: 9999; text-align: center; display: none ;}
# Tips {border: 1px solid # ccc; padding: 0 10px ;}
</Style>
</Head>
<Body>
<H1> prompt box <Br/>
<Div id = "tips">
I have read <a href = "#" tip = ""> </a> the IT article "30 good experiences in improving the efficiency of Web application execution" published today. these 30 rules are very useful for web development, no, I know it, but I do not know what it is. <a href = "#" tip = "文2 2 2"> 文< </a>. The following is my understanding and analysis of these Principles. Some JS performance standards have also been tested for their differences in <a href = "#"> scripts </a>, you can download the DEMO page. If your understanding is incorrect, please advise.
</Div>
</Body>
<Script type = "text/javascript" src = "tips. js"> </script>
</Html>