简体   繁体   English

将计算的 jQuery 值/变量提供给 CSS class

[英]Give calculated jQuery value / variable to CSS class

I got the following navbar.我得到了以下导航栏。 What I want it to do is:我想要它做的是:

  1. Come into view when you scroll down向下滚动时进入视野
  2. Leave the view when scrolling up again (to a defined amount)再次向上滚动时离开视图(达到定义的量)
  3. "Close" the navbar with the arrow to make it smaller用箭头“关闭”导航栏以使其更小
  4. "Reopen" the navbar after it's "closed"导航栏“关闭”后“重新打开”
  5. At any time I would still like the navbar to hide when scrolling to the top.在任何时候,我仍然希望导航栏在滚动到顶部时隐藏。 Doesn't matter if it's extended or compressed不管是扩展还是压缩

That's not working, because my code to toggle the navbar will set inline values for top which is needed in class to hide it when scrolling up这不起作用,因为我切换导航栏的代码将为top设置内联值,这是 class 在向上滚动时隐藏它所必需的

So the real question besides what's wrong with my code is:所以除了我的代码有什么问题之外,真正的问题是:

Is there any way to give my calculated height into CSS?有什么方法可以将我计算出的高度输入 CSS? Or don't set the styles inline but rather define a new class or something and give that class the values?或者不要设置 styles 内联,而是定义一个新的 class 或其他东西,然后给 class 值?

So I can still have some order in CSS and the hide on scroll up won't get overridden by the inline styles.所以我仍然可以在 CSS 中有一些订单,并且向上滚动时的隐藏不会被内联 styles 覆盖。

 // Sticky Header / Appear when scrolled to XY $(window).scroll(function() { var header_sticky = $('.header-sticky'); if ($(this).scrollTop() > 250) { header_sticky.addClass('fixed'); } else { header_sticky.removeClass('fixed'); } }); // Hide / Show Navbar $(function() { $('#nav_toggle').on('click', function() { toggleNav(); }); }); function toggleNav() { let navHeight = $('.header-sticky.toggleable').height(); let anchorHeight = $('#nav_toggle').height(); let toggleHeight = navHeight - anchorHeight; if ($('#nav_toggle').parent().hasClass('nav_toggled')) { $('#nav_toggle').parent().css('top', 0); $('#nav_toggle').parent().removeClass('nav_toggled') $('#nav_toggle').html('<i class="fas fa-angle-down"></i>') } else { $('#nav_toggle').parent().addClass('nav_toggled') $('#nav_toggle').parent().css('top', -toggleHeight); $('#nav_toggle').html('<i class="fas fa-angle-up"></i>') } }
 /* Nav Styling */.nav { width: 100vw; padding: 0 10vw; position: fixed; background: #fefefe; z-index: 8; left: 0; right: 0; box-shadow: 0px 3px 10px 1px rgba(0, 0, 0, 0.2); }.nav ul { list-style-type: none; display: flex; justify-content: center; flex-wrap: wrap; align-items: center; width: 100%; }.nav ul li { padding: 0.5rem 1rem; } /* Toggle Styling */ #nav_toggle { font-size: 1.5rem; text-align: center; width: 2rem; height: 100%; display: block; margin: 0 auto; } /* Sticky Header CSS */.header-sticky { position: fixed; z-index: 1000; top: -250px; transition: all ease-in-out.25s; }.header-sticky.fixed { top: 0; } /* Some height so it's scrollable */.for_some_height { height: 200vw; width: 100%; z-index: -1; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" /> <div class="nav header-sticky toggleable"> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> <a href="javascript:void(0)" id="nav_toggle"><i class="fas fa-angle-down"></i></a> </div> <div class="for_some_height"></div>

Its done, All you need to do is, Hide and show that menu with display and not with position, in this way, you not need to worry about the position and the state of the menu items它完成了,你需要做的就是,隐藏和显示带有显示而不是 position 的菜单,这样你就不用担心菜单项的 position 和 state

 // Sticky Header / Appear when scrolled to XY $(window).scroll(function() { var header_sticky = $('.header-sticky'); if ($(this).scrollTop() > 250) { header_sticky.addClass('fixed'); } else { header_sticky.removeClass('fixed'); } }); // Hide / Show Navbar $(function() { $('#nav_toggle').on('click', function() { $(this).prev('ul').slideToggle(); $(this).toggleClass('closed'); if($('#nav_toggle').hasClass('closed')){ $('#nav_toggle').html('<i class="fas fa-angle-up"></i>'); }else { $('#nav_toggle').html('<i class="fas fa-angle-down"></i>'); } }); });
 /* Nav Styling */.nav { width: 100vw; padding: 0 10vw; position: fixed; background: #fefefe; z-index: 8; left: 0; right: 0; box-shadow: 0px 3px 10px 1px rgba(0, 0, 0, 0.2); }.nav ul { list-style-type: none; display: flex; justify-content: center; flex-wrap: wrap; align-items: center; width: 100%; }.nav ul li { padding: 0.5rem 1rem; } /* Toggle Styling */ #nav_toggle { font-size: 1.5rem; text-align: center; width: 2rem; height: 100%; display: block; margin: 0 auto; } /* Sticky Header CSS */.header-sticky { position: fixed; z-index: 1000; top: -250px; transition: all ease-in-out.25s; }.header-sticky.fixed { top: 0; } /* Some height so it's scrollable */.for_some_height { height: 200vw; width: 100%; z-index: -1; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" /> <div class="nav header-sticky toggleable"> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> <a href="javascript:void(0)" id="nav_toggle"><i class="fas fa-angle-down"></i></a> </div> <div class="for_some_height"></div>

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

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