简体   繁体   English

使用JavaScript,HTML和CSS的下拉菜单

[英]Dropdown Menu using javascript, HTML and CSS

I can't seem to get this to work. 我似乎无法使它正常工作。 I don't wanna put jQuery in yet. 我还不想放入jQuery。 Just doing plain javascript. 只是做普通的JavaScript。 When I click on the image nothing happens. 当我单击图像时,没有任何反应。 I need it to dropdown the navigation when I click the image. 单击图像时,需要它来下拉导航。 Edited my Javascript code. 编辑了我的Javascript代码。 I added alert to show the current status of what class the toggle is using. 我添加了警报以显示切换器正在使用的类的当前状态。 But still I cant get to change it from header_navigation_mobile to header_navigation_mobile.is_open 但是我仍然无法将其从header_navigation_mobile更改为header_navigation_mobile.is_open

This is my HTML CODE for the Clickable Image 这是我的可点击图片的HTML代码

<a href="#" onclick="toggleMenu()">
    <img class="mobile_navigation_button" src="{{site.baseurl}}/assets/img/menu.svg"/>
</a>

This is the HTML for the drop down navigation 这是下拉导航的HTML

<div class="header_navigation_mobile">
   <ul>
        <li><a href="{{site.baseurl}}/index.html">HOME</a></li>
        <li><a href="#">ABOUT</a></li>
        <li><a href="#">CONTACT</a></li>
        <li><a href="#"> </a></li>
        <li><a href="#"> </a></li>
   </ul>
</div>

Then my CSS For the Clickable Image to show the navigation 然后我的CSS为可点击图片显示导航

.header_navigation_mobile.is_open{
    display: block;
    transform: translateY(0%);
}   

This is the CSS for the Clickable Image first state which is to Hide it 这是Clickable Image第一种状态的CSS,即隐藏它

.header_navigation_mobile{
    display: none;
    position: absolute;
    width: 290px;
    background: #484547;
    transform: translateY(-100%);
    transition: all 0.3s ease-in-out;
}

Then finally my Javascript 然后最后是我的Javascript

function toggleMenu(){
var mobileNav = document.getElementById('mobile_Nav');
var mobileNav_toggle = mobileNav.getAttribute("class");

if(mobileNav_toggle == "header_navigation_mobile") {
    mobileNav_toggle == "header_navigation_mobile.is_open";
}
else {
    mobileNav_toggle == "header_navigation_mobile";
}

alert(mobileNav_toggle);
}

In your JS try like this.use querySelector for selecting elements.And for setting and getting css properties use selector.style.property_name . 在您的JS中尝试这样。使用querySelector选择元素。并为设置和获取CSS属性使用selector.style.property_name

function toggleMenu(){
    var mobileNav_Hide = document.querySelector('.header_navigation_mobile');
    var mobileNav_Show = document.querySelector('.header_navigation_mobile.is_open')
    if(mobileNav_Hide.style.display == "none"){
        mobileNav_Show.stylr.display == "block";
    }
    else{
        mobileNav.style.display = "none";
    }
}

I would give the menu an ID so it's easier to target, then just toggle a class that you use to hide/show the menu. 我会给菜单一个ID,以便于定位,然后只需切换用于隐藏/显示菜单的类即可。

 .header_navigation_mobile { display: none; } .open { display: block; } 
 <a href="#" onclick="toggleMenu()">toggle</a> <div class="header_navigation_mobile" id="mobilenav"> <ul> <li><a href="{{site.baseurl}}/index.html">HOME</a></li> <li><a href="#">ABOUT</a></li> <li><a href="#">CONTACT</a></li> <li> <a href="#"> </a> </li> <li> <a href="#"> </a> </li> </ul> </div> <script> function toggleMenu(){ var nav = document.getElementById('mobilenav'); nav.classList.toggle('open'); } </script> 

I know this is not a specific answer to your question but may be a handy solution as well. 我知道这不是您问题的具体答案,但也可能是一个方便的解决方案。 I put together a jsfiddle of a hidden menu you can easily edit to your needs. 我将隐藏菜单的jsfiddle放在一起,可以轻松地根据需要进行编辑。 Here is the link. 链接在这里。

 #Btn{ position: fixed; right: 20px; width: 20px; height: 24px; background: linear-gradient(#FFF,#DDD); border: #AAA 1px solid; border-radius: 2px; padding: 2px 5px; cursor: pointer; transition: border 0.3s linear 0s; } #Btn:hover{ border: #06F 1px solid; } #Btn:hover div{ background: #06F; } #Btn > div{ width: 20px; height: 4px; background: #333; margin: 3px 0px; border-radius: 4px; transition: background 0.3s linear 0s; } #hidden{ position: fixed; right: -300px; top: 60px; width: 260px; height: 0px; background: #333; color:#ededed; padding:15px; transition: right 0.3s linear 0s; } 
 <script> function toggleBTN(){ var hidden = document.getElementById("hidden"); hidden.style.height = window.innerHeight - 60+"px"; if(hidden.style.right == "0px"){ hidden.style.right = "-300px"; } else { hidden.style.right = "0px"; } } </script> <div id="Btn" onclick="toggleBTN()"> <div></div> <div></div> <div></div> </div> <div id="hidden"> <ul> <li>MENU ITEM 1</li> <li>MENU ITEM 2</li> <li>MENU ITEM 3</li> </ul> </div> <div id="page_content"> <script> for(var i = 0; i < 10; i++){ document.write("<h2>"+(i+1)+". Dummy page content ... </h2>"); } </script> </div> 

Hope this helps :) 希望这可以帮助 :)

There are a few things in the else statement where is mobileNav coming from there is no instance of that anywhere in your code. else语句中有几件事,其中的mobileNav来自于代码中的任何地方。 Taking a step back a minute from your solution does the adding and removal of the classname is_open define the entire show hide behaviour you desire? 从您的解决方案退后一分钟,是否通过添加和删除类名is_open定义了所需的整个显示隐藏行为?

Your process of getting the same element with the subclass present/not present and then trying to set the display property is confusing. 获得与子类存在/不存在的相同元素然后尝试设置display属性的过程令人困惑。

You have an equality comparer when setting the display property when it should be just a single equals which along with my first statement i think is your main problem. 当设置显示属性时,您应该有一个相等比较器,当它应该只是一个等号时,我认为我的第一个陈述是您的主要问题。

Instead of the way you are doing it i would just look at toggling the is_open class this link Toggle class on HTML element without jQuery shows how to do that and also demonstrates a few other ways of toggling styles including without javascript 代替您做事的方式,我只是看切换is_open类,此链接在没有jQuery的HTML元素上切换类展示了如何做到这一点,并且还演示了切换样式的其他几种方式,包括没有javascript的方式。

Finally just check because im being lazy that display is valid as a property and that it shouldnt instead be style.display as others have indicated 最后只是检查一下,因为我很懒惰,display作为一个属性是有效的,而不应该像其他人指出的那样是style.display

js
let c = document.getElementById("dropdownlist");
      let e = document.getElementById("dropdownicon");
      let d = e.classList.toggle('fa-angle-down');
      if(d===true) {
        if(c.style.display==='none'){
          c.style.display = 'block';
        }
        else{
          c.style.display ='block';
        }
      }
      else{
        c.style.display = 'none';
      }

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

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