jquery - 使用 jQuery animate 增加 DIV 的高度,而不会将内容向下推

标签 jquery html css

我有这个代码,

HTML

<div id="header-line"></div>
<div id="main-menu"></div>
<div id="content"></div>

CSS

#header-line {
    height: 20px;
    background-color: black;
    top:0;
    left:0;
}
#main-menu {
    height: 30px;
    background-color: green;
}
#content {
    background-color: blue;
    height: 200px;
}

Javascript

$("#main-menu").hover(function () {
    $(this).stop(true, false).animate({
        height: "100px"
    });
}, function () {
    $(this).stop(true, false).animate({
        height: "30px"
    });
});

Demo jsFiddle

我用 jQuery animate 函数使中间的 DIV 扩展了它的高度。但问题在于它会压低底部的 DIV。

如何让它在不向下推的情况下扩展到底部 DIV?

最佳答案

将 html 更改为

 <div id="header-line"></div>
 <div id="content">
     <div id="main-menu">Expands on hover</div>
 </div>

将 jquery 更改为

$("#main-menu").hover(function () {
    $(this).stop(true, false).animate({
        height: "100px"
    });
}, function () {
    $(this).stop(true, false).animate({
        height: "30px"
    });
});

将样式更改为

#header-line {
    height: 20px;
    background-color: black;
    top:0;
    left:0;
}
#main-menu {
    height: 30px;
    background-color: green;
    position:relative;
    z-index:3;
}
#content {
    background-color: blue;
    height: 200px;
}

主要目的是将div放在另一个div里面,然后它不会把底部的div放下。

Working fiddle.

关于jquery - 使用 jQuery animate 增加 DIV 的高度,而不会将内容向下推,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17229924/

相关文章:

javascript - Fabricjs 检测鼠标悬停在对象路径上

html - 修复滚动 div 内的绝对 div

javascript - 与 jquery 和 form 冲突

css - 使用 CSS : text only flows about original position? float 图像

html - IE 中没有 CSS 边框

javascript - jquery 在 google chrome 浏览器上缓慢渲染微调器

jquery - 当 cycle lite 开始运行时定位困惑

java - 如何使用 jquery 自动完成?

html - 创建左侧标题和右侧按钮的导航栏

javascript - 根据焦点获取两个提交按钮中任意一个的值