html - 增加浏览器大小后如何显示div?

标签 html css

我正在显示一个 div。当我单击关闭/打开时,div 会隐藏或显示。

我想要的是当 div 被隐藏并且当我将浏览器大小增加到 > 767px 时 div 应该自动显示回来。

要测试它,首先减小浏览器的大小然后单击关闭以便 div 隐藏,然后再次增大浏览器大小然后 div 应该显示回来但它没有

如何实现?

/* Set the width of the side navigation to 250px */
function openNav() {
    document.getElementById("demo").style.display = "block";
}

/* Set the width of the side navigation to 0 */
function closeNav() {
    document.getElementById("demo").style.display = "none";
}
#demo{
   display:block;
}


@media screen and (max-width: 767px) {
        #demo {
            display: none;
        }
}
<div id="demo">Hello</div>

<span onclick="openNav()">Open</span>
<span onclick="closeNav()">Close</span>

最佳答案

您必须使用 min-width 而不是 max-width 来实现此目的。我还使用了 !important,尽管我很鄙视它,但它是覆盖 JavaScript 设置的样式所必需的。

@media screen and (min-width: 767px) {
    #demo {
        display: block!important;
    }
}

片段:

/* Set the width of the side navigation to 250px */
function openNav() {
  document.getElementById("demo").style.display = "block";
}

/* Set the width of the side navigation to 0 */
function closeNav() {
  document.getElementById("demo").style.display = "none";
}
#demo {
  display: block;
}

@media screen and (min-width: 767px) {
  #demo {
    display: block!important;
  }
}
<div id="demo">Hello</div>
<span onclick="openNav()">Open</span>
<span onclick="closeNav()">Close</span>

关于html - 增加浏览器大小后如何显示div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48441865/

相关文章:

javascript - 如何在页面加载时仅显示特定幻灯片

html - 头部的 CSS 框阴影具有粘性位置

css - 删除 Bootstrap 下的空间

html - 防止在 100% 宽度的页面上水平拖动滚动

javascript - 打印带有图像的 html(每个图像在单独的页面上)

html - Joomla 规范到 404

html - struts2 元素的布局页面出现不需要的边框

jquery - REM 作为大众响应式 CSS 站点

html - CSS 两个 span 或 div 在同一行中都在一个 div 中

javascript - 如何旋转图像.onclick