jquery - 在 100% 宽度的容器内动画 div

标签 jquery html css

我被这个问题困住了。我有 5 列 div 并排 float 。悬停在其上时,悬停的 div 应通过动画扩展其宽度。到目前为止我已经介绍了那部分,但我的问题是当我悬停那个 div 时,最后一个 div 出现在底部(意味着它超过 100% 宽度的容器)我不确定如何在不防止超过 100% 容器的情况下解决这个问题。

这是我到目前为止所做的:

jsfiddle

$(document).ready(function() {
  var boxWidth = $(".box").width();
  $(".box").mouseenter(function() {
    $(this).animate({
      width: "30%"
    });
  }).mouseleave(function() {
    $(this).animate({
      width: boxWidth
    });
  });
});
body {
  margin: 0;
  padding: 0;
}
.box {
  height: 100vh;
  width: 20%;
  float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box" style="background:red"></div>
<div class="box" style="background:grey"></div>
<div class="box" style="background:yellow"></div>
<div class="box" style="background:green"></div>
<div class="box" style="background:#000000"></div>

最佳答案

如果喜欢flex ,而不是这样做:

body {
    margin:0;
    padding:0;
}
.boxes {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    width: 100%;
}
.box {
    height: 100vh;
    flex: 1 1 auto;
    transition: flex-grow .3s ease-in-out;
}
.box:hover {
    flex-grow: 1.5;
}
<div class="boxes">
    <div class="box" style="background:red"></div>
    <div class="box" style="background:grey"></div>
    <div class="box" style="background:yellow"></div>
    <div class="box" style="background:green"></div>
    <div class="box" style="background:#000000"></div>
</div>

一个很好的 flex 指南 here .

JSFiddle 上的相同代码.

关于jquery - 在 100% 宽度的容器内动画 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39478175/

相关文章:

javascript - 输入字段中的 token 、标签、徽章

javascript - 带有包含图像特定文本的单独 div 的 jQuery 幻灯片

javascript - 播放 x 到 y 秒的音频

html - 固定定位侧边栏对齐

javascript - 根据媒体查询更改 window.scroll 位置?

使用变量的 Javascript Date 对象

html - 无论内容如何,​​如何确保三个DIV的高度相同

python - 使用 xpath 的复选框无法访问

html - 使用 CSS 从图像中删除 mime 类型图标,可能吗?

javascript - 我如何使用 jquery 在 <body> 中设置 html 内容