无法计算隐藏div的宽度

Jon*_*Jon 8 css jquery

由VEGA BELOW解决.这是工作的好消息.我将保留整个帖子以供将来的访问者查看此问题是如何解决的.


我正在尝试创建一个按钮,当它悬停时,会在下面的抽屉上显示一些信息.

我想让它很容易实现到一个页面,所以我把它变成了一个jQuery小部件.我想根据按钮和/或抽屉上的内容进行调整,但现在没有这样做.用户应该能够将单个HTML div标记与隐藏数据放在一起,它才能正常工作.

理想地,按钮/抽屉宽度将调整到两者之间的最大值,以使抽屉在处于闭合状态时完全隐藏.我只是不希望我的用户不得不设置固定宽度.

我需要帮助!

这是它的样子:

在此输入图像描述

在此输入图像描述

相关HTML(小提琴中的完整HTML ):

<div class="download" data-value="It's awesome!">Visit Website</div>
Run Code Online (Sandbox Code Playgroud)

相关CSS(小提琴中的完整CSS ):

.button {
    position:absolute;
    padding:10px;
}

.drawer {
    box-sizing:border-box;
    z-index:-1;
    position:absolute;
    top:3px;
    left:3px;
    padding:6px 10px;
    text-align:center;
    -webkit-transition:0.3s left ease-in;

}
Run Code Online (Sandbox Code Playgroud)

相关的jQuery(小提琴中的完整jQuery ):

$(function() {
    $(".button").each(function() {
        $(this).append("<div class='drawer'>" +  $(this).data('value') + "<div class='handle'>||</div></div>");
        $(".drawer", this).css("width", $(".button").width());
    });

    $(".button").mouseenter(function() {
        $(".drawer", this).css("left", $(".button").outerWidth());
    }).mouseleave(function() {
        $(".drawer", this).css("left", "3px");
    });
});
Run Code Online (Sandbox Code Playgroud)

Sel*_*gam 2

您必须根据按钮计算宽度this。见下文,

$(function() {
    $(".button").each(function() {
        $(this).append("<div class='drawer'>" +  $(this).data('value') + "<div class='handle'>||</div></div>");
        $(".drawer", this).width($(this).width()); //updated with this object
    });

    $(".button").mouseenter(function() {
        $(".drawer", this).css("left", $(this).outerWidth()); //updated with this object
    }).mouseleave(function() {
        $(".drawer", this).css("left", "3px");
    });
});
Run Code Online (Sandbox Code Playgroud)

演示: http: //jsfiddle.net/dELNa/11/