简体   繁体   English

Bootstrap折叠隐藏在调整大小后

[英]Bootstrap Collapse hide on resize

I have the following issue, 我有以下问题,

In certain part of my website I'm using a Bootstrap collapse to show an element that is supposed to be only seen in a mobile device, or resolution under 768px. 在我网站的某些部分中,我正在使用Bootstrap折叠来显示应该仅在移动设备或768px以下分辨率下才能看到的元素。

The issue I have is that if I toggle the element when under 768px but I don't close it, if I resize the browser the element remains visible. 我遇到的问题是,如果我在768px以下时切换元素,但没有关闭它,如果我调整浏览器的大小,则该元素仍然可见。

I'm using $('#collapse').collapse('toggle'); 我正在使用$('#collapse').collapse('toggle'); to show the element. 显示元素。

So when I resize I can use .collapse('hide') to hide the element if it was toggled 因此,当我调整大小时,可以使用.collapse('hide')来隐藏元素(如果已切换)

But if I'm on a resolution below 768px without activating the Collapse and I resize back to a bigger resolution, the element gets toggled for sure because of the .collapse('hide') . 但是,如果我的分辨率低于768像素,而没有激活Collapse,并且我将其调整为更大的分辨率,则由于.collapse('hide') ,该元素肯定会切换。

So, how can I know if the element was toggled or active and in order to revert that when the browser gets resized? 那么,如何确定元素是已切换还是处于活动状态,以及如何在调整浏览器大小时还原该元素?

You can use the jQuery Visible selector and if the element is visible set it to hidden. 您可以使用jQuery Visible选择器 ,如果该元素可见,则将其设置为隐藏。

if ($('#collapse').is(':visible')) {
    $('#collapse').collapse('hide');
}

Bootstrap Collapse 引导折叠

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM