简体   繁体   English

Javascript:如何创建下拉效果? (没有 JQuery)

[英]Javascript: How to create a dropdown effect? (Without JQuery)

At the moment when the menu button is clicked the dropdown displays, however, clicking the button once more does not hide the dropdown menu as it should.在单击菜单按钮时,会显示下拉菜单,但是,再次单击该按钮并不会像应有的那样隐藏下拉菜单。

Script.js脚本.js

//Variables
var menuBtn = document.getElementById('menu__icon');
var dropdown = document.getElementsByClassName('dropdown__menu')[0];

var isClicked = false;

// Drop down Mobile
menuBtn.addEventListener('click', function () {
  if (isClicked == false) {
    //Btn Styles
    menuBtn.style.backgroundColor = "none";
    menuBtn.style.color = "black";
    //Menu visible
    dropdown.style.display = "none";
  } else {
    isClicked = true;
    //Btn Styles
    menuBtn.style.backgroundColor = "#1f283b";
    menuBtn.style.color = "black";
    //Menu visible
    dropdown.style.display = "block";
  }
});

it is better to change the content of your 'if block statement' to display the dropdown since the initial value of isClicked is false.最好更改“if 块语句”的内容以显示下拉列表,因为 isClicked 的初始值为 false。 After clicking the button then isCliked must be updated to true since you clicked it.单击该按钮后,必须将 isCliked 更新为 true,因为您单击了它。 Then the else block will contain the code for hiding the dropdown then update the isClicked variable to false.然后 else 块将包含隐藏下拉列表的代码,然后将 isClicked 变量更新为 false。

isClicked = false;

// inside the event listener
if(isClicked==false){
    //show dropdown
    isClicked = true;
}else{
    //hide dropdown
    isClicked = false;
}

you can also use jquery Library and use toggle() function您还可以使用 jquery 库并使用 toggle() 函数

set isClicked to true in the first if statement then false in the other.在第一个 if 语句中将 isClicked 设置为 true,然后在另一个 if 语句中设置为 false。

    //Variables
    var menuBtn = document.getElementById('menu__icon');
    var dropdown = document.getElementsByClassName('dropdown__menu')[0];

    var isClicked = false;

    // Drop down Mobile
    menuBtn.addEventListener('click', function(){
    
    if (isClicked == false) {
    //Btn Styles
    menuBtn.style.backgroundColor = "none";
    menuBtn.style.color = "black";
    //Menu visible
    dropdown.style.display = "none";
    isClicked = true
    } else {
        isClicked = false;
        //Btn Styles
        menuBtn.style.backgroundColor = "#1f283b";
        menuBtn.style.color = "black";
        //Menu visible
        dropdown.style.display = "block";
    }
    
    });

You'd be better off using CSS, toggling a class on and off.您最好使用 CSS,打开和关闭类。 With this approach you could then also leverage CSS Transitions.通过这种方法,您还可以利用 CSS Transitions。

 var menuBtn = document.getElementById('menu__icon'); var dropdown = document.getElementsByClassName('dropdown__menu')[0]; menuBtn.addEventListener("click", function() { this.classList.toggle("active"); dropdown.classList.toggle("active"); });
 .dropdown__menu { /*display: none;*/ transform: scaleY(0); transform-origin: top; transition: transform 0.26s ease; } .dropdown__menu.active { /*display: block;*/ transform: scaleY(1); } #menu__icon { transition: all 1s; } #menu__icon.active { background-color: #1f283b; color: #FFF; }
 <button id="menu__icon">Click Me</button> <div class="dropdown__menu">I'm a menu</div>

If you really want to keep the style in the javascript, invert the isClicked variable in one spot.如果您真的想保留 javascript 中的样式,请在一处反转 isClicked 变量。

//Variables
var menuBtn = document.getElementById('menu__icon');
var dropdown = document.getElementsByClassName('dropdown__menu')[0];

var isClicked = false;

// Drop down Mobile
menuBtn.addEventListener('click', function () {
  if (!isClicked) {
    //Btn Styles
    menuBtn.style.backgroundColor = "none";
    menuBtn.style.color = "black";
    //Menu visible
    dropdown.style.display = "none";
  } else {
    //Btn Styles
    menuBtn.style.backgroundColor = "#1f283b";
    menuBtn.style.color = "black";
    //Menu visible
    dropdown.style.display = "block";
  }
  //Invert true/false
  isClicked = !isClicked
});

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

相关问题 如何在不使用jQuery的情况下为javascript页面赋予淡出效果 - how to give fadeout effect to a page in javascript without using jQuery 如何使用 jquery 和 javascript 创建自动向下滚动效果? - How to create auto-scroll down effect with jquery & javascript? 如何在没有 Jquery 的情况下在 CSS Flexbox 中创建可点击的动画下拉菜单? - How to create a clickable animated dropdown in CSS Flexbox without Jquery? 如何使用 Javascript 创建重力效果? - How to create a gravity effect with Javascript? Bootstrap Dropdown效果-jQuery - Bootstrap Dropdown effect - jQuery 使用不带jQuery的JavaScript的下拉菜单选择 - Dropdown menu selection using JavaScript without jQuery 无法使用javascript和jquery创建下拉框 - Can't create dropdown box with javascript and jquery 如何知道没有iphone和ipad,任何javascript / jquery效果/插件将适用于那些? - How to know without having iphone and ipad, any javascript/jquery effect/plugin will work on those? 如何在没有jQuery的情况下使用纯JavaScript制作像fullpage.js这样的快照滚动效果 - How to make snap scroll effect like fullpage.js with pure javascript without jQuery 如何使用网络编程创建文本曲线效果。 首选(C#,VB JavaScript或Jquery) - How to create text curve effect using web programming. preferring (c#, vb javascript or jquery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM