简体   繁体   English

如何为不同大小的设备使用不同的导航栏类?

[英]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. 有几种方法可以实现它,这里有两种方法。

  1. Load CSS StyleSheets based on screen size (ex: <link rel="stylesheet" media="screen and (min-device-width: 800px)" href="800.css" /> ). 根据屏幕大小加载CSS StyleSheets(例如: <link rel="stylesheet" media="screen and (min-device-width: 800px)" href="800.css" /> )。 You can define the inverse colors in the new StyleSheet 您可以在新StyleSheet中定义反色
  2. Use @media() queries (Learn how to use them here: https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries ) 使用@media()查询(在此处了解如何使用它们: https//developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries

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-lightnavbar-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.

相关问题 如何在不同的屏幕尺寸上将导航栏居中 - How can I center my navbar on different screen sizes 如何使用CSS / HTML在具有不同屏幕分辨率和尺寸的所有设备上显示高度为2mm的文本? - How use CSS/HTML to display text with height, for example, 2mm on all devices with different screen resolutions and sizes? 我如何获得此代码以缩放到不同的窗口大小? - How do i get this code to scale to different window sizes? 如何使我的块引用响应不同的屏幕尺寸? - How do i make my blockquote responsive to different screen sizes? 我如何居中对齐不同屏幕尺寸的文本 - How do i center align text in different screen sizes 如何调整 div 元素以使网站在不同屏幕尺寸的设备上看起来相同? - How can I adjust div elements so that the website looks the same on devices of different screen sizes? 如何折叠移动设备的导航栏? - How do I collapse a navbar for mobile devices? Bootstrap Navbar中具有不同大小的两行 - Two rows with different sizes in Bootstrap Navbar 移动尺寸和桌面上的不同导航栏样式 - Different navbar styles in mobile sizes & desktop 如何根据不同的设备使用鼠标悬停和单击? - How can I use hover and click depending on different devices?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM