简体   繁体   English

HTML中的不同类,具体取决于屏幕大小

[英]Different class in HTML depending of screen size

I'm newbie in bootstrap and I don't know how to do the following: I'm working with Django and in my HTML template I have this: 我是Bootstrap的新手,我不知道如何执行以下操作:我正在使用Django,并且在我的HTML模板中,我有以下内容:

<div class="tabs tabs-vertical tabs-left">
                            <ul class="nav nav-tabs">

This works fine with large screens, but it looks ugly when I access into the page with my phone, so I need that my template look like this if the screen is xs: 这在大屏幕上可以正常工作,但是当我用手机访问页面时看起来很难看,因此如果屏幕是xs,我需要我的模板看起来像这样:

<div class="tabs">
                            <ul class="nav nav-tabs nav-justified flex-column flex-md-row">

Can this be done with bootstrap? 可以用引导程序完成吗? It's necessary to clone the entire div content and hide depending of screen? 是否有必要克隆整个div内容并根据屏幕隐藏? Thank you. 谢谢。

You could try using mobiledetect.js script, and depending on what device it registers, send it to a different CSS script that would fit it for each type of device (ie mobile, pc, etc.) 您可以尝试使用mobiledetect.js脚本,并根据其注册的设备,将其发送到适合每种类型的设备(例如,移动设备,个人计算机等)的其他CSS脚本。

Script download: http://hgoebl.github.io/mobile-detect.js/ 脚本下载: http : //hgoebl.github.io/mobile-detect.js/

Just use javascript to add the classes if the screen width is less that your desired breakpoint. 如果屏幕宽度小于所需的断点,请使用javascript添加类。 Since you're using Bootstrap, you can use jQuery to make it easier. 由于您使用的是Bootstrap,因此可以使用jQuery使其变得更容易。 Example: 例:

 if (window.innerWidth <= 500) { $('#nav').removeClass('tabs-vertical tabs-left'); $('#navUl').addClass('nav-justified flex-column flex-md-row'); } console.log(document.getElementById('nav').classList); console.log(document.getElementById('navUl').classList); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="tabs tabs-vertical tabs-left" id='nav'> <ul class="nav nav-tabs" id='navUl'> </ul> </div> 

You can use that if statement to do what you want. 您可以使用该if语句执行所需的操作。 If you attach it to $(window).resize(); 如果将其附加到$(window).resize(); event you can update it to add the styles and remove them responsively. 事件,您可以对其进行更新以添加样式并相应地将其删除。

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

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