2017-03-03 50 views
-1

我做出了一個響應式菜單,如果您使用移動設備,所有工作都會有所期待。 我想漢堡包圖標滾動菜單鏈接,現在如果你滾動它只是停留在頂部,需要給漢堡包一個位置固定點擊,如果再次點擊返回到位置絕對我怎樣才能改變父母的類點擊複選框和回來?

我試着用javascript和青菜,如果它可以只用純CSS可以做到,那麼它的prerfect否則一些JavaScript將被罰款

圖標ID是#menutoggle < =需要作出這個位置:固定在複選框點擊

#header { 
 
    transition: all 300 ms ease; 
 
    font-size: 0.9em; 
 
    font-weight: 300; 
 
    background: black; 
 
    margin: 0 auto; 
 
    padding: 2em 0 1em; 
 
    border-bottom: 4px solid red; 
 
} 
 
@media only screen and (min-width: 768px) and (max-width: 1023px) { 
 
    #header { 
 
    padding: 1em 0; 
 
    } 
 
} 
 
@media only screen and (max-width: 767px) { 
 
    #header { 
 
    height: 4em; 
 
    padding: 0; 
 
    } 
 
} 
 
#header a { 
 
    color: white; 
 
} 
 
#header .header-inner { 
 
    max-width: 1024px; 
 
    margin: 0 auto; 
 
    position: relative; 
 
    height: 4em; 
 
} 
 
@media only screen and (min-width: 768px) and (max-width: 1023px) { 
 
    #header .header-inner { 
 
    max-width: 768px; 
 
    height: 3em; 
 
    } 
 
} 
 
@media only screen and (max-width: 767px) { 
 
    #header .header-inner { 
 
    height: 4em; 
 
    padding: 0 1em 1em; 
 
    } 
 
} 
 
#header .navbar { 
 
    top: 0; 
 
    height: 100%; 
 
} 
 
@media only screen and (max-width: 767px) { 
 
    #header .navbar { 
 
    left: 0; 
 
    height: 100%; 
 
    width: 100%; 
 
    position: absolute; 
 
    } 
 
} 
 
#header .navbar .leaf { 
 
    transition: all 300ms ease; 
 
    border-radius: 20px; 
 
} 
 
#header .navbar .leaf:hover { 
 
    background: red; 
 
} 
 
#header .navbar .logo { 
 
    margin: 1em 0; 
 
    position: absolute; 
 
    right: 360px; 
 
    text-align: center; 
 
    top: -38px; 
 
    z-index: 5; 
 
} 
 
@media only screen and (max-width: 767px) { 
 
    #header .navbar .logo { 
 
    position: absolute; 
 
    transform: translate(50%, 0px); 
 
    margin: 0 auto; 
 
    width: 60px; 
 
    height: 50px; 
 
    right: 50%; 
 
    top: 5px; 
 
    background: url("../images/crown.jpg.svg"); 
 
    background-repeat: no-repeat; 
 
    background-size: contain; 
 
    } 
 
} 
 
@media only screen and (min-width: 768px) and (max-width: 1023px) { 
 
    #header .navbar .logo { 
 
    margin: 0.5em 0; 
 
    right: 280px; 
 
    top: -20px; 
 
    } 
 
} 
 
#header .navbar .logo img { 
 
    background-position: contain; 
 
    width: 300px; 
 
} 
 
@media only screen and (max-width: 767px) { 
 
    #header .navbar .logo img { 
 
    width: 135px; 
 
    height: 50px; 
 
    opacity: 0; 
 
    } 
 
} 
 
@media only screen and (min-width: 768px) and (max-width: 1023px) { 
 
    #header .navbar .logo img { 
 
    width: 225px; 
 
    } 
 
} 
 
#header .navbar .menu { 
 
    text-align: center; 
 
    margin-top: 2em; 
 
} 
 
@media only screen and (max-width: 767px) { 
 
    #header .navbar .menu { 
 
    display: none; 
 
    } 
 
} 
 
#header .navbar .menu ul { 
 
    list-style-type: none; 
 
} 
 
#header .navbar .menu ul .video { 
 
    margin-right: 345px; 
 
} 
 
@media only screen and (min-width: 768px) and (max-width: 1023px) { 
 
    #header .navbar .menu ul .video { 
 
    margin-right: 260px; 
 
    } 
 
} 
 
#header .navbar .menu li { 
 
    display: inline; 
 
    padding: 10px 1.2em; 
 
    margin-right: 40px; 
 
} 
 
@media only screen and (min-width: 1024px) { 
 
    #header .navbar .menu li { 
 
    padding: 10px 1.2em; 
 
    margin-right: 40px; 
 
    } 
 
} 
 
#header .navbar .menu li:last-child { 
 
    margin-right: 0px; 
 
} 
 
#header .navbar nav { 
 
    display: none; 
 
    transition: all 300ms ease; 
 
    width: 100%; 
 
    /* And let's fade it in from the left */ 
 
} 
 
@media only screen and (max-width: 767px) { 
 
    #header .navbar nav { 
 
    display: block; 
 
    } 
 
} 
 
#header .navbar nav a { 
 
    text-decoration: none; 
 
    color: white; 
 
    transition: color 0.3s ease; 
 
} 
 
#header .navbar nav a:hover { 
 
    color: red; 
 
} 
 
#header .navbar nav #menuToggle { 
 
    display: block; 
 
    position: relative; 
 
    z-index: 1; 
 
    -webkit-user-select: none; 
 
    user-select: none; 
 
    top: 16px; 
 
} 
 
