开发者

My tool tip code is displaying "Tip is undefined" in IE 8 when it should be displaying the tool tip.

My tool tip code is displaying "Tip is undefined" in IE 8 when it should be displaying the tool tip. The following code works for all other browsers.

//This is my code

jQuery(".tiptrigger").hover(function(){
    tip = jQuery('#tool_content');
    tip.show(); //Show tooltip
}, function() {
    tip.hide(); //Hide tooltip
}).mousemove(function(e) {
    var mousex = e.pageX + 20; //Get X coodrinates
    var mousey = e.pageY + 20; //Get Y coordinates
    var tipWidth = tip.width(); //Find width of tooltip
    var tipHeight = tip.height(); //Find height of tooltip

    //Distance of el开发者_开发百科ement from the right edge of viewport
    var tipVisX = jQuery(window).width() - (mousex + tipWidth);
    //Distance of element from the bottom of viewport
    var tipVisY = jQuery(window).height() - (mousey + tipHeight);

    if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
        mousex = e.pageX - tipWidth - 20;
    } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
        mousey = e.pageY - tipHeight - 20;
    }
    tip.css({  top: mousey, left: mousex });
});




    <p class="tiptrigger">Sample Tool tip
        <div id="tool_content" class="tip" style="display: none;">
                    Welcome
        </div>
    </p>


Looks like you are using tip as a global variable. Try this

var tip = null;
jQuery(".tiptrigger").hover(function(){
    if(!tip)
      tip = jQuery('#tool_content');
    tip = jQuery('#tool_content');
    tip.show(); //Show tooltip
}, function() {
    if(!tip)
      tip = jQuery('#tool_content');
    tip.hide(); //Hide tooltip
}).mousemove(function(e) {
    if(!tip)
      tip = jQuery('#tool_content');
    var mousex = e.pageX + 20; //Get X coodrinates
    var mousey = e.pageY + 20; //Get Y coordinates
    var tipWidth = tip.width(); //Find width of tooltip
    var tipHeight = tip.height(); //Find height of tooltip

    //Distance of element from the right edge of viewport
    var tipVisX = jQuery(window).width() - (mousex + tipWidth);
    //Distance of element from the bottom of viewport
    var tipVisY = jQuery(window).height() - (mousey + tipHeight);

    if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
        mousex = e.pageX - tipWidth - 20;
    } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
        mousey = e.pageY - tipHeight - 20;
    }
    tip.css({  top: mousey, left: mousex });
});


Going by your example, try:

var tip = jQuery('#tool_content');

jQuery(".tiptrigger").hover(function(){
   tip.show(); //Show tooltip
   //rest of your code
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