[英]How do I use a different navbar class for devices of different sizes?
This is the current code I'm using, when the screen is scaled down for viewing in mobile devices I want to make the navbar class into a navbar-inverse class 这是我正在使用的当前代码,当缩小屏幕以便在移动设备中查看时,我想将navbar类变为navbar-inverse类
<button class="navbar-toggler" data-toggle="collapse" data-target="#collapse_target">
<span class="navbar-toggler-icon"></span>
<span class="navbar-text">Hello</span>
</button>
<div class="collapse navbar-collapse" id="collapse_target">
<ul class="nav navbar-nav ml-auto" >
<li class="nav-item active">
<a class="nav-link" href="#">Home </a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Bio</span></a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Tour </a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Videos </a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Media </a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
I've tried using media queries but I can't select a different class in each query 我尝试过使用媒体查询但我不能在每个查询中选择不同的类
There are a few ways you can achieve it, here are two of them. 有几种方法可以实现它,这里有两种方法。
<link rel="stylesheet" media="screen and (min-device-width: 800px)" href="800.css" />
). <link rel="stylesheet" media="screen and (min-device-width: 800px)" href="800.css" />
)。 You can define the inverse colors in the new StyleSheet Hope this helps. 希望这可以帮助。
You can style manage using by a css media query, that will be the fastest and standard way 您可以使用css媒体查询进行样式管理,这将是最快速和标准的方式
another solution you can change the class name by javascript or jQuery library 另一种解决方案,您可以通过javascript或jQuery库更改类名
Hope this script will help you, to remove and add class 希望这个脚本可以帮助你,删除和添加类
var targetElement = document.getElementById("demo");
window.addEventListener("resize", myFunction);
window.addEventListener("load", myFunction);
function myFunction() {
var wWidth = window.innerWidth ;
if( wWidth < 500 ) {
targetElement.classList.add("below500");
targetElement.classList.remove("above500");
}else{
targetElement.classList.add("above500");
targetElement.classList.remove("below500");
}
}
I have created a quick demo for you. 我已经为您创建了一个快速演示。 Please check it.
请检查一下。
https://codepen.io/rhythm19/pen/GaOVPY https://codepen.io/rhythm19/pen/GaOVPY
navbar-light bg-light
and navbar-dark bg-dark
are the main classes to be toggled depending on window size. navbar-light bg-light
和navbar-dark bg-dark
是根据窗口大小切换的主要类。 I have used jquery to toggle classes. 我用jquery来切换类。 You can even use any custom class and override the background and text colors with media query.
您甚至可以使用任何自定义类,并使用媒体查询覆盖背景和文本颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.