简体   繁体   English

如何让菜单从底部按钮向上滑动而不是仅仅出现? HTML/CSS/JS

[英]How do I get the menu to slide up from bottom button instead of just appearing? HTML/CSS/JS

I have tried many different ways to do it but the menu appears the exact same way each time.我尝试了许多不同的方法来做到这一点,但菜单每次都以完全相同的方式出现。 Things I´ve tried: height 0->20%, width 0->100%, jQuery, JavaScript, slideUp/Down, hide/Show, margin-bottom: -50%->0%, animate etc.我试过的东西:高度 0->20%,宽度 0->100%,jQuery,JavaScript,上滑/下滑,隐藏/显示,边距底部:-50%->0%,动画等。

 $(document).ready(function(){ $("#settings-menu-btn").click(function(){ $("#nav").slideDown(500); }); //Hiding the menu $("#close-menu-btn").click(function(){ $("#nav").slideUp("slow"); }); });
 .nav{ display: none; }.menu { padding: 0 10% 10%; background-image: linear-gradient(to left, $accent-color, $dark-primary-color); background-image: -webkit-linear-gradient(to left, $accent-color, $dark-primary-color); border-radius: 5px 5px 0 0; list-style-type: none; width: 100%; max-width: $max-width-menu; max-height: $max-height-menu; position: fixed; bottom: 0; z-index: 10; left: 50%; transform: translateX(-50%); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="nav" id="nav"> <ul class="menu" id="menu"> <li>menu item</li> </ul> </div> <div class="settings-menu-link"> <img id="settings-menu-btn" src="settings-icon.png"> </div>

removed position and translate styling for menu and its working.删除 position 并翻译菜单样式及其工作。

 $(document).ready(function(){ $("#settings-menu-btn").click(function(){ $("#nav").slideDown(500); }); //Hiding the menu $("#close-menu-btn").click(function(){ $("#nav").slideUp("slow"); }); });
 .nav{ display: none; }.menu { padding: 0 10% 10%; background-image: linear-gradient(to left, $accent-color, $dark-primary-color); background-image: -webkit-linear-gradient(to left, $accent-color, $dark-primary-color); border-radius: 5px 5px 0 0; list-style-type: none; width: 100%; max-width: $max-width-menu; max-height: $max-height-menu; /* position: fixed;*/ bottom: 0; z-index: 10; left: 50%; background: #ff8a009e; /* transform: translateX(-50%);*/ }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="settings-menu-link"> <img id="settings-menu-btn" src="https://img.icons8.com/ios/452/settings.png" style="width:20px;"> </div> <div class="nav" id="nav"> <ul class="menu" id="menu"> <li>menu item</li> </ul> <button id="close-menu-btn">close</button> </div>

I solved your issue, but let me just help you like this first.我解决了你的问题,但让我先帮助你。

You should almost never put your script tag before your HTML code, but always at the very bottom, before the body tag closes.你几乎不应该把你的script标签放在你的 HTML 代码之前,但总是在最底部,在body标签关闭之前。

There are very few situations where you will have to load your script before the page has been fully loaded first.在极少数情况下,您必须在页面首先完全加载之前加载脚本。 So try to avoid that.所以尽量避免这种情况。

Also, there is no reason to use a translate property on an element that has been positioned absolute/fixed.此外,没有理由在绝对/固定定位的元素上使用translate属性。 You can already move it with top, right, bottom, left .您已经可以使用top, right, bottom, left来移动它。

Simply just give your navigation a negative value for top property, and then make a new class that will have top property equal to 0.只需为您的导航top属性设置一个负值,然后创建一个新的 class,其top属性等于 0。

Then, on the click of your button, just toggle that class to navigation each time the button is clicked: :)然后,单击您的按钮,只需在每次单击按钮时将 class 切换到导航::)

Here is my solution to this, it will fix your issue:这是我的解决方案,它将解决您的问题:

https://codepen.io/Juka99/pen/WNGjNPG https://codepen.io/Juka99/pen/WNGjNPG

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

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