html - 使用 HTML5/CSS3 更改工具提示位置

标签 html css tooltip

我从一个站点获得了一些 CSS3 代码,当鼠标悬停在 anchor 元素 ( <a> ) 上时,它会提供一个漂亮的工具提示框。但是,工具提示总是出现在 <a> 的右上角。 .如果 anchor 位于屏幕顶部,则工具提示会出现在浏览器的上边缘之外,如果 anchor 位于屏幕的右边缘,则工具提示会出现在浏览器的右侧。无论哪种情况,我都看不到大部分工具提示。有没有一种方法可以根据 anchor 离屏幕边缘的距离动态地让工具提示出现在屏幕的可见区域?

这是 CSS 和示例 anchor :

.tooltip {
    border-bottom: 1px dotted #0077AA;
    color: #0077AA;
    cursor: help;
}

.tooltip:hover {
    color: #0099CC;
}

.tooltip:after {
    -moz-transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
    -ms-transition: all 0.4s ease-in-out;
    -o-transition: all 0.4s ease-in-out;
    -webkit-transition: all 0.4s ease-in-out;
    background: rgba(0, 0, 0, 0.8);
    border-radius: 8px 8px 8px 0px;
    box-shadow: 1px 1px 10px rgba(0, 0, 0, 0.5);
    color: #FFF;
    content: attr(data-tooltip);
    margin-top: -24px;
    opacity: 0;
    padding: 3px 7px;
    position: absolute;
    transition: all 0.4s ease-in-out;
    visibility: hidden;
}

.tooltip:hover:after {
    opacity: 1;
    visibility: visible;
}

.htooltip, .htooltip:visited, .tooltip:active {
    color: #0077AA;
    text-decoration: none;
}

.htooltip:hover {
    color: #0099CC;
}

.htooltip span {
    -moz-transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
    -ms-transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
    -o-transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
    -webkit-transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
    background-color: rgba(0, 0, 0, 0.8);
    border-radius: 15px 15px 15px 0px;
    box-shadow: 1px 1px 10px rgba(0, 0, 0, 0.5);
    color: #fff;
    margin-left: 2px;
    margin-top: -75px;
    opacity: 0;
    padding: 10px 10px 10px 40px;
    position: absolute;
    text-decoration: none;
    transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
    visibility: hidden;
    width: 350px;
    z-index: 10;
}

.htooltip:hover span {
    opacity: 1;
    position: absolute;
    visibility: visible;
}

和 HTML:

<a class="htooltip" href="#">WA &mdash; Washington
 <span>Washington State is the northwesternmost state in the contiguous 48.</span>
</a>

更新:

这是我的解决方案的第一个打击,以防它可能对某人有帮助:

    <script type="text/javascript" src="jquery-1.7.2.js"></script>

    <style type="text/css">
        .htooltip, .htooltip:visited, .tooltip:active
        {
            color: #0077AA;
            text-decoration: none;
        }

        .htooltip:hover
        {
            color: #0099CC;
        }

        .htooltip span
        {
            background-color: rgba(0,0,0, 0.8);
            border-radius: 15px 15px 15px 0px;
            box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.5);
            color: #fff;
            opacity: 0;
            padding: 10px 10px 10px 40px;
            position: absolute;
            text-decoration: none;
            visibility: hidden;
            width: 350px;
            z-index: 10;

            -moz-transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
            -webkit-transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
            -o-transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
            -ms-transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
            transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
        }

        .htooltip:hover span
        {
            position: absolute;
            opacity: 1;
            visibility: visible;
        }
    </style>


<a onmouseover="SetTopLeft(this);" class="htooltip" href="#">WA &mdash; Washington<span>Washington State is the northwesternmost state in the contiguous 48.</span></a>


    <script type="text/javascript">
        function SetTopLeft(obj) {
            var mouseX = $(obj).offset().left;
            var mouseY = $(obj).offset().top - $(window).scrollTop();
            var screenWidth = $(window).width();
            var screenHeight = $(window).height();

            var top;
            var left;

            // If the tooltip would be too close to the top of the browser, show it below the element.
            if (screenHeight - mouseY > screenHeight - 50)
            {
                top = mouseY + 17;
            }
            else
            {
                top = mouseY - 60;
            }

            if (screenWidth - mouseX < 350 && mouseX < 350) // Close to the left and the right - center the span over the element.
            {
                left = mouseX / 2;
            }
            else if (screenWidth - mouseX < 350) // Close to the right - put span to the left.
            {
                left = mouseX - 350;
            }
            else
            {
                left = mouseX + 50; // Close to the left with extra space on the right - put span to the right.
            }

            $(obj).find("span").css("top", top);
            $(obj).find("span").css("left", left);
        }
    </script>

最佳答案

查看此 jsfiddler .我添加了以下几行:

.htooltip span {
    position: relative;
    top: 6em;
    left: 5em;
    /* ... */
}

关于html - 使用 HTML5/CSS3 更改工具提示位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10803590/

相关文章:

CSS bootstrap 悬停工具提示不起作用

javascript - 如何使用相同的 script.js 文件通过多个 ID 调用不同的 CSS 类

php - Wordpress PHP 和 HTML5 详解

php动态访问表单变量

css - 如何让每个Div都是占据整个Body高度的一个section?

CSS 工具提示不适用于 slider

javascript - 我在使用复选框时遇到问题

CSS:在底部设置动态高度标题和内容区域的最佳方式

jquery - 当隐藏的侧面板可见时如何将按钮放在侧面

tooltip - 将自定义文本添加到 Google 可视化工具提示