Bootstrap mobile menu icon change to x close

后端 未结 8 1441
一向
一向 2021-02-05 13:54

EDIT

@zim answer uses 2020 CSS to easily solve the issue and better applies to Bootstrap 4.

The original question and selected answer are still

8条回答
  •  粉色の甜心
    2021-02-05 14:25

    You don't need Javascript, CSS will do the Job

    .navbar-toggle {
        .icon-bar {
            transition: 300ms ease-in-out;
            background-color: #fff;
            position: relative;
            width: 24px;
            height: 3px;
        }
        .icon-bar:last-child {
            -webkit-transform: rotate(-45deg);
            -ms-transform: rotate(-45deg);
            -o-transform: rotate(-45deg);
            transform: rotate(-45deg);
            top: -7px;
        }
        .icon-bar:nth-child(2) {
            -webkit-transform: rotate(45deg);
            -ms-transform: rotate(45deg);
            -o-transform: rotate(45deg);
            transform: rotate(45deg);
            top: 0px;
        }
        .icon-bar:nth-child(3) {
            opacity: 0;
        }
        &.collapsed {
            .icon-bar {
                -webkit-transform: rotate(0deg);
                -ms-transform: rotate(0deg);
                -o-transform: rotate(0deg);
                transform: rotate(0deg);        
                top: 0;
                opacity: 1;
            }
        }
    }
    

提交回复
热议问题