39.手机导航

手机导航

html部分

css部分

*{
    margin: 0;
    padding: 0;
}
body{
    background-color: rgb(184, 138, 203);
    height: 100vh;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}
img{
    width: 350px;
    height: 550px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    object-fit: cover;
    position: absolute;
    transition: all .5s;
    opacity: 0;
}
img.active{
    opacity: 1;
}

.phone{
    height: 600px;
    width: 350px;
    padding: 2px;
    border-radius: 10px;
    background-color: #fff;
}
.content{
    overflow: hidden;
    height: 550px;
    position: relative;
}
.nav{
    padding: 10px;
    display: flex;
    justify-content: space-around;
}
i{
    font-size: 24px !important;
    color: #777777;
    cursor: pointer;
}
i.active{
    color: #8e44ad !important;
}

js部分

// 获取dom
const is = document.querySelectorAll("i")
const imgs = document.querySelectorAll("img")


is.forEach((item, index) => {
    item.addEventListener("click", function () {
        // 如果非活动状态添加活动类
        if (!item.classList.contains("active")) {
            item.classList.add("active")
            imgs[index].classList.add("active")
        }

        // 移除其他元素的活动类
        is.forEach((sub, idx) => {
            if (index != idx) {
                sub.classList.remove("active")
            }
        })
        imgs.forEach((sub, idx) => {
            if (index != idx) {
                sub.classList.remove("active")
            }
        })
    }
    )
})

效果

你可能感兴趣的:(前端开发小案例,前端)