单击链接后自动关闭菜单

时间:2019-08-02 13:27:22

标签: javascript html css

目前,我的导航栏包含指向一页上各个部分的链接,而没有指向其他页面的任何链接。单击链接后,它会自动转到该页面上的相应div,但是导航栏会覆盖目标div的一部分,导航栏将保持打开状态,直到单击关闭按钮为止。

我的问题是,一旦单击链接,是否有一种方法可以强制关闭标题?

HTML:

<div class="navigation">
            <div class="container">
                <nav>
                    <div class="col">
                        <h3>Why Tracker?</h3>
                        <ul>
                            <li><a href="#intro-block">Learn more</a></li>
                        </ul>
                    </div>

                    <div class="col">
                        <h3>Key Features</h3>
                        <ul>
                            <li><a href="#features-wrapper">View all</a></li>
                        </ul>
                    </div>

                    <div class="col">
                        <h3>How to Buy</h3>
                        <ul>
                            <li><a href="#htb">Learn more</a></li>
                        </ul>
                    </div>
                    <div class="col">
                        <h3>FAQ's</h3>
                        <ul>
                            <li><a href="#faq">View all</a></li>
                        </ul>
                    </div>

                    <div class="col">
                        <h3>Where to Buy</h3>
                        <ul>
                            <li><a href="http://www.oxfordproducts.com/motorcycle/where_to_buy" target="_blank">Store locator</a></li>
                            <li><a href="https://www.oxfordproducts.com/trade/login.php" target="_blank">Trade customer login</a></li>
                        </ul>
                    </div>




                    <div class="col">
                        <a href="https://www.facebook.com/OxfordProductsLtd/" target="_blank">
                            <div class="social-link"><i class="fab fa-facebook-f"></i></div>
                        </a>
                        <a href="https://twitter.com/oxfordproducts?lang=en" target="_blank">
                            <div class="social-link"><i class="fab fa-twitter"></i></div>
                        </a>
                        <a href="https://www.instagram.com/oxfordproducts/" target="_blank">
                            <div class="social-link"><i class="fab fa-instagram"></i></div>
                        </a>
                        <a href="https://www.youtube.com/user/OxfordProductsLtd" target="_blank">
                            <div class="social-link"><i class="fab fa-youtube"></i></div>
                        </a>
                    </div>
                </nav>
            </div>
        </div>
        <div class="menu">
            <div class="container">
                <a href="http://oxfordluggage.com/"><img class="logo" src="Images/Logos/Oxford-tracker-Logo-white.png" alt="Oxford Tracker logo"></a>
                <div class="menu-trigger">
                    <div class="bar bar--1"></div>
                    <div class="bar bar--2"></div>
                    <div class="bar bar--3"></div>

                </div>
            </div>
        </div>

CSS:

