0

JQUERY NINJAはありますか?

IEでエラーが発生しますか?

'tip' is null or not an object

小さなスクリプトは次のとおりです。

  $(document).ready(function() {
       //Tooltips
        var tip;
        $(".tip_trigger").hover(function(){

            //Caching the tooltip and removing it from container; then appending it to the body
            tip = $(this).find('.tip').remove();
            $('body').append(tip);

            tip.show(); //Show tooltip


        }, function() {

            tip.hide().remove(); //Hide and remove tooltip appended to the body
            $(this).append(tip); //Return the tooltip to its original position

        }).mousemove(function(e) {
        //console.log(e.pageX)
              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 = $(window).width() - (mousex + tipWidth);
              var tipVisY = $(window).height() - (mousey + tipHeight);

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

    });

更新されたコード:(更新されたコードをこれに統合できないようです)

$(document).ready(function() {
   //Tooltips
    var tip = null;

    $(".tip_trigger").hover(function(){

        //Caching the tooltip and removing it from container; then appending it to the body
        tip = $(this).find('.tip').remove();
        $('body').append(tip);

        tip.show(); //Show tooltip


    }, function() {

        tip.hide().remove(); //Hide and remove tooltip appended to the body
        $(this).append(tip); //Return the tooltip to its original position

    }).mousemove(function(e) {
    //console.log(e.pageX)
          if ( tip == null ) return;


          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 = $(window).width() - (mousex + tipWidth);
          var tipVisY = $(window).height() - (mousey + tipHeight);

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

});
4

2 に答える 2

6
$(function() {

    $('.tip_trigger').each(function() {        
        var tip = $(this).find('.tip');

        $(this).hover(
            function() { tip.appendTo('body'); },
            function() { tip.appendTo(this); }
        ).mousemove(function(e) {
            var x = e.pageX + 20,
                y = e.pageY + 20,
                w = tip.width(),
                h = tip.height(),
                dx = $(window).width() - (x + w),
                dy = $(window).height() - (y + h);

            if ( dx < 20 ) x = e.pageX - w - 20;
            if ( dy < 20 ) y = e.pageY - h - 20;

            tip.css({ left: x, top: y });
        });         
    });

});

ライブデモ: http://jsfiddle.net/vNBNz/4/

ご覧のとおり、上記のコードは機能します。ライブ デモでは、次の CSS ルールに注意してください。

.tip_trigger .tip { display:none; }

上記のルールはすべての.tip要素を非表示にしますが、それらが要素内にある場合のみです.tip_trigger。これは、要素を BODY に追加するとすぐ.tipに表示されることを意味します。これは、「非表示ルール」が.tip内の要素にのみ適用されるため.tip_triggerです。

于 2011-02-12T20:24:14.870 に答える
2

これは、IE ではmove前に発生する可能性があるためhoverです。この例を試してみてください。Chrome でこの機能を再現しませんでした。私の IE テストは IE9 ベータ版でした。

したがって、私の推測では、この行で失敗していると思います:

var tipWidth = tip.width();

mousemove のコールバックvar tip;はまだundefined.

于 2011-02-12T20:05:08.313 に答える