jQuery:在slideDown()和slideUp()时附加固定高度的div

时间:2013-06-24 07:54:38

标签: jquery

在此页面上,将鼠标悬停在文本上会附加一个带有幻灯片效果的文本的div,但问题是效果后它的高度不稳定。如何固定高度?

的index.html

<!DOCTYPE html>
<html>
    <head>

        <script src = "http://code.jquery.com/jquery-1.10.1.min.js" type = "text/javascript"></script>
        <script src = "script.js" type = "text/javascript"></script>
    </head>

    <body>
        <div>
            <h1 style = "width: 299px" id = "text1">Hover to see the truth</h1>
        </div>
    </body>
</html>

的script.js

$(document).ready (function() {
    $(document).on ("mouseenter", "#text1", function() {

        $("body").append ("<div style = 'background-color: red; width: 299px' id = 'descr'></div>");
        $("#descr")
            .hide()
            .append ("<h3>You're an idiot</h3>")
            .slideDown ("slow");
    });

    $(document).on ("mouseleave", "#text1", function() {
        $("#descr").slideUp ("slow", function() {
            $(this).remove();
        });
    });
});

1 个答案:

答案 0 :(得分:1)

为html添加一个固定高度的样式标记。例如:将.append方法更改为:

append("<h3 style="height:100px">You're an idiot</h3>")