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

一款绿色风格整形公司下拉导航菜单特效

下拉显示的二级导航菜单,展开到底时有一个弹回的动画效果!
  导航菜单
 51037  0

jquer点击按钮触发显示全屏垂直导航菜单特效

一款手机移动端全屏导航代码,鼠标点击切换按钮触发背景放大动画效果,并显示全屏导航菜单。
  导航菜单
 31044  0

jquery左侧竖向可上下滚动导航菜单下拉列表代码

固定于页面左侧浮动,带上下可滚动轮换的箭头按钮,里面的菜单可展开收缩。
  导航菜单
 8760  0

jquery鼠标悬停导航向上显示二级导航菜单

可以自定义nav变量值, nav = 0; // 默认值为0,二级菜单向下滑动显示;值为1,则二级菜单向上滑动显示
  导航菜单
 6776  0

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

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