简体   繁体   English

如何检测菜单数量可变的标题的断点?

[英]How could i detect breakpoints of a header with variable number of menus?

I'm creating a header for a web application and i want this header to be configurable : admin can add or remove menus as he want. 我正在为Web应用程序创建头,并且我希望该头是可配置的:管理员可以根据需要添加或删除菜单。 For example : 例如 : 在此处输入图片说明 Or 要么 在此处输入图片说明

The breakpoint of these two navbar isn't the same. 这两个导航栏的断点不同。 So I'm wondering how could i deal with that ? 所以我想知道我该如何处理? How can I detect that the menus are too long for the screen so i have to reduce it ? 如何检测菜单对于屏幕而言太长,因此必须缩小菜单?

Media queries are fixed so it's not a good solution and I don't find how to do that in JavaScript. 媒体查询是固定的,因此它不是一个很好的解决方案,而且我也找不到如何在JavaScript中执行此操作。

Here is a basic approach. 这是一种基本方法。 It uses jQuery to detect the distance between the logo and the menu items, and if it goes below an arbitrary value (100px here), some css is added. 它使用jQuery来检测徽标和菜单项之间的距离,如果它低于任意值(此处为100px),则会添加一些CSS。 You could expand this to do whatever you'd like when the distance gets too small. 当距离太小时,您可以扩展此功能以执行任何操作。 Lower the padding, logo size, swap in a mobile menu, etc. 降低填充,徽标大小,移动菜单中的交换等。

jsfiddle here jsfiddle在这里

 $(window).on('load resize', function() { var menuOffset = $('.menu-items').offset().left var logoWidth = $('.logo').width(); if (menuOffset - logoWidth <= 100) { $('.menu-items').css('background-color', 'red'); } else if (menuOffset - logoWidth > 100) { $('.menu-items').css('background-color', 'blue'); } }); 
 body { font-family: helvetica; font-weight: bold; } .nav { display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .nav .menu-items { display: flex; } .nav .menu-items .item { padding: 0 10px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <nav class="nav"> <div class="logo"> logo </div> <div class="menu-items"> <div class="item"> item </div> <div class="item"> item </div> <div class="item"> item </div> <div class="item"> item </div> <div class="item"> item </div> <div class="item"> item </div> <div class="item"> item </div> </div> </nav> 

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

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