RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1051251
Accepted
VanilJS
VanilJS
Asked:2020-11-27 17:56:45 +0000 UTC2020-11-27 17:56:45 +0000 UTC 2020-11-27 17:56:45 +0000 UTC

window.resize 时如何从菜单中删除闪烁或如何适应动态菜单?

  • 772

动态菜单脚本(不同数量的项目)

如果菜单不适合(比容器宽) - 激活移动视图。
如果菜单适合(比容器窄) - 删除移动视图。


如何去除闪烁?
或者在这种情况下为动态菜单编写脚本会更正确吗?

脚本代码(尝试调整页面大小,菜单会闪烁)-

var headerWrapper = document.querySelector('.header-wrapper'),
        headerContainer = document.querySelector('.header .container'),
        logo = document.querySelector('.logo'),
        nav = document.querySelector('nav'),
        headerBtn = document.querySelector('.header .btn'),
        gamburger = document.querySelector('.gamburger-wrapper');

    
    window.onload = mobileMenu; // Сравниваем ширину при загрузке
    window.addEventListener('resize', mobileMenu); // Сравниваем ширину при загрузке

    
    function mobileMenu() {
        // Если ширина контейнера меньше, чем сумма ширин блоков
        if (headerContainer.offsetWidth < (logo.offsetWidth + nav.offsetWidth + headerBtn.offsetWidth)) {
            
            // true - активируем мобильный вид
            headerWrapper.classList.add('mobile')
            
            // false - убираем мобильный вид
        } else {
            headerWrapper.classList.remove('mobile')
        }
    }

    
    // Нажатие на гамбургер меню
    gamburger.addEventListener('click', () => {
        nav.classList.toggle('active')
    })
*,
    :after,
    :before {
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
    }

    :focus {
        outline: none
    }

    .headerWrapper {
        padding: 0;
        margin: 0;
        background-color: #f2f2f2
    }

    p {
        color: #000
    }

    ul {
        list-style: none
    }

    a {
        color: inherit;
        text-decoration: none
    }

    .btn,
    a {
        cursor: pointer
    }

    .btn {
        color: #838b93;
        padding: 5px 16px 4px;
        margin: 0;
        height: 32px;
        background-color: #fff;
    }

    .btn,
    .btn:hover {
        -webkit-transition: all .3s;
        transition: all .3s
    }

    .btn:hover {
        color: #485d67;
    }

    .container {
        height: 100%;
        max-width: 960px;
        padding: 0 15px;
        margin: 0 auto;
        position: relative
    }

    .header {
        display: block;
        width: 100%;
        height: 40px;
        padding: 0;
        margin: 0;
        position: relative;
        background-color: #fff;
        -webkit-box-shadow: 0 2px 4px rgba(0, 0, 0, .1);
        box-shadow: 0 2px 4px rgba(0, 0, 0, .1)
    }

    .header .container {
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -webkit-box-pack: justify;
        -ms-flex-pack: justify;
        justify-content: space-between;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center
    }

    .header .container nav {
        margin-left: auto
    }

    .header ul {
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-wrap: nowrap;
        flex-wrap: nowrap;
        height: 100%;
        width: 100%;
        color: #838b93;
        padding: 0;
        margin-right: 20px
    }

    .header ul li {
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
        height: 100%;
        -webkit-box-flex: 0;
        -ms-flex: 0;
        flex: 0;
        padding: 5px 14px
    }

    .header ul li:hover {
        cursor: pointer;
        color: #485d67
    }

    .header .logo {
        display: block;
        min-width: 105px;
        font-weight: 700;
        font-size: 20px;
        color: #444
    }

    .header .logo span {}

    .mobile .btn {
        margin-right: 0
    }

    .mobile .header nav {
        display: none;
        position: absolute;
        top: 40px;
        right: 0;
        background-color: #fff
    }

    .mobile .header nav ul {
        display: block;
        margin-top: 0;
        margin-bottom: 0;
        -webkit-box-shadow: -2px 2px 4px rgba(0, 0, 0, .1);
        box-shadow: -2px 2px 4px rgba(0, 0, 0, .1)
    }

    .mobile .header nav ul li {
        display: block;
        padding: 10px;
        border-left: 2px solid transparent
    }

    .mobile .header nav ul li:hover {
        background-color: #f2f2f2;
        border-left: 2px solid #838b93
    }

    .mobile .header .active {
        display: block
    }

    .mobile .gamburger-wrapper {
        display: block;
        cursor: pointer;
        height: 30px;
        width: 30px;
        position: relative
    }

    .mobile .gamburger {
        display: block;
        width: 30px;
        height: 3px;
        background-color: #777;
        position: absolute;
        top: 45%;
        left: 0
    }

    .mobile .gamburger:after {
        top: 7px
    }

    .mobile .gamburger:after,
    .mobile .gamburger:before {
        content: "";
        display: block;
        width: 30px;
        height: 3px;
        position: absolute;
        background-color: #777;
        left: 0
    }

    .mobile .gamburger:before {
        bottom: 7px
    }