.menu {
        position:   fixed;
        top:        0;
        left:       0;
        width:      102%;
        z-index:    20;
        background: black;
        height:     90px;}
    .container {
        position: relative;
        margin:   0 auto;
        width:    calc(100vw - 200px);
        padding:  0 200px;}

    .logo{
        height:   40px;
        width:    auto;
        position: absolute;
        top:      25px;
        left:     40px;}

    .menu-trigger {
        position:           absolute;
        top:                18.5px;
        right:              255px;
        height:             55px;
        width:              60px;
        cursor:             pointer;
        transition:         opacity 130ms ease-out;
        -webkit-transition: opacity 130ms ease-out;
        -moz-transition:    opacity 130ms ease-out;
        -ms-transition:     opacity 130ms ease-out;}
        .menu-trigger:hover {
            opacity: 1;}
            .menu-trigger h5 {
                position:           absolute;
                right:              10px;
                top:                9px;
                text-transform:     uppercase;
                color:              #fff;
                user-select:        none;
                -webkit-user-select:none;
                -moz-user-select:   none;
                -ms-user-select:    none;
                -o-user-select:     none;
                -khtml-user-select: none;
                transition:         color 300ms ease-out;
                -webkit-transition: color 300ms ease-out;
                -moz-transition:    color 300ms ease-out;
                -ms-transition:     color 300ms ease-out;}


    .menu-trigger .bar {
        position:           absolute;
        left:               10px;
        width:              40px;
        height:             5px;
        background:         #fff;
        transition:         transform 180ms ease-out, opacity 160ms ease-out, top 180ms ease-out, width 120ms ease-out, background 300ms ease-out;
        -webkit-transition: transform 180ms ease-out, opacity 160ms ease-out, top 180ms ease-out, width 120ms ease-out, background 300ms ease-out;
        -moz-transition:    transform 180ms ease-out, opacity 160ms ease-out, top 180ms ease-out, width 120ms ease-out, background 300ms ease-out;
        -ms-transition:     transform 180ms ease-out, opacity 160ms ease-out, top 180ms ease-out, width 120ms ease-out, background 300ms ease-out;}
        .bar--1 {
            top: 15px; }
        .bar--2 {
            top: 25px}
        .bar--3 {
            top: 35px;}

    .open, .open:hover {
        opacity: 1 !important;}
        .open h5 {
            color: #fff;}
            .open .bar {
                background: #fff; }
                .open .bar--1 {
                    top:               21px;
                    transform:         rotate(135deg);
                    -webkit-transform: rotate(135deg);
                    -moz-transform:    rotate(135deg);
                    -ms-transform:     rotate(135deg); }
                .open .bar--2 {
                    opacity: 0;
                    width:   0px; }
                .open .bar--3 {
                    top:                21px;
                    transform:         rotate(-135deg);
                    -webkit-transform: rotate(-135deg);
                    -moz-transform:    rotate(-135deg);
                    -ms-transform:     rotate(-135deg);}

    .navigation {
        visibility:                  hidden;
        position:                    fixed;
        z-index:                     19;
        top:                         20px;
        left:                        0;
        width:                       100%;
        max-width:                   100%;
        background:                  #FFFFFF;
        box-shadow:                  0px 6px 8px rgba(0,0,0,0.13);
        padding:                     100px 0 20px 0;
        opacity:                     0;
        transform-origin:            center top;
        -webkit-transform-origin:    center top;
        -moz-transform-origin:       center top;
        -ms-transform-origin:        center top;
        transform:                   scale(0.9);
        -webkit-transform:           scale(0.9);
        -moz-transform:              scale(0.9);
        -ms-transform:               scale(0.9);
        backface-visibility:         hidden;
        -webkit-backface-visibility: hidden;
        transition:                  opacity 190ms ease-out, transform 40ms ease-out;
        -webkit-transition:          opacity 190ms ease-out, transform 40ms ease-out;
        -moz-transition:             opacity 190ms ease-out, transform 40ms ease-out;
        -ms-transition:              opacity 190ms ease-out, transform 40ms ease-out;}
        .navigation .container {
            padding: 0 18px; }
    .nav-open {
        visibility:         visible;
        opacity:            1;
        transform:         scale(1);
        -webkit-transform: scale(1);
        -moz-transform:    scale(1);
        -ms-transform:     scale(1);}

    nav {
        position: relative;
        width: 100%;
        max-width: 100%;
        margin: 0 auto;
        display: flex;
        align-items: flex-start;
        justify-content: flex-start;
        flex-flow: row wrap;}
        nav h3 {
            font-family:    'Univers';  
            position:       relative;
            display:        block;
            margin:         0 0 15px 0;
            color:          black;
            font-size:      1.2em;
            font-weight:    600;
            text-transform: uppercase;}
            nav ul {
                position:        relative;
                padding:         0 0;
                margin:          0 0;
                width:           100%;
                max-width:       100%;
                list-style-type: none;}
                nav li {
                    display:       block;
                    color:         #919191 !important;
                    font-size:     0.88em;
                    font-family:   'helvetica';
                    margin:         6px 0;
                    font-weight:    400;
                    letter-spacing: 0.025em;}

                    nav li{}
                        nav li > a > i {
                        color: #121212;
                        font-size:         1.4em;
                        margin-right:      8px;
                        display:           inline-block;
                        transform:         translateY(1px);
                        -webkit-transform: translateY(1px);
                        -moz-transform:    translateY(1px);
                        -ms-transform:     translateY(1px);
                        opacity:           0.6; }

            nav .social-link {
                float:         left;
                width:         44px;
                height:        44px;
                line-height:   48px;
                border-radius: 44px;
                text-align:    center;
                margin:        5px;
                cursor:        pointer;
                transition:    all 0.25s ease-in-out;}
                    nav .social-link > i:hover {
                        color: black; }
                    nav .social-link:last-child {
                        margin-right: 0px;  }
                        nav .social-link > i {
                            color:     #B5B5B5;
                            font-size: 1.57em;
                            margin:    0 auto; }

        nav .col {
            min-height:                   auto;
            width:                        auto;
            flex-direction:               row;
            margin:                       0 auto;
            margin-bottom:                25px;
            text-align:                   left;
            transform:                    translateY(25px);
            -webkit-transform:            translateY(25px);
            -moz-transform:               translateY(25px);
            -ms-transform:                translateY(25px);
            opacity:                      0;
            backface-visibility:         hidden;
            -webkit-backface-visibility: hidden;
            will-change:                 transform, opacity; }
            .c-in {
                animation-name:                    fadeInUp;
                -webkit-animation-name:            fadeInUp;
                -moz-animation-name:               fadeInUp;
                -ms-animation-name:                fadeInUp;
                animation-duration:                860ms;
                -webkit-animation-duration:        860ms;
                -moz-animation-duration:           860ms;
                -ms-animation-duration:            860ms;
                animation-fill-mode:               forwards;
                -webkit-animation-fill-mode:       forwards;
                -moz-animation-fill-mode:          forwards;
                -ms-animation-fill-mode:           forwards;
                animation-timing-function:         cubic-bezier(0.190, 1.000, 0.220, 1.000);
                -webkit-animation-timing-function: cubic-bezier(0.190, 1.000, 0.220, 1.000);
                -moz-animation-timing-function:    cubic-bezier(0.190, 1.000, 0.220, 1.000);
                -ms-animation-timing-function:     cubic-bezier(0.190, 1.000, 0.220, 1.000);  }
                .col:first-child {
                    animation-delay:         50ms;
                    -webkit-animation-delay: 50ms;
                    -moz-animation-delay:    50ms;
                    -ms-animation-delay:     50ms;}
                .col:nth-child(2) {
                    animation-delay:         130ms;
                    -webkit-animation-delay: 130ms;
                    -moz-animation-delay:    130ms;
                    -ms-animation-delay:     130ms; }
                .col:nth-child(3) {
                    animation-delay:         210ms;
                    -webkit-animation-delay: 210ms;
                    -moz-animation-delay:    210ms;
                    -ms-animation-delay:     210ms; }
                .col:nth-child(4) {
                    animation-delay:         290ms;
                    -webkit-animation-delay: 290ms;
                    -moz-animation-delay:    290ms;
                    -ms-animation-delay:     290ms; }

                    main {
                        position:  relative;
                        width:     100%;
                        max-width: 100%;
                            margin:    0 auto;  }
                        main .container {
                                padding: 82px 18px 0 18px;  }

                    @media screen and (min-width: 680px) {
                        nav .col {
                            width:      50%;
                            min-height: 136px;}   }
                    @media screen and (min-width: 992px) {
                        nav .col {
                            width:      auto;
                            min-height: 136px;}
                        .search {
                            max-width: 235px; } }
                    @media screen and (max-width: 480px) {
                        .container {
                            position: relative;
                            margin:   0 auto;
                            width:    calc(100vw - 100px);
                            padding:  0 50px;}

                        .menu-trigger{
                            right: 20px;
                            top:15px;}

                        nav .col {
                            width:      100% !important;
                            min-height: 136px; }

                        nav .social-link{
                            margin:1px; }  

                        .logo {
                            height: 30px;
                            width: auto;
                            position: absolute;
                            top: 25px;
                            left: 9px;
                    }

                        }
                    @media screen and (max-width: 1024px) {
                        nav{
                            justify-content: flex-start !important;
                            align-items:     flex-start !important;}

                        nav .social-link{
                            margin: 10px;  }

                        nav .col {
                            min-height: auto !important;
                            text-align: center; }

                        .col:nth-child(5) {
                            display:         flex;
                            justify-content: center !important;
                            flex-direction:  column;   }
                        .search {
                            max-width: 235px; }   }

                    @keyframes fadeInUp {
                        0% {transform: translateY(25px); -webkit-transform: translateY(25px); -moz-transform: translateY(25px); -ms-transform: translateY(25px); opacity: 0;}
                        100% {transform: translateY(0px); -webkit-transform: translateY(0px); -moz-transform: translateY(0px); -ms-transform: translateY(0px); opacity: 1;}  }
                    @-webkit-keyframes fadeInUp {
                        0% {transform: translateY(25px); -webkit-transform: translateY(25px); -moz-transform: translateY(25px); -ms-transform: translateY(25px); opacity: 0;}
                        100% {transform: translateY(0px); -webkit-transform: translateY(0px); -moz-transform: translateY(0px); -ms-transform: translateY(0px); opacity: 1;}  }
                    @-moz-keyframes fadeInUp {
                        0% {transform: translateY(25px); -webkit-transform: translateY(25px); -moz-transform: translateY(25px); -ms-transform: translateY(25px); opacity: 0;}
                        100% {transform: translateY(0px); -webkit-transform: translateY(0px); -moz-transform: translateY(0px); -ms-transform: translateY(0px); opacity: 1;}  }
                    @-ms-@keyframes fadeInUp {

                        0% {transform: translateY(25px); -webkit-transform: translateY(25px); -moz-transform: translateY(25px); -ms-transform: translateY(25px); opacity: 0;}
                        100% {transform: translateY(0px); -webkit-transform: translateY(0px); -moz-transform: translateY(0px); -ms-transform: translateY(0px); opacity: 1;}  }

JS:

$(document).ready(function () {
            function openMenu() {
                $(".menu-trigger").addClass("open");
                $(".navigation").addClass("nav-open");
                $(".col").addClass("c-in");
            }

            function closeMenu() {
                $(".menu-trigger").removeClass("open");
                $(".navigation").removeClass("nav-open");
                $(".col").removeClass("c-in");
            }
            $(".menu-trigger").click(function () {
                if ($(".menu-trigger").hasClass("open")) {
                    closeMenu();
                } else {
                    openMenu();
                }
            });
            $("main").click(function () {
                if ($(".menu-trigger").hasClass("open")) {
                    closeMenu();
                }
            });
            $(document).keyup(function (e) {
                if (e.keyCode == 27) {
                    closeMenu();
                }
            });
        });

1 个答案:

答案 0 :(得分:1)

将此jQuery函数添加到您的js文件中:

getS()

以下是用于理解的代码段:

import { shallow } from 'enzyme' 
import Element from './yourpath'

const wrapper = shallow(<Element />); // mount/render/shallow when applicable

//as shallow renders only one level of children components
expect(wrapper).toContainReact(<FormattedMessage id="some value" />);

//or if you want to test actually the span element you should use mount
const wrapper2 = mount(<Element />);
expect(wrapper).toContainReact(<span> some value </span>);
$("a").click(function () {
  if ($(".menu-trigger").hasClass("open")) {
        closeMenu();
    }
})
$(document).ready(function () {
    function openMenu() {
        $(".menu-trigger").addClass("open");
        $(".navigation").addClass("nav-open");
        $(".col").addClass("c-in");
    }

    function closeMenu() {
        $(".menu-trigger").removeClass("open");
        $(".navigation").removeClass("nav-open");
        $(".col").removeClass("c-in");
    }
    $(".menu-trigger").click(function () {
        if ($(".menu-trigger").hasClass("open")) {
            closeMenu();
        } else {
            openMenu();
        }
    });
    $("main").click(function () {
        if ($(".menu-trigger").hasClass("open")) {
            closeMenu();
        }
    });
    $(document).keyup(function (e) {
        if (e.keyCode == 27) {
            closeMenu();
        }
    });
    $("a").click(function () {
      if ($(".menu-trigger").hasClass("open")) {
            closeMenu();
        }
    })

});

相关问题