Javascripttips prompt box component implementation code _ javascript skills

Source: Internet
Author: User
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. Tip. js

The Code is as follows:


Function tips (obj, tag ){
Var tip = document. createElement ('P'), 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 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.

The Code is as follows:











Prompt box





Prompt box






I have read the IT article "30 good experiences in improving the efficiency of Web application execution" published by my team. These 30 guidelines are very useful for web development, no, I know it, but I don't know it. The following is my understanding and analysis of these Principles. Some JS performance standards have also been tested for their differences. You can download the DEMO page, if your understanding is incorrect, please advise.



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.