<div class="header-wrapper">
    <div class="header">
        <div class="container">
            <div class="logo">
                <span>Лого</span>
            </div>
            <nav>
                <ul>
                    <li><a href="#">Меню1</a></li>
                    <li><a href="#">Меню2</a></li>
                    <li><a href="#">Меню3</a></li>
                    <li><a href="#">Меню4</a></li>
                </ul>
            </nav>
            <button class="btn">Кнопка</button>
            <div class="gamburger-wrapper">
                <div class="gamburger"></div>
            </div>
        </div>
    </div>
</div>

HTML 文件示例https://yadi.sk/d/UKDYOJpPczu8FQ

javascript
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Get-Web
    2020-11-27T18:53:55Z2020-11-27T18:53:55Z

    你使用奇怪的逻辑,你是这样想的:

    如果 容器的宽度 小于 徽标的宽度+菜单的宽度+按钮的宽度,则添加移动类。但是通过添加移动类,你隐藏了菜单,所以你仍然有徽标宽度+按钮宽度,之后你的条件不起作用,因为现在容器的宽度更大并且移动 类被删除等等一个圆圈……

    初始菜单宽度为常数值的示例:

    var headerWrapper = document.querySelector('.header-wrapper'),
            headerContainer = document.querySelector('.header .container'),
            logo = document.querySelector('.logo'),
            nav = document.querySelector('nav'),
            navWidth = document.querySelector('nav').offsetWidth,
            headerBtn = document.querySelector('.header .btn'),
            gamburger = document.querySelector('.gamburger-wrapper');
    
        
        window.onload = mobileMenu; // Сравниваем ширину при загрузке
        window.addEventListener('resize', mobileMenu); // Сравниваем ширину при загрузке
    
        
        function mobileMenu() {
            // Если ширина контейнера меньше, чем сумма ширин блоков
            if (headerContainer.offsetWidth < (logo.offsetWidth + navWidth + headerBtn.offsetWidth)) {
                
                // true - активируем мобильный вид
                headerWrapper.classList.add('mobile')
                
                // false - убираем мобильный вид
            } else {
                headerWrapper.classList.remove('mobile')
            }
        }
    
        
        // Нажатие на гамбургер меню
        gamburger.addEventListener('click', () => {
            nav.classList.toggle('active')
        })
    *,
        :after,
        :before {
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
        }
    
        :focus {
            outline: none
        }
    
        .headerWrapper {
            padding: 0;
            margin: 0;
            background-color: #f2f2f2
        }
    
        p {
            color: #000
        }
    
        ul {
            list-style: none
        }
    
        a {
            color: inherit;
            text-decoration: none
        }
    
        .btn,
        a {
            cursor: pointer
        }
    
        .btn {
            color: #838b93;
            padding: 5px 16px 4px;
            margin: 0;
            height: 32px;
            background-color: #fff;
        }
    
        .btn,
        .btn:hover {
            -webkit-transition: all .3s;
            transition: all .3s
        }
    
        .btn:hover {
            color: #485d67;
        }
    
        .container {
            height: 100%;
            max-width: 960px;
            padding: 0 15px;
            margin: 0 auto;
            position: relative
        }
    
        .header {
            display: block;
            width: 100%;
            height: 40px;
            padding: 0;
            margin: 0;
            position: relative;
            background-color: #fff;
            -webkit-box-shadow: 0 2px 4px rgba(0, 0, 0, .1);
            box-shadow: 0 2px 4px rgba(0, 0, 0, .1)
        }
    
        .header .container {
            display: -webkit-box;
            display: -ms-flexbox;
            display: flex;
            -webkit-box-pack: justify;
            -ms-flex-pack: justify;
            justify-content: space-between;
            -webkit-box-align: center;
            -ms-flex-align: center;
            align-items: center
        }
    
        .header .container nav {
            margin-left: auto
        }
    
        .header ul {
            display: -webkit-box;
            display: -ms-flexbox;
            display: flex;
            -ms-flex-wrap: nowrap;
            flex-wrap: nowrap;
            height: 100%;
            width: 100%;
            color: #838b93;
            padding: 0;
            margin-right: 20px
        }
    
        .header ul li {
            display: -webkit-box;
            display: -ms-flexbox;
            display: flex;
            -webkit-box-align: center;
            -ms-flex-align: center;
            align-items: center;
            height: 100%;
            -webkit-box-flex: 0;
            -ms-flex: 0;
            flex: 0;
            padding: 5px 14px
        }
    
        .header ul li:hover {
            cursor: pointer;
            color: #485d67
        }
    
        .header .logo {
            display: block;
            min-width: 105px;
            font-weight: 700;
            font-size: 20px;
            color: #444
        }
    
        .header .logo span {}
    
        .mobile .btn {
            margin-right: 0
        }
    
        .mobile .header nav {
            display: none;
            position: absolute;
            top: 40px;
            right: 0;
            background-color: #fff
        }
    
        .mobile .header nav ul {
            display: block;
            margin-top: 0;
            margin-bottom: 0;
            -webkit-box-shadow: -2px 2px 4px rgba(0, 0, 0, .1);
            box-shadow: -2px 2px 4px rgba(0, 0, 0, .1)
        }
    
        .mobile .header nav ul li {
            display: block;
            padding: 10px;
            border-left: 2px solid transparent
        }
    
        .mobile .header nav ul li:hover {
            background-color: #f2f2f2;
            border-left: 2px solid #838b93
        }
    
        .mobile .header .active {
            display: block
        }
    
        .mobile .gamburger-wrapper {
            display: block;
            cursor: pointer;
            height: 30px;
            width: 30px;
            position: relative
        }
    
        .mobile .gamburger {
            display: block;
            width: 30px;
            height: 3px;
            background-color: #777;
            position: absolute;
            top: 45%;
            left: 0
        }
    
        .mobile .gamburger:after {
            top: 7px
        }
    
        .mobile .gamburger:after,
        .mobile .gamburger:before {
            content: "";
            display: block;
            width: 30px;
            height: 3px;
            position: absolute;
            background-color: #777;
            left: 0
        }
    
        .mobile .gamburger:before {
            bottom: 7px
        }
    <div class="header-wrapper">
        <div class="header">
            <div class="container">
                <div class="logo">
                    <span>Лого</span>
                </div>
                <nav>
                    <ul>
                        <li><a href="#">Меню1</a></li>
                        <li><a href="#">Меню2</a></li>
                        <li><a href="#">Меню3</a></li>
                        <li><a href="#">Меню4</a></li>
                    </ul>
                </nav>
                <button class="btn">Кнопка</button>
                <div class="gamburger-wrapper">
                    <div class="gamburger"></div>
                </div>
            </div>
        </div>
    </div>

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5