FrontEnd 步步高升:Dropdown,纯javascript实现下拉菜单

实现思路:

  • 获取class="dropdown"的所有元素
  • 验证其data-toggle是否为dropdown
  • 添加 onclick 事件
  • dropdown 上增加/删除
    "open" class
  • 判断open实现相应的toggle进行开/关

目前只完成了:

  1. SimpleDropdown              :  无动画效果, 通过修改display实现开/关
  2. AccordionDropdown         :  有上下移动的动画效果,通过修改height, 实现动画效果和开/关

图上左边蓝色的是通用方法(继承),右边的是各自实现的方法(接口).

通用方法:

function isDropdownToggle(toggle) {
    if (toggle.attributes["data-toggle"].value == "dropdown") {
        return true;
    }
    return false;
}
function isOpen(dropdownNode) {
    if (dropdownNode.className.indexOf("open") > -1) {
        return true;
    }
    return false;
}

SimpleDropdown DOM

                       <li class="dropdown" data-toggle="dropdown">
                            <a class="btn" href="#">
                                <i class="halflings-icon white user"></i> Cdk
                                <span class="caret"></span>
                            </a>
                            <ul class="dropdown-menu">
                                <li class="dropdown-menu-title">
                                    <span>帐户设置</span>
                                </li>
                                <li>
                                    <a href="#"><i class="halflings-icon user"></i> 个人信息</a>
                                </li>
                                <li>
                                    <a href="#"><i class="halflings-icon off"></i> 注销</a>
                                </li>
                            </ul>
                        </li>

SimpleDropdown JS

function initSimpleDropdown() {

    var dropdownsToggles = document.getElementsByClassName("dropdown");
    for (var i = 0; i < dropdownsToggles.length; i++) {
        if (!isDropdownToggle(dropdownsToggles[i])) {
            continue;
        }
        dropdownsToggles[i].onclick = function () {
            var dropdownMenus = this.getElementsByClassName("dropdown-menu");
            if (dropdownMenus.length > 0) {
                toggle(this, dropdownMenus);
            }
        };
    }    

    function toggleDropdown(dropdownMenus, operate) {
        for (var j = 0; j < dropdownMenus.length; j++) {
            dropdownMenus[j].style.display = operate;
        }
    }

    function toggle(target, dropdownMenus){
        if (isOpen(target)) {
            target.className = target.className.replace("open", "");
            toggleDropdown(dropdownMenus, "none");
        } else {
            target.className = target.className + " " + "open";
            toggleDropdown(dropdownMenus, "inline-block");
        }
    }

}

AccordionDropdown DOM

<li class="accordion-dropdown" data-toggle="dropdown">
                    <a href="#"><i class="icon-folder-close-alt"></i><span> Dropdown</span><span class="label label-important"> 3 </span></a>
                        <ul>
                            <li>
                                <a class="submenu" href="submenu.html"><i class="icon-file-alt"></i><span class="hidden-tablet"> Sub Menu 1</span></a>
                           </li>
                           <li><a class="submenu" href="submenu2.html"><i class="icon-file-alt"></i><span class="hidden-tablet"> Sub Menu 2</span></a></li>
                            <li><a class="submenu" href="submenu3.html"><i class="icon-file-alt"></i><span class="hidden-tablet"> Sub Menu 3</span></a></li>
                        </ul>
                </li>

AccordionDropdown JS

function accordionDropdown(){
    var accordionDropdowns = document.getElementsByClassName("accordion-dropdown");
    for(var i = 0;i < accordionDropdowns.length;i++){
        if(!isDropdownToggle(accordionDropdowns[i])){

           continue;
        }
        accordionDropdowns[i].onclick = function(){
            var uls = this.getElementsByTagName("ul");
            if(uls.length > 0){

                toggle(this, uls);
            }
        }
    }

    function toggleDropdown(dropdownMenus, operate) {
        for (var j = 0; j < dropdownMenus.length; j++) {
            dropdownMenus[j].style.height = operate;
        }
    }

    function toggle(target, dropdownMenus){
        var subElements = dropdownMenus[0].getElementsByTagName("li");
        var totalExpandHeight = subElements[0].offsetHeight * subElements.length;
        if (isOpen(target)) {
            target.className = target.className.replace("open", "");
            toggleDropdown(dropdownMenus, "0px");
        } else {
            target.className = target.className + " " + "open";
            toggleDropdown(dropdownMenus, totalExpandHeight.toString() + "px");
        }
    }
}
时间: 2024-11-02 23:23:56

FrontEnd 步步高升:Dropdown,纯javascript实现下拉菜单的相关文章

纯css+js下拉菜单实例代码

纯css+js下拉菜单实例代码 分享一个css+js下拉菜单代码,js+css实现的简单下拉菜单,兼容性不错. 例子:<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <

纯CSS实现下拉菜单及下拉容器等(纯CSS实现导航条及导航下拉容器)

原文:纯CSS实现下拉菜单及下拉容器等(纯CSS实现导航条及导航下拉容器) 虽然网上类似甚至相同的案例有很多,但是我还是写下,以记下笔记,也可供大家参考 希望大家可以指导批评~~ 首先我们以列表ul li 来开始我们菜单也可以说导航条的制作: 在页面中我们首先构建以下XHTML结构: <body> <ul id="navWrapper"> <li> <a href="#">Menu A</a> <ul

javascript 横向下拉菜单演示

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>css横向下拉菜单演示</title><style type="text/c

纯css实现下拉菜单

今天给大家分享一个纯html+css实现的下拉菜单.在此声明一点,源码并非出自本人之手,是同项目组一兄弟PLUTO写的.好东西嘛,所以果断拿出来和大家分享.如果有更好的想法或者建议,一定记得留言哦!好了,直接上代码:(IE6请直接越过) <!DOCTYPE html><html lang="en"><head><!-- create by pluto on 13-12-6 --><title>MENU</title>

用纯css实现下拉菜单的几种方式

第一种:display:none和display:block切换 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <style> 7 ul{ 8 list-style: none; 9 } 10 .nav>li{ 11 float

javascript实现下拉菜单的显示与隐藏

demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>实现下拉菜单效果</title> <link rel="stylesheet" type="text/css" href="demo.css"> <script

javascript + css 下拉菜单 2.0版

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> New Document </title> <meta http-equiv="Content-Type" content="

纯CSS制作下拉菜单

<style> body{padding:0;margin:0;width:100%;height:100%;color:#111} a{color:#111;text-decoration:none} ul,li{list-style:none;padding:0;margin:0} .nav{width:1000px;margin:0 auto;height:28px;line-height:28px} .nav li{width:100px;background:#ccc;border:

Javascript实现下拉菜单

效果如下: 全部代码如下: