寻找的结果:
如何看待我试图摆脱的问题:
这种行为对我来说没有意义,因为媒体查询指定当窗口<465px时,TOC的宽度为0。所以为什么将其重置为150px对我来说还是一个谜。但是对我来说最重要的是,当移除了作为覆盖层的TOC时(当“覆盖层”类被切换(关闭)时,如何摆脱这种不需要的过渡?
function showMenuAsOverlay(caller) {
var node = document.getElementById("toc");
node.classList.toggle('overlay');
if (node.classList.contains('overlay'))
caller.innerHTML = "Hide Table of Content";
else
caller.innerHTML = "Show Table of Content";
}
.wrapper {
display: flex;
flex-direction: row;
border: 3px solid black;
z-index: -1;
position: relative;
}
.container-left {}
#toc {
border: 1px solid green;
flex: 0 0 auto;
white-space: pre;
z-index: -1;
width: 150px;
background-color: red;
transition: width 1s ease-out;
box-sizing: border-box;
}
.container-right {
display: flex;
border: 1px solid red;
flex 1 1 auto;
max-width: 400px;
background-color: white;
z-index:-1;
box-sizing: border-box;
}
.myicon {
cursor: pointer;
visibility: hidden;
}
@media
screen and (max-width: 465px) {
#toc {
width: 0;
background-color: purple;
transition: width 1s ease-out;
}
#toc.overlay {
z-index: 999;
position: absolute;
left: 0px;
background-color: cyan;
bottom: 0;
top: 0;
width: 150px;
transition: left 1s ease-out;
}
.myicon {
visibility: visible;
}
}
<body>
<div onclick="showMenuAsOverlay(this)" class="myicon" id="myicon">Show Table of Content</div>
<div class="wrapper">
<div class="container-left" id="toc" data-state="0">This is some text in the TOC</div>
<div class="container-right">
This is some content this is some content this is some more content, and this is content again and again.
</div>
</div>
</body>
答案 0 :(得分:2)
当<div id="toc">
失去班级overlay
时,就会发生过渡。
这意味着您不再应用此规则:
#toc.overlay {
z-index: 999;
position: absolute;
left: 0px;
background-color: cyan;
bottom: 0;
top: 0;
width: 150px;
transition: left 1s ease-out;
}
要应用此规则:
#toc {
width: 0;
background-color: purple;
transition: width 1s ease-out;
}
这很清楚为什么会发生过渡。您将从width: 150px
到width: 0
,并且此过渡从#toc
开始应用:width 1s ease-out;
此外,您无需媒体查询即可应用此功能:
#toc {
border: 1px solid green;
flex: 0 0 auto;
white-space: pre;
z-index: -1;
width: 150px;
background-color: red;
transition: width 1s ease-out;
box-sizing: border-box;
}
这意味着转换会应用任何屏幕尺寸。我认为那不是你想要的。在该块周围放置一个媒体查询,以便仅在您确实需要时才应用。