1

我正在尝试找出要添加到此脚本中的内容,以防止工具提示超出页面底部的边缘。非常感谢任何帮助和您的时间,谢谢。

<!-------------SCRIPT---------------->

    <script>
    $(document).ready(function() {
            // Tooltip only Text
            $('.masterTooltip').hover(function(){
                    // Hover over code
                    var title = $(this).attr('title');
                    $(this).data('tipText', title).removeAttr('title');
                    $('<p class="tooltip"></p>')
                    .text(title)
                    .appendTo('body')
                    .fadeIn('slow');
            }, function() {
                    // Hover out code
                    $(this).attr('title', $(this).data('tipText'));
                    $('.tooltip').remove();
            }).mousemove(function(e) {
                    var mousex = e.pageX + 20; //Get X coordinates
                    var mousey = e.pageY + 10; //Get Y coordinates
                    $('.tooltip')
                    .css({ top: mousey, left: mousex })
            });
    });
    </script>



 <!--------------CSS------------------>   
    <style>
    .tooltip {
        display:none;
        position:absolute;
        border:1px solid #333;
        background-color:#161616;
        border-radius:5px;
        padding:10px;
        color:#fff;
        font-size:12px Arial;
    }
    </style>


<!------------------HTML----------------->    
    <a href="#" title="This will show up in the tooltip" class="masterTooltip">Your Text</a>
    <p title="Mouse over the heading above to view the tooltip." class="masterTooltip">Mouse over the heading text above to view it's tooltip.</p>
    <img src="image.jpg" class="masterTooltip" title="Tooltip on image" />

演示:http: //jsfiddle.net/KP7mT/

4

1 回答 1

0

您需要检查页面的高度,如果菜单的高度 + 菜单(顶部)的 Y 位置大于该高度,则调整菜单的顶部位置。

var boxX = e.pageX;
var boxY = e.pageY;
var menuHeight = $('.tooltip').outerHeight();
var bodyHeight = $('body').height();
if ((boxY + menuHeight) > bodyHeight){
    boxY = bodyHeight - menuHeight;
}
$('.tooltip').css({ top: boxY + 'px', left: boxX + 'px' })
于 2013-02-27T23:29:29.837 回答