简体   繁体   English

屏幕太小时,bootstrap dropdown-menu-right

[英]bootstrap dropdown-menu-right when screen too small

I have a dropdown menu and using 我有一个下拉菜单并使用

class = dropdown-menu 类别=下拉菜单

the dropdown menu uses extra space on the right site. 下拉菜单会在正确的网站上使用额外的空间。 If I make the screen smaller (or use a mobile device) there might not be any space on the right site. 如果我缩小屏幕(或使用移动设备),则正确的网站上可能没有任何空间。 In this case I can use 在这种情况下,我可以使用

class = dropdown-menu-right class =下拉菜单右

which uses the space on the left site instead. 而是使用左侧网站上的空间。 My question is, is there a way to detect the size of the screen and based on that render a html template with one of the two options above? 我的问题是,有没有一种方法可以检测屏幕的大小,并根据上述两个选项之一呈现HTML模板? thanks carl 谢谢卡尔

Solution using media Query only css 使用媒体的解决方案仅查询CSS

Note: Run code in full screen and try to reduce screen 注意:全屏运行代码并尝试缩小屏幕

 @media (min-width: 768px) { .one { width: 30px; height: 30px; background-color: red } } @media (max-width: 768px) { .two { width: 30px; height: 30px; background-color: yellow } } 
 <div class="one two "></div> 

see this answer : https://stackoverflow.com/a/33209805/4696809 in this answer run snippet in full screen you can see menu if you reduce window size menu will gone and button will appear even i am changing center part also depending upon window size 看到此答案: https : //stackoverflow.com/a/33209805/4696809在此答案中全屏运行代码段您可以看到菜单,如果您减小窗口大小,菜单将消失并且按钮将出现,即使我更改中心部分也取决于窗口大小

Using Jquery 使用jQuery

Note: Run code in full screen and try to reduce screen and see console and div.if you reduce screen width div color will change. 注意:以全屏模式运行代码,然后尝试缩小屏幕并查看控制台和div。如果减小屏幕宽度,div颜色将发生变化。

 $(document).ready(function() { $(window).resize(function() { var width = $(window).width() console.log($(window).width()) if (width > 786) { console.log("BIg") $('.first').removeClass('new') } else if (width < 786) { console.log("small") $('.first').addClass('new') } }); }); 
 .first { width: 30px; height: 30px; background-color: red; } .new{ background-color: yellow; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="first"></div> 

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

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