IE8下的替代elementFromPoint函數

來源:互聯網
上載者:User

因為IE8下的elementFromPoint(x, y)中,x, y 不能同時為非立即數,所以自己寫一個替代的函數。
[javascript] 
/*
         * 該函數用於模仿:elementFromPoint.
         * 擷取parent這個元素下,含有tagCls這個class屬性,又包含(x, y)這個client位置的最佳元素。
         * return el;
         */ 
        getPerfectTag : function(parent, tagCls, x, y) { 
             
            // 第一層篩選:parent下,所有含tagCls的元素 
            var children = KISSY.DOM.query('.' + tagCls, parent);  
                tags = []; 
            if(children.length === 0) return null; 
             
            // 第二層篩選: 包含(x, y)點的元素   
            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; 
                    // 該item包含了滑鼠所在的點 
                    if( (x0<=x && x1>=x) && (y0<=y && y1>=y)) { 
                        var obj = { 
                            item : item, 
                            zindex : $(item).css('zindex') || null, 
                            isTop : $(item).children("." + tagCls).length === 0 // 它的孩子是否已經不含有tagCls 
                        }; 
                        tags.push(obj); 
                    } 
                }); 
                return tags; 
            } 
             
            var len = tags.length;  www.2cto.com
            // 如果所有包含tagCls的元素,均不包含(x, y),那麼需要尋找children的所有後代元素。因為可能子項目偏離父元素的盒子模型。 
            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; 
                } 
                // 逐個尋找滿足tagCls的元素的所有後代元素,並把該集合放入(x, y)進行匹配,匹配成功則算是尋找成功。 
                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; 
            }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.