#header .navbar nav #menuToggle input { 
 
    display: block; 
 
    width: 55px; 
 
    height: 32px; 
 
    position: absolute; 
 
    top: 0px; 
 
    left: -5px; 
 
    cursor: pointer; 
 
    opacity: 0; 
 
    /* hide this */ 
 
    z-index: 12; 
 
    /* and place it over the hamburger */ 
 
    -webkit-touch-callout: none; 
 
} 
 
#header .navbar nav #menuToggle ul a { 
 
    width: 100; 
 
    display: block; 
 
    border-bottom: 1px solid rgba(215, 7, 120, 0.2); 
 
} 
 
#header .navbar nav #menuToggle span { 
 
    display: block; 
 
    width: 33px; 
 
    height: 4px; 
 
    margin-left: 10px; 
 
    margin-bottom: 5px; 
 
    position: relative; 
 
    background: #cdcdcd; 
 
    border-radius: 3px; 
 
    z-index: 3; 
 
    transform-origin: 4px 0px; 
 
    transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1), opacity 0.55s ease; 
 
} 
 
#header .navbar nav #menuToggle span:first-child { 
 
    transform-origin: 0% 0%; 
 
    margin-left: 10px; 
 
} 
 
#header .navbar nav #menuToggle span:nth-last-child(2) { 
 
    transform-origin: 0% 100%; 
 
    margin-left: 10px; 
 
} 
 
#header .navbar nav #menuToggle input:checked ~ span { 
 
    opacity: 1; 
 
    margin-left: 10px; 
 
    transform: rotate(45deg) translate(-2px, -1px); 
 
    background: red; 
 
} 
 
#header .navbar nav #menuToggle input:checked ~ span:nth-last-child(3) { 
 
    margin-left: 10px; 
 
    opacity: 0; 
 
    transform: rotate(0deg) scale(0.2, 0.2); 
 
} 
 
#header .navbar nav #menuToggle input:checked ~ span:nth-last-child(2) { 
 
    margin-left: 10px; 
 
    opacity: 1; 
 
    transform: rotate(-45deg) translate(0, -1px); 
 
} 
 
#header .navbar nav #menu { 
 
    position: absolute; 
 
    width: 100%; 
 
    padding: 50px; 
 
    height: 100vh; 
 
    top: 0; 
 
    position: fixed; 
 
    background: black; 
 
    list-style-type: none; 
 
    -webkit-font-smoothing: antialiased; 
 
    /* to stop flickering of text in safari */ 
 
    transform-origin: 0% 0%; 
 
    transform: translate(-100%, 0); 
 
    transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1); 
 
} 
 
#header .navbar nav #menu li { 
 
    padding: 10px 0; 
 
    font-size: 22px; 
 
} 
 
#header .navbar nav #menuToggle input:checked ~ ul { 
 
    transform: scale(1, 1); 
 
    opacity: 1; 
 
    width: 100%; 
 
    top: 0; 
 
} 
 

 
.blok { 
 
    width: 100%; 
 
    height: 1000px; 
 
    background: blue; 
 
}
<body> 
 
    <section id="home"> 
 
    <div id="header"> 
 
     <div class="header-inner"> 
 
     <div class="navbar"> 
 
      <div class="logo"> 
 
      <img src="" /> 
 
      </div> 
 
      <div class="menu"> 
 
      <ul class="menu"> 
 
       <li class="leaf"><a title="" href="/photo">Home</a></li> 
 
       <li class="leaf video"><a title="" href="/video">About</a></li> 
 
       <li class="leaf"><a title="" href="/music">Menu</a></li> 
 
       <li class="leaf"><a title="" href="/forum">Contact</a></li> 
 
      </ul> 
 
      </div> 
 
      <nav role="navigation"> 
 
      <div id="menuToggle"> 
 
       <input type="checkbox" /> 
 
       <span></span> 
 
       <span></span> 
 
       <span></span> 
 
       <ul id="menu"> 
 
       <a href="#"> 
 
        <li>Home</li> 
 
       </a> 
 
       <hr class="line-menu"> 
 
       <a href="#"> 
 
        <li>About</li> 
 
       </a> 
 
       <hr class="line-menu"> 
 
       <a href="#"> 
 
        <li>Info</li> 
 
       </a> 
 
       <hr class="line-menu"> 
 
       <a href="#"> 
 
        <li>Contact</li> 
 
       </a> 
 
       </ul> 
 
      </div> 
 
      </nav> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    <!--end header--> 
 
    <!--begin header--> 
 
    <div class="blok"></div>

+1

爲什麼你不能在這裏添加代碼? –

+0

我不明白?添加鏈接本身如何 –

+0

請在問題中添加您的代碼。規則是有原因的;鏈接腐爛可能意味着這個問題將對未來的訪問者無用。顛覆規則只會讓人們更難以幫助你,這意味着你不會得到任何幫助,也不會得到更多的讚譽。 –

回答

1

添加該代碼

@media only screen and(max-width: 767px) { 
    #header { 
     position: fixed; 
     top: 0; 
     width: 100% ; 
    } 
} 
+1

http://codepen.io/hunzaboy/pen/VpaGbQ – Aslam

+0

它看起來很簡單,但它不工作 –

+0

@amirhanif,?檢查代碼筆,使屏幕變小。 – Aslam

0

JQ:

$(document).on('change', '.checkbox', function() { 
    $('#menutoggle').toggleClass('position-style'); 
}); 

CSS:

.position-style { 
    position: fixed; 
} 

希望這有助於你]

//編輯https://jsfiddle.net/zfo5nzf3/ ŧ他的代碼正在工作,根據你的「圖標ID是#menutoggle < =需要使這個位置:固定在點擊複選框上」添加'.position'類(其中規則是position:fixed;)到#menutoggle。

+0

我試過這是行不通的 –

+0

你已經安裝了jquery librares嗎? – Paul

相關問題