html - 宽度过渡 - div 重叠

标签 html css

我有 2 个 div,它们在一个容器内水平并排放置。我希望每个 div 在悬停时将宽度扩展到容器的整个宽度。 问题是,在指针不再悬停时,过渡后左侧 div(在 html 流中位于第一个)重叠在右侧 div 下。

这是一个例子。 要重新创建,只需将指针放在左侧 div 上,直到转换完成,然后将指针从 div 上移开。 期望的效果是宽度会逐渐减小(就像右边的div一样)。

* { margin: 0; padding: 0; }

#wrap { position: relative; width: 500px; margin: 0 auto; }

#one, #two { height: 100px; position: absolute; transition: width 1s ease-out; }

#one { width: 300px; background: #49d7b0; }
#two { right: 0; width: 200px; background: #d8c800; }

#one:hover, #two:hover { width: 500px; z-index: 1; }
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<link rel="stylesheet" type="text/css" href="z-index.css">
</head>
<body>
	<div id="wrap">
		<div id="one"></div>
		<div id="two"></div>
	</div>
</body>
</html>

最佳答案

animation 可以在这里解决问题。实际上 z-index 导致了这里的问题。您可以通过以下方式解决。

* { margin: 0; padding: 0; }

#wrap { position: relative; width: 500px; margin: 0 auto; }

#one, #two { height: 100px; position: absolute; transition: width 1s ease-out; }

#one { width: 300px; background: #49d7b0; animation: movedec 1s; }
#two { right: 0; width: 200px; background: #d8c800; }

#one:hover { animation: moveinc 1s forwards;  -webkit-animation: moveinc 1s forwards; }
#two:hover { width: 500px;  }

@keyframes moveinc {
    from {width: 300px; z-index: 1;}
    to {width: 500px; z-index: 1;}
}

@keyframes movedec {
    from {width: 500px; z-index: 1;}
    to {width: 300px; z-index: 1;}
}

@-webkit-keyframes moveinc {
    from {width: 300px; z-index: 1;}
    to {width: 500px; z-index: 1;}
}

@-webkit-keyframes movedec {
    from {width: 500px; z-index: 1;}
    to {width: 300px; z-index: 1;}
}
<div id="wrap">
		<div id="one"></div>
		<div id="two"></div>
	</div>

关于html - 宽度过渡 - div 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35722256/

相关文章:

javascript - 使用 OpenLayers 显示 POI,无需文本文件

html - 如何停止 Internet Explorer 6/7 显示悬停子菜单

css - CSS 内容属性可以在 IE7 中使用吗?

html - 使用子菜单时内容消失(菜单和子菜单争夺目标)

javascript根据屏幕上的div可见性删除css

html - IE8 中的表格 tbody 滚动

javascript - 简单的背景颜色

jquery - Unslider - 响应高度

html - 这个 CSS 有什么问题?添加更多内容时不会达到 100%?

CSS 屏幕分辨率