Ext JS自定义Tooltip类无法正常工作

时间:2015-03-09 22:50:22

标签: javascript html css extjs tooltip

我使用了Ext的快速提示来制作这个自定义工具提示,当有一个带有' my_tip'的元素时,它应显示工具提示。属性。

例如,

<div class="some class" my_tip="This is my tip text">

加载页面时,会将以下标记添加到HTML中,这是正确的:

<div class="x-layer my_tool_tip_layer" id="ext-gen7" style="position: absolute; z-index: 20000; visibility: hidden; left: -10000px; top: -10000px;">
    <div class="my_tool_tip" id="ext-gen8">&nbsp;</div>
</div>

当我将鼠标悬停在带有&#39; my_tip&#39;的元素上时,会发生什么?属性,它应该更新上面div的样式属性,并用my_tip类的内容替换。&lt; nbsp。

像这样:

<div class="x-layer my_tool_tip_layer displayed" id="ext-gen7" style="position: absolute; z-index: 20000; visibility: visible; left: 160px; top: 30px;">
    <div class="my_tool_tip" id="ext-gen8">This is my tip text</div>
</div>

我有一个css文件处理工具提示的样式,但我现在的问题是让html更新为正确的事件,而不是这样做。

以下是工具提示的javascript。非常感谢任何帮助。

谢谢

myToolTips=function()
{
    var mytt_ext_lyr=null;
    var mytt_text_el=null;
    var mytt_visible=false;
    var mytt_hide_proc=null;
    var mytt_show_proc=null;
    var mytt_tip_text='';
    var mytt_hide_delay = 10000;
    var mytt_show_delay = 200;
    var mytt_started = false;
    var mytt_last_xy = null;
    var mytt_mouse_y_offset = 20;
    var mytt_track_mouse = false;
    var mytt_initialized = false;
    var mytt_singleton = null;
    var mytt_max_search_depth = 5;

    var on_over = function(evt) {
        var target = evt.target;
        var depth = 0;

        while (target && target.nodeType == 1 && !target.getAttribute('my_tip') && depth < mytt_max_search_depth) {
            ++depth;
            target = target.parentNode;
        }

        if (!target || target.nodeType != 1 || !target.getAttribute('my_tip')) {
            if (mytt_visible)
                hide();
            return;
        }

        var new_tip = target.getAttribute('my_tip');
        if (new_tip != mytt_tip_text && mytt_visible)
            hide();
        if (target.getAttribute('clip_tip') && target.getAttribute('clip_tip') == 'true') {
            for (var i = 0; i < target.childNodes.length; ++i) {
                var child = target.childNodes[i];
                if (child.scrollWidth <= child.offsetWidth) {
                    return;
                } else {
                    break;
                }
            }
        }
        if (target.getAttribute('clip_tip') && target.getAttribute('clip_tip') == 'this_node') {
            if (target.scrollWidth <= target.offsetWidth)
                return;
        }
        var mouse_xy = evt.getXY();
        var tip = {mouse_xy: mouse_xy, new_tip: new_tip, target: target}
        mytt_show_proc = show.defer(mytt_show_delay, mytt_singleton, [tip]);
        mytt_started = true;
    };

    var on_move = function (evt) {
        if (mytt_visible) {
            if (mytt_track_mouse) {
                var mouse_xy = evt.getXY();
                mouse_xy[1] += mytt_mouse_y_offset;
                mytt_ext_lyr.setXY(mouse_xy);
            }
        }
        else if (mytt_started)
            mytt_last_xy = evt.getXY();
    };

    var on_out=function(evt) {
        hide();
    };

    var on_click = function(evt) {
        var target = evt.target;
        var depth = 0;
        while (target && target.nodeType == 1 && !target.getAttribute('my_tip') && depth < mytt_max_search_depth) {
            ++depth;
            target = target.parentNode;
        }
        if (!target || target.nodeType != 1 || !target.getAttribute('my_tip'))
            hide();
    };

    var show=function(mytt) {
        var tip=mytt.new_tip;
        var mouse_xy=mytt.mouse_xy;
        if(mytt_last_xy)
            mouse_xy=mytt_last_xy;
        if(!mytt_visible) {
            if(tip) {
                mytt_tip_text=tip;
                mytt_text_el.update(mytt_tip_text.replace(/\[\[/g,"<").replace(/\]\]/g,">"));
            }
            mytt_ext_lyr.show();
            mytt_ext_lyr.addClass('displayed');
            mytt_visible=true;
            if(!mytt_hide_proc)
                mytt_hide_proc=setTimeout(hide,mytt_hide_delay);
        }
        var db=Ext.get(document.body);
        var dw=db.getWidth();
        var dh=db.getHeight();
        if (mytt.target.getAttribute('top_tip') === 'true') {
            var telm = Ext.get(mytt.target);
            var left = telm.getLeft();
            var width_diff = db.getWidth() - (left + mytt_ext_lyr.getWidth());
            if (width_diff < 5)
                left+=width_diff-15;
            mytt_ext_lyr.dom.style.right='';
            mytt_ext_lyr.dom.style.top='';
            mytt_ext_lyr.dom.style.left=left+'px';
            mytt_ext_lyr.dom.style.bottom=(dh-telm.getTop())+'px';
            mytt_ext_lyr.sync();
        }
        else {
            mouse_xy[1]+=mytt_mouse_y_offset;
            var width_diff=db.getWidth()-(mouse_xy[0]+mytt_ext_lyr.getWidth());
            if(width_diff<5)
                mouse_xy[0]+=width_diff-15;
            mytt_ext_lyr.setXY(mouse_xy);
        }
        if(mytt_show_proc) {
            clearTimeout(mytt_show_proc);
            mytt_show_proc=null;
        }
    };

    var hide=function() {
        mytt_ext_lyr.hide();
        mytt_ext_lyr.removeClass('displayed');
        mytt_visible=false;
        mytt_started=false;
        mytt_last_xy=null;
        if(mytt_hide_proc) {
            clearTimeout(mytt_hide_proc);
            mytt_hide_proc=null;
        }
        if(mytt_show_proc) {
            clearTimeout(mytt_show_proc);
            mytt_show_proc=null;
        }
    };

    return {
        init:function() {
            mytt_singleton=myToolTips;
            if(!mytt_initialized) {
                if(!Ext.isReady) {
                    Ext.onReady(myToolTips.init,myToolTips);
                    return;
                }
                mytt_ext_lyr=new Ext.Layer({cls:'my_tool_tip_layer',shim:true,constrain:true});
                mytt_ext_lyr.fxDefaults={stopFx:true};
                mytt_ext_lyr.update('<div class="my_tool_tip">&nbsp;</div>');
                mytt_text_el=mytt_ext_lyr.child('div.my_tool_tip');
                mytt_text_el.update('&nbsp;');
                var doc=Ext.get(document);
                doc.on('mouseover',on_over);
                doc.on('mouseout',on_out);
                doc.on('mousemove',on_move);
                doc.on('click',on_click);
                mytt_initialized=true;
            }
        },

        ready:function() {
            return mytt_initialized;
        }
    };
}();


// make tooltips work
myToolTips.init();

0 个答案:

没有答案