【问题标题】:Neat UX Trick Not working. How can I fix it?整洁的 UX 技巧不起作用。我该如何解决?
【发布时间】:2020-10-10 18:42:30
【问题描述】:

问题

javascript 顶部的注释代码是我尝试使相关选择的按钮选项卡(在导航栏上更改颜色、获取背景等)。但是当我取消注释顶部的代码时,它不仅不起作用,而且汉堡菜单也停止工作!

如何调整它以使课程正常工作(使所选选项卡对用户显而易见)?

我的代码

//Nav Bar!!

//Button selected gets color change

/*
const selectedNav = document.querySelectorAll('li');

selectedNav.forEach((item)=>{
    document.selectedNav.addEventListener('click', navChange)
})

function navChange(event){
    ul.forEach((item)=>{
        item.classList.remove('add-this-to-selected-section');
    })
    event.target.classList.add('add-this-to-selected-section');
}
*/

const navSlide = () =>{
    const burger = document.querySelector('.burger');
    const nav = document.querySelector('.nav-links');
    const navLinks = document.querySelectorAll('.nav-links li');

// Toggle Nav
    burger.addEventListener('click',()=>{
        nav.classList.toggle('nav-active');


    //Animate Links
    navLinks.forEach((link, index)=>{
        if(link.style.animation){
            link.style.animation = '';
        } else {
        link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7 + 0.5}s`
        }
   
    })
    //Burger Animation

    burger.classList.toggle('toggle');

})

}

navSlide();
nav{
    display: flex;
    justify-content: space-around;
    align-items: center;
    min-height: 8vh;
    font-family: 'Poppins', sans-serif;
    background-color: #ffffff;
}


.nav-links{
    display: flex;
    justify-content: space-around;
    width: 50%;
}

.nav-links li{
   list-style: none;
}

.nav-links a{
    color: #245871;
    text-decoration: none;
    letter-spacing: 3px;
    font-weight: 800;
    font-size: 18px;
}

.add-this-to-selected-section{
    color: white;
    background-color: #245871;
    padding: 5px;
    border-radius: 5px;
}

.burger div{
    width: 25px;
    height: 3px;
    background-color: #245871;
    margin: 5px;
    transition: all 0.3s ease;
}

.burger{
    display: none;
    cursor: pointer;
}


@media screen and (max-width: 1024){
    .nav-links{
        width: 70%;
        cursor: pointer;
        font-weight: 1200;
    }
}

@media screen and (max-width: 997px){
   body{
       overflow-x: hidden;
   }
   
   nav{
       color: white;
   }

   .head-btn{
       color: white;
   }

    .nav-links{
        position: absolute;
        right: 0px;
        height: 92vh;
        top: 8vh;
        background-color: #245871;
        color: white;
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 70%;
        transform: translateX(100%);
        transition: transform 0.5s ease-in;
    }

    .nav-links a{
        opacity: 1;
        color: white;
    }
    .burger{
      display: block;
    }
}

.nav-active{
    transform: translateX(0%);
    color: white;
}

@keyframes navLinkFade{
    from{
        opacity: 0;
        transform: translateX(50px);
    } to {
        opacity: 1;
        transform: translateX(0px);
    }
}
<div class="logo">
                    <h4>xxx</h4>    
            </div>
            <ul class="nav-links">
                <li class=".home-btn"><a href="#">xxx</a></li>
                <li class=".exchange-btn"><a href="#">xxx</a></li>
                <li class=".debit-btn"><a href="#">xxx</a></li>
                <li class=".crypto-btn"><a href="#">xxx</a></li>
                <li class=".stock-btn"><a href="#">xxx</a></li>
            </ul>
            <div class="burger">    
                <div class="line1"></div>
                <div class="line2"></div>
                <div class="line3"></div>

            </div>

非常感谢您的帮助:)

【问题讨论】:

    标签: javascript html css user-interface user-experience


    【解决方案1】:

    该类的背景颜色相同,并且该功能的 javascript 也存在一些问题。应该是这样的:

    const selectedNav = document.querySelectorAll('li');
    
    const navChange = (index) => {
        selectedNav.forEach((item)=>{
            item.classList.remove('add-this-to-selected-section');
        })
        selectedNav[index].classList.add('add-this-to-selected-section');
    }
    
    
    selectedNav.forEach((item, index)=>{
        item.addEventListener('click', ()=> navChange(index))
    })
    
    
    
    const navSlide = () =>{
        const burger = document.querySelector('.burger');
        const nav = document.querySelector('.nav-links');
        const navLinks = document.querySelectorAll('.nav-links li');
    
    // Toggle Nav
        burger.addEventListener('click',()=>{
            nav.classList.toggle('nav-active');
    
    
        //Animate Links
        navLinks.forEach((link, index)=>{
            if(link.style.animation){
                link.style.animation = '';
            } else {
            link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7 + 0.5}s`
            }
       
        })
        //Burger Animation
    
        burger.classList.toggle('toggle');
    
    })
    
    }
    
    navSlide();
    nav{
        display: flex;
        justify-content: space-around;
        align-items: center;
        min-height: 8vh;
        font-family: 'Poppins', sans-serif;
        background-color: #ffffff;
    }
    
    
    .nav-links{
        display: flex;
        justify-content: space-around;
        width: 50%;
    }
    
    .nav-links li{
       list-style: none;
    }
    
    .nav-links a{
        color: #245871;
        text-decoration: none;
        letter-spacing: 3px;
        font-weight: 800;
        font-size: 18px;
    }
    
    .add-this-to-selected-section{
        color: white;
        background-color: red;
        padding: 5px;
        border-radius: 5px;
    }
    
    .burger div{
        width: 25px;
        height: 3px;
        background-color: #245871;
        margin: 5px;
        transition: all 0.3s ease;
    }
    
    .burger{
        display: none;
        cursor: pointer;
    }
    
    
    @media screen and (max-width: 1024){
        .nav-links{
            width: 70%;
            cursor: pointer;
            font-weight: 1200;
        }
    }
    
    @media screen and (max-width: 997px){
       body{
           overflow-x: hidden;
       }
       
       nav{
           color: white;
       }
    
       .head-btn{
           color: white;
       }
    
        .nav-links{
            position: absolute;
            right: 0px;
            height: 92vh;
            top: 8vh;
            background-color: #245871;
            color: white;
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 70%;
            transform: translateX(100%);
            transition: transform 0.5s ease-in;
        }
    
        .nav-links a{
            opacity: 1;
            color: white;
        }
        .burger{
          display: block;
        }
    }
    
    .nav-active{
        transform: translateX(0%);
        color: white;
    }
    
    @keyframes navLinkFade{
        from{
            opacity: 0;
            transform: translateX(50px);
        } to {
            opacity: 1;
            transform: translateX(0px);
        }
    }
    <div class="logo">
                        <h4>xxx</h4>    
                </div>
                <ul class="nav-links">
                    <li class=".home-btn"><a href="#">xxx</a></li>
                    <li class=".exchange-btn"><a href="#">xxx</a></li>
                    <li class=".debit-btn"><a href="#">xxx</a></li>
                    <li class=".crypto-btn"><a href="#">xxx</a></li>
                    <li class=".stock-btn"><a href="#">xxx</a></li>
                </ul>
                <div class="burger">    
                    <div class="line1"></div>
                    <div class="line2"></div>
                    <div class="line3"></div>
    
                </div>

    【讨论】:

    • 发布的第一个代码有点错误,所以我更新了它。
    猜你喜欢
    • 2020-12-27
    • 2019-12-26
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多