【问题标题】:Need help removing or hiding divs depending on screen width需要帮助根据屏幕宽度删除或​​隐藏 div
【发布时间】:2019-08-10 02:11:47
【问题描述】:

所以我在将任何屏幕 > 大于 768px(平板电脑/台式机)上的 3x3 图片库布局更改为任何屏幕 的图像滑块时遇到了问题(移动)。

在大家,尤其是 Danilo(cmets 中的解决方案)的帮助之后,问题是 css 没有正确定位屏幕,我需要 - @media only screen em>,我也错过了一个 DIV...(我知道菜鸟错误)。

因此,如果有人试图在 Bootstrap 4 中将显示的内容从桌面布局更改为移动布局,您可以使用 CSS 媒体查询包装的 DIV 为目标来显示或不显示。

更新了下面的工作 sn-p:

@media only screen and (min-width: 768px) {
    .slider {
        display: none;
    }
    .block {
        display: block;
    }
}

@media only screen and (max-width: 768px) {
    .slider {
        display: block;
    }
    .block {
        display: none;
    }
}

.hold-tests {
    display: flex;
    width: 100%;
}

.col {
    flex: 1;
} 
	<!--- Start of Gallery Section -->	
	<div id="hold-tests">
		<div class="col slider">
			<div id="sliderIndicators" class="carousel slide carousel-fade z-depth-1-half" data-ride="carousel" data-interval="9000">
				<div class="carousel-inner col-" role="listbox">
					<!--- First Slide -->
					<div class="carousel-item active">
						<img src="Img/PHportrait.png" alt="background image">
					</div>
					<div class="carousel-item">
						<img src="Img/PHportrait2.png" alt="background image">
					</div>
					<div class="carousel-item">
						<img src="Img/PHportrait.png" alt="background image">
					</div>
				</div>
					<div class="narrow">
						<div class="col-12">
							<p class="lead text-center">Want to see more of my work?</p>
							<!-- add in social media icons and add target="_blank" to open in new tab -->
						</div>
					</div>
			</div>
		</div>	

		<div class="col block">
			<div id="gallery" class="offset">
				<div class="col-md">
					<div class="row no-padding">
							<div class="col-md-4">
								<div class="gallery-card">
									<a href="Img/PHportrait.png" target="_blank">
										<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
									</a>
								</div>
							</div>
							<div class="col-md-4">
								<div class="gallery-card">
									<a href="Img/PHportrait.png" target="_blank">
										<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
									</a>
								</div>
							</div>
							<div class="col-md-4">
								<div class="gallery-card">
									<a href="Img/PHportrait.png" target="_blank">
										<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
									</a>
								</div>
							</div>
						<div class="col-md-4">
							<div class="gallery-card">
								<a href="Img/PHportrait.png" target="_blank">
									<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
								</a>
							</div>
						</div>
						<div class="col-md-4">
							<div class="gallery-card">
								<a href="Img/PHportrait.png" target="_blank">
									<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
								</a>
							</div>
						</div>
						<div class="col-md-4">
							<div class="gallery-card">
								<a href="Img/PHportrait.png" target="_blank">
									<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
								</a>
							</div>
						</div>
					</div>
				</div>
				<div class="narrow">
					<div class="col-12">
						<p class="lead text-center">Want to see more of my work?</p>
							<!-- add in social media icons and add target="_blank" to open in new tab -->
					</div>
				</div>
			</div>
		</div>
	</div>
	<!--- End of Gallery Section -->

【问题讨论】:

    标签: javascript jquery html css bootstrap-4


    【解决方案1】:

    由于您使用的是引导程序,因此请使用它。不需要 jQuery,因为它已经被完全覆盖了。

    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <div class="d-none d-md-block">
      Only visible starting from md
    </div>
    <div class="d-md-none">
      Only visible on small screen
    </div>

    https://getbootstrap.com/docs/4.0/utilities/display/

    https://v4-alpha.getbootstrap.com/layout/responsive-utilities/


    另请注意,ID 通常不以 # 开头。尽管这可能是合法的,但您至少必须在 jquery 代码中使用 $('##option1') 选择它们。

    【讨论】:

    • 谢谢克里斯,但我刚刚尝试过,在移动宽度上它仍然显示 3x3 和幻灯片。但是,对于桌面,它现在也不显示。有什么我想念的吗?
    • 这取决于您添加类的位置。你能用你尝试的方法编辑你的问题,以便我们检查@Calum1300
    • 是的,我试图在 jquery 中定位它们,但在发布之前忘记删除 #。达尼洛的答案适用于移动宽度,但我仍然在桌面上显示,你知道解决方法吗?
    【解决方案2】:

    你可以做到这一点,只需使用 CSS

    html

    <div class="hold-tests">
        <div class="col test1">Test1</div>
        <div class="col test2">Test2</div>
        <div class="col test3">Test3</div>
    </div>
    

    css

    @media only screen and (min-width: 768px) {
        .test1 {
            display: none;
        }
        .test3 {
            display: block;
        }
    }
    @media only screen and (max-width: 768px) {
        .test3 {
            display: none;
        }
    }
    
    .hold-tests {
        display: flex;
        width: 100%;
    }
    
    .col {
        flex: 1;
    }
    

    【讨论】:

    • 我刚刚尝试了您提到的内容并编辑了要显示的问题。它适用于仅显示幻灯片的移动设备,谢谢!但在桌面上它仍然显示两者。
    • 我尝试复制 css 媒体查询并将目标更改为 test1 但这两个都删除了...
    • 我已经编辑了我的解决方案。你想要那样的东西吗?您可以使用@media query 随意使用可见性
    • 是的,这适用于移动设备,显示幻灯片并删除了 3x3,但现在桌面上都没有显示哈哈。对于我脑海中如此简单的事情,事实证明这要困难得多。感谢您迄今为止的所有帮助。
    • 你的 CSS 很好,但问题是 HTML,我已经修复了你的 sn-p
    猜你喜欢
    • 2014-09-18
    • 2015-10-26
    • 1970-01-01
    • 2016-03-29
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多