原生js常用的页面顶部点击按钮展开下拉菜单特效代码

所属分类: 网页特效-导航菜单    2023-12-08 11:32:24

原生js常用的页面顶部点击按钮展开下拉菜单特效代码 ie兼容6
反馈问题  查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

原生js常用的页面顶部点击按钮展开下拉菜单特效代码(共3个文件)

    • index.html

使用方法

//The menu js class:
class sikFloatingMenu {
    menuEl = null;
    constructor(_menu) {
        //The menu element:
        this.menuEl = typeof _menu === 'string'
                      ? document.querySelector(_menu)
                      : _menu;
        //Attach handlers:
        this.attachHandlers();
    }
    attachHandlers() {
        if (this.menuEl) {
            this._on(this.menuEl, 'click', '.trigger-menu', this._handler.bind(this));
        }
    }
    _open(item) {
        let opened = item.closest('.fmenu').querySelectorAll('.trigger-menu.open');
        for (const ele of opened) {
            this._close(ele);
        }
        item.classList.add('open');
        //expand:
        let list = item.closest('li').querySelector(".floating-menu");
        list.style.setProperty("max-height", this._measureExpandableList(list));
        list.style.setProperty("opacity", "1");
        item.style.setProperty("max-width", this._measureExpandableTrigger(item));
    }
    _close(item) {
        let list = item.closest('li').querySelector(".floating-menu");
        item.classList.remove('open');
        //shrink:
        list.style.removeProperty("max-height");
        list.style.removeProperty("opacity");
        item.style.removeProperty("max-width");
    }
    _measureExpandableList(list) {
        const items = list.querySelectorAll('li');
        return (items.length * this._getHeight(items[0], "outer") + 10) + 'px';
    }
    _measureExpandableTrigger(item) {
        const textEle = item.querySelector('.text');
        const sizeBase = this._getWidth(item, "outer");
        const sizeExpandLabel = this._getWidth(textEle, "outer");
        return (sizeBase + sizeExpandLabel + 6) + 'px';
    }
    _handler(el, ev) {
        if (el.classList.contains('open')) {
            this._close(el);
        } else {
            this._open(el);
        }
    }
    _on(ele, type, selector, handler) {
        ele.addEventListener(type, function(ev) {
            let el = ev.target.closest(selector);
            if (el) handler.call(this, el, ev); //The element is bind to this
        });
    }
    _getWidth(el, type) {
        if (type === 'inner') 
            return el.clientWidth;
        else if (type === 'outer') 
            return el.offsetWidth;
        return 0;
    }
    _getHeight(el, type) {
        if (type === 'inner')
            return el.clientHeight;
        else if (type === 'outer')
            return el.offsetHeight;
        return 0;
    }
}

//Intialize menu: 
window.sik_menu = new sikFloatingMenu("#mymenu");

站长提示:
1. 平台上所有素材资源,需注册登录会员方能正常下载。
2. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励20K币
3. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
4. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 导航菜单

原生js精仿早期淘宝商品分类导航菜单

一款深红色风格的分类导航菜单,适用于大型门户类网站,很丝滑很实用。
  导航菜单
 212  

jquery滚动顶部悬浮固定二级导航菜单

一款蓝色悬浮贴顶的导航菜单,带有二级导航,很简单实用。
  导航菜单
 312  

jquery树形结构无限级展开收缩的下拉菜单代码

一个轻量级简单实用的侧边栏树结构导航菜单插件,从HTML嵌套的无序列表生成树状视图导航菜单。
  导航菜单
 276  

jquery右侧悬浮固定多级定位导航菜单插件

一款置于页面右侧悬浮定位滚动导航小插件,支持二级导航菜单,可定位滚动到指定位置。
  导航菜单
 183  

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论