【问题标题】:Center Div and Javascript Tooltip problemCenter Div 和 Javascript Tooltip 问题
【发布时间】:2011-07-24 07:17:44
【问题描述】:

目前我正在使用名为EZPZ tooltips 的javascript 工具提示。这是 EZPZ 工具提示的demo 页面。

我有这种带有 html 和 css 的布局。

最大的 div 与 margin-left:auto 相对位置;和边距右:自动;并且在小盒子里面是绝对位置。

这是这个 html 页面的完整代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>The EZPZ Way – EZPZ Tooltip </title>
    <link href="css/application.css" media="screen" rel="stylesheet" type="text/css">
    <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
    <script type="text/javascript" charset="utf-8" src="js/jquery.ezpz_tooltip.js"></script>



<script type="text/javascript" charset="utf-8">
    $(document).ready(function(){

        $("#stay-target-1").ezpz_tooltip({
            contentPosition: 'belowStatic',
            stayOnContent: true,
            offset: 0
        });


    });
</script>

<style type="text/css" media="screen">

    .wrapper {
    position:relative;
    width:800px;
    height:600px;
    border:1px solid #000;
    margin-left: auto;
    margin-right: auto;
    }

    h3 { margin-top: 20px; }
    .tooltip-target {
        display: block;
        padding: 10px;
        background-color: #EEE;
        text-align: center;
        width:100px;
        position:absolute;
        top:100px;
        right:100px;
    }
    .tooltip-content {
        display: none;      /* required */
        position: absolute; /* required */
        width: 250px;
        padding: 10px;
        border: 3px solid #AF8A31;
        background-color: #FFC848;
        text-align: center;
        color: black;
    }
    .tooltip-content p {
        margin: 0;
    }


</style>

</head>

<body>

<div class="wrapper">

    <div class="tooltip-target" id="stay-target-1">Stay on Content ToolTip</div>
    <div style="top: 1455px; left: 190px; display: none;" class="stay-tooltip-content tooltip-content" id="stay-content-1">
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet enim...<br>
            <a href="javascript:">You can reach down and click this</a>
        </p>
    </div>


</div>
</body>
</html>

这里是 EZPZ 工具提示的 javascript。

// EZPZ Tooltip v1.0; Copyright (c) 2009 Mike Enriquez, http://theezpzway.com; Released under the MIT License
(function($){
    $.fn.ezpz_tooltip = function(options){
        var settings = $.extend({}, $.fn.ezpz_tooltip.defaults, options);

        return this.each(function(){
            var content = $("#" + getContentId(this.id));
            var targetMousedOver = $(this).mouseover(function(){
                settings.beforeShow(content, $(this));
            }).mousemove(function(e){
                contentInfo = getElementDimensionsAndPosition(content);
                targetInfo = getElementDimensionsAndPosition($(this));
                contentInfo = $.fn.ezpz_tooltip.positions[settings.contentPosition](contentInfo, e.pageX, e.pageY, settings.offset, targetInfo);
                contentInfo = keepInWindow(contentInfo);

                content.css('top', contentInfo['top']);
                content.css('left', contentInfo['left']);

                settings.showContent(content);
            });

            if (settings.stayOnContent && this.id != "") {
                $("#" + this.id + ", #" + getContentId(this.id)).mouseover(function(){
                    content.css('display', 'block');
                }).mouseout(function(){
                    content.css('display', 'none');
                    settings.afterHide();
                });
            }
            else {
                targetMousedOver.mouseout(function(){
                    settings.hideContent(content);
                    settings.afterHide();
                })
            }

        });

        function getContentId(targetId){
            if (settings.contentId == "") {
                var name = targetId.split('-')[0];
                var id = targetId.split('-')[2];
                return name + '-content-' + id;
            }
            else {
                return settings.contentId;
            }
        };

        function getElementDimensionsAndPosition(element){
            var height = element.outerHeight(true);
            var width = element.outerWidth(true);
            var top = $(element).offset().top;
            var left = $(element).offset().left;
            var info = new Array();

            // Set dimensions
            info['height'] = height;
            info['width'] = width;

            // Set position
            info['top'] = top;
            info['left'] = left;

            return info;
        };

        function keepInWindow(contentInfo){
            var windowWidth = $(window).width();
            var windowTop = $(window).scrollTop();
            var output = new Array();

            output = contentInfo;

            if (contentInfo['top'] < windowTop) { // Top edge is too high
                output['top'] = windowTop;
            }
            if ((contentInfo['left'] + contentInfo['width']) > windowWidth) { // Right edge is past the window
                output['left'] = windowWidth - contentInfo['width'];
            }
            if (contentInfo['left'] < 0) { // Left edge is too far left
                output['left'] = 0;
            }

            return output;
        };
    };

    $.fn.ezpz_tooltip.positionContent = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
        contentInfo['top'] = mouseY - offset - contentInfo['height'];
        contentInfo['left'] = mouseX + offset;

        return contentInfo;
    };

    $.fn.ezpz_tooltip.positions = {
        aboveRightFollow: function(contentInfo, mouseX, mouseY, offset, targetInfo) {
            contentInfo['top'] = mouseY - offset - contentInfo['height'];
            contentInfo['left'] = mouseX + offset;

            return contentInfo;
        }
    };

    $.fn.ezpz_tooltip.defaults = {
        contentPosition: 'aboveRightFollow',
        stayOnContent: false,
        offset: 10,
        contentId: "",
        beforeShow: function(content){},
        showContent: function(content){
            content.show();
        },
        hideContent: function(content){
            content.hide();
        },
        afterHide: function(){}
    };

})(jQuery);

// Plugin for different content positions. Keep what you need, remove what you don't need to reduce file size.

(function($){
    $.fn.ezpz_tooltip.positions.aboveFollow = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
        contentInfo['top'] = mouseY - offset - contentInfo['height'];
        contentInfo['left'] = mouseX - (contentInfo['width'] / 2);

        return contentInfo;
    };

    $.fn.ezpz_tooltip.positions.rightFollow = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
        contentInfo['top'] = mouseY - (contentInfo['height'] / 2);
        contentInfo['left'] = mouseX + offset;

        return contentInfo;
    };

    $.fn.ezpz_tooltip.positions.belowRightFollow = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
        contentInfo['top'] = mouseY + offset;
        contentInfo['left'] = mouseX + offset;

        return contentInfo;
    };

    $.fn.ezpz_tooltip.positions.belowFollow = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
        contentInfo['top'] = mouseY + offset;
        contentInfo['left'] = mouseX - (contentInfo['width'] / 2);

        return contentInfo;
    };

    $.fn.ezpz_tooltip.positions.aboveStatic = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
        contentInfo['top'] = targetInfo['top'] - offset - contentInfo['height'];
        contentInfo['left'] = (targetInfo['left'] + (targetInfo['width'] / 2)) - (contentInfo['width'] / 2);

        return contentInfo;
    };

    $.fn.ezpz_tooltip.positions.rightStatic = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
        contentInfo['top'] = (targetInfo['top'] + (targetInfo['height'] / 2)) - (contentInfo['height'] / 2);
        contentInfo['left'] = targetInfo['left'] + targetInfo['width'] + offset;

        return contentInfo;
    };

    $.fn.ezpz_tooltip.positions.belowStatic = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
        contentInfo['top'] = targetInfo['top'] + targetInfo['height'] + offset;
        contentInfo['left'] = (targetInfo['left'] + (targetInfo['width'] / 2)) - (contentInfo['width'] / 2);

        return contentInfo;
    };

})(jQuery);

当我悬停时,小灰色框,工具提示显示为这样,这是错误的。

但如果我删除 margin-left: auto;和边距右:自动;从包装类。有用。这是工具提示应该显示的实际外观。

我认为它在某个地方发生了冲突。但我不知道如何解决它。无论如何要使 div 居中而不使用 margin-left:auto;和边距右:自动; ?我尝试使用 margin:0px auto;和 text-align:center 也是。但是工具提示仍然出现同样的问题。

我需要网站处于中心位置,并且工具提示也需要在悬停内容下正常工作。请帮助我。谢谢。

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    keepInWindow() 看起来是你的敌人。

    http://jsfiddle.net/gyLpJ/


    编辑:让我们尝试一些更简单的东西,欢迎喝醉!

    好的。我一直对 jQuery 的醉醺醺的插件发誓。它是最好的工具提示插件,它的编码非常好。看这个:http://jsfiddle.net/gyLpJ/1/

    .. 或者,如果您希望将工具提示 HTML 作为文档的一部分,而 不在 javascript 中...试试这个巧妙的技巧:http://jsfiddle.net/knQvR/2/

    【讨论】:

    • 那我该如何解决?评论 keepInWindow() 不起作用 :( .. 有什么想法吗?
    • 嗯,对不起,我想你也改变了 html 中的一些东西?我刚刚注意到您删除了工具提示框的顶部和左侧位置?我完全替换了您身边的所有内容 html 和 javascript。但是当我用您的文件替换所有内容时,我的末尾什么都没有出现。我使用所有最新的浏览器 IE9、FF4、chrome 10 进行了测试。但是如果我从 html 添加工具提示的顶部和左侧位置,它可以工作,但这与我面临的问题相同。工具提示在框外,不在内容之下。有什么想法吗?
    • 对不起,我的错误,当我从您的 html 和 javascript 复制所有内容时,什么都没有出现,因为里面没有 javascript。现在我再次添加并测试,不幸的是,我面临的仍然是同样的问题。工具提示仍然与内容显示得太远。你的浏览器怎么样?能否请您截图并显示结果。谢谢
    • 嗨,Gary Green,感谢您提供的醉酒和 HTML 示例,但问题是,您看到工具提示中的链接了吗?它是这样写的,你可以向下点击它。这就是我选择 EZPZ 使用它的原因之一,因为当我悬停时,我可以进入工具提示框并单击链接。我需要它。但是使用您的示例和 html 工具提示,我无法进入框内,一旦移动它,工具提示框就会消失。无论如何得到它?
    • 啊,是的。在我看来,EZPZ 搞错了。工具提示的全部目的是提供快速有用的信息,非交互式。如果你想要一些可以用来点击和交互的东西,你应该看看“工具提示”之外的其他地方——试试 boxy:onehackoranother.com/projects/jquery/boxy
    猜你喜欢
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 2011-09-02
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多