Replace elementFromPoint function in IE8

Source: Internet
Author: User
Tags return tag

In elementFromPoint (x, y) of IE8, x and y cannot be non-immediate at the same time, so you can write an alternative function.
[Javascript]
/*
* This function is used to simulate: elementFromPoint.
* Obtain the class attribute tagCls and the best element of the client location (x, y) of the parent element.
* Return el;
*/
GetPerfectTag: function (parent, tagCls, x, y ){

// Filter at the first layer: All elements with tagCls under the parent
Var children = KISSY. DOM. query ('.' + tagCls, parent );
Tags = [];
If (children. length = 0) return null;

// Layer 2 filtering: elements containing (x, y) points
Tags = getInXY (children );

Function getInXY (children ){
Var tags = [];
Hx. each (children, function (item ){
Var x0 = item. offsetLeft,
Y0 = item. offsetTop,
X1 = x0 + item. offsetWidth,
Y1 = y0 + item. offsetHeight;
// This item contains the point where the mouse is located
If (x0 <= x & x1> = x) & (y0 <= y & y1> = y )){
Var obj = {
Item: item,
Zindex: Items (item).css ('zindex') | null,
IsTop: $ (item). children ("." + tagCls). length = 0 // whether its child does not contain tagCls
};
Tags. push (obj );
}
});
Return tags;
}

Var len = tags. length; www.2cto.com
// If all the elements that contain tagCls do not contain (x, y), you need to find all descendant elements of children. Because the child element may deviate from the box model of the parent element.
If (len = 0 ){
Var tag;
Function getAllChildren (el, all ){
Var all = all | [];
If (el = null) return null;
All. push (el );
Hx. each ($ (el). children (), function (item ){
GetAllChildren (item, all );
});
Return all;
}
// Search for all descendant elements that meet tagCls one by one, and put the set into (x, y) for matching. If the matching is successful, the search is successful.
Hx. each (children, function (item ){
Var all = getAllChildren (item );
Var temps = getInXY (all );
If (temps. length> 0 ){
Tag = temps [0]. item;
Return false;
}
});


Return tag;
}

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.