2016-10-10 175 views
-2

我一直在關注如何使用CSS創建下拉菜單的在線視頻。我跟着它,我的網站上沒有任何下拉菜單的跡象。這太令人沮喪了,因爲我想要解決它,所以我現在可以專注於後端開發。希望你們可以弄清楚這一點。CSS下拉菜單不工作?

代碼的html:

<!DOCTYPE> 
    <html lang="en"> 
<head> 
<title>MUSIC STORE</title> 

<script src="js/jquery-1.11.2.min.js"></script> 


<link rel="stylesheet" href="jquery.bxslider.css"/> 
<link rel="stylesheet" href="styles.css"/> 
<link rel="shortcut icon" type="image/png" href="../Music Store/img/rockSign.png"/> 
</head> 

<body> 
    <div id="wrapper"> 
     <header id="main_header"> 
      <div id="callout"> 
       <h2>&#9742; 111222333</h2> 
       <p>Michigan State Kawasaki Iceland</p> 
      </div> 
      <h1>MUSIC STORE</h1> 
     </header> 

     <div class="clearfix"></div> 

     <nav id="nav_menu"> 
      <ul id="nav"> 
       <li><a href="#">HOME</a></li> 
       <li><a href="#">INSTRUMENTS</a> 
        <ul class="sub-menu"> 
         <li><a href="#">ELECTRIC GUITARS</a></li> 
         <li><a href="#">BASS GUITARS</a></li> 
         <li><a href="#">DRUMS</a></li> 
        </ul> 
       </li> 
       <li><a href="#">AMPLIFIERS</a></li> 
       <li><a href="#">ACCESSORIES</a></li> 
       <li><a href="#">FEATURED ARTISTS</a></li> 
      </ul> 
     </nav> 

     <script src="js/jquery.bxslider.min.js"></script><!--For Image Slider--> 
     <div class="slide-wrap"> 
      <div class="slider"> 
       <ul class="slider1"> 
        <li><img src="../Music Store/img/ibanez.jpg"/></li> 
        <li><img src="../Music Store/img/raven.jpg"/></li> 
        <li><img src="../Music Store/img/metallica.jpg"/></li> 
       </ul> 
      </div> 
     </div> 
     <script type="text/javascript"> 
     $('.slider1').bxSlider({ 
      mode: 'fade', 
      captions: false, 
      auto:true, 
      pager:false, 

      }); 
     $('.slider2').bxSlider({ 
      pager:false, 
      captions: true, 
      maxSlides: 3, 
      minSlides: 1, 
      slideWidth: 230, 
      slideMargin: 10 
      }); 
     $('.slider3').bxSlider({ 
      mode: 'fade', 
      captions: false, 
      auto:true, 
      pager:false, 
      controls:false, 
      }); 
     </script>  


     <section class="one-third"> 
      <div class="border_section"> 
       <h3>NEW BASS AMPS THIS YEAR</h3> 
       <img src="../Music Store/img/fender_amps_bassbreaker.jpg"/> 
       <p>We would like to proudly announce the new shipment of fender bass amps that you all have been 
       waiting for. It will provide you that rich rock and roll tone like no other. Please check 
       out our bass amp section for more details.</p> 
      </div> 

     </section> 

     <section class="one-third"> 
      <div class="border_section"> 
       <h3>NEW FEATURE ARTIST</h3> 
       <img src="../Music Store/img/feature_markHolcomb.jpg"/> 
       <p>Behold Mark Holcomb from Periphery is in the house! Check out his info and new signature guitar 
       at our feature artists section. He will also be having a 20-minute guitar clinic on Oct 8 2016 right 
       here at Music Store.</p> 
      </div> 
     </section> 

     <section class="one-third"> 
      <div class="border_section"> 
       <h3>ATTENTION VOCALISTS!</h3> 
       <img src="../Music Store/img/GoMic.jpg"/> 
       <p>Check out the new Samson Go Mic Connect. It has a top-notch noise cancellation feature that can 
       definitely minimize the annoying sound that your dog makes while your recording. For more details, 
       Go to Accessories section.</p> 
      </div> 
     </section> 

     <div class="clearfix"></div> 

     <footer> 
      <p>&copy;All Rights Reserved</p> 
     </footer> 
    </div> 
    </body> 
</html> 

代碼CSS:

@import url(http://fonts.googleapis.com/css?family=Black+Ops+One:400,700); /*--- Header --*/ 
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,700); /*---  Navigation --*/ 

* 
{ 
margin: 0; 
border: 0; 
padding: 0; 
} 

body 
{ 
background-image: url('../Music Store/img/background.png'); 
background-repeat: repeat-x; 

} 

.clearfix 
{ 
clear:both; 
} 

#wrapper 
{ 
margin: 0 auto; 
max-width: 1120px; 
overflow: auto;  
border: 1px solid black; 
} 

#main_header 
{ 
width: 100%; 
font-family: 'Black Ops One', sans-serif; 
background-color: black; 
border: 1px solid black; 
color: white; 
} 

#main_header h1 
{ 
float: left; 
font-size: 380%; 
margin: -10% 0 0 2%; 

} 

#callout 
{ 
margin: 50px 20px 0 0; 
} 

#callout h2{ 
text-align: right; 
color: white; 
} 

#callout p{ 
text-align: right; 
padding: 0%; 
color: grey; 
font-size: 20px; 
margin-bottom: 3px; 
} 

#nav_menu 
{ 

width: 100%; 
height: 10%; 
background-color: white; 
} 

#nav_menu li 
{ 
display: inline-block; 
margin: 20px 20px 20px 63px; 
font-family: 'Open Sans', sans-serif; 
font-size: 20px; 
font-weight: bold; 
} 

#nav_menu li a 
{ 
text-decoration: none; 
color: black; 
} 

#nav_menu li a:hover 
{ 
color: grey; 
} 

.sub-menu 
{ 
position: absolute; 
background-color: black; 
list-style-type: none; 
width: 124px; 
padding-left: 0px; 
margin-left: -25px; 
padding-top: 5px; 
opacity: 0; 
} 

.sub-menu li 
{ 
padding-left: 25px; 
padding-top: 5px; 
padding-bottom: 5px; 
} 

#nav_menu li:hover .submenu 
{ 
opacity: 1; 
} 

.sub-menu li:hover 
{ 
color: green; 
background-color: yellow; 
} 

/*--- Start Image Slider --*/ 
.slider{ 
max-width: 1120px; 
box-shadow: 1% 2% 5% 0 rgba(0, 0, 0, 0.07); 
} 

.slider1 img{ 
width: 100%; 
margin: 0 auto; 
} 

.slider .bx-wrapper .bx-controls-direction a{ 
outline: 0 none; 
position: absolute; 
text-indent: -9999px; 
top: 40%; 
height: 71px; 
width: 40px; 
z-index: -1; 
transition: all 0.7s; 
} 

.slider .bx-wrapper:hover .bx-controls-direction a{ 
z-index: 5; 
} 

.slider .bx-wrapper .bx-prev{ 
background: url("../Music Store/img/arrow_left.png") no-repeat 7px 9px; 
left: 0px; 
} 

.slider .bx-wrapper .bx-prev:hover{ 
background: url("../Music Store/img/arrow_left.png") no-repeat 8px 15px; 
} 

.slider .bx-wrapper .bx-next{ 
background: url("../Music Store/img/arrow_right.png") no-repeat 10px 12px; 
right: 0px; 
} 

.slider .bx-wrapper .bx-next:hover{ 
background: url("../Music Store/img/arrow_right.png") no-repeat 10px 17px; 
} 

/*--- End Image Slider --*/ 

.one-third img{ 
text-align: center; 
max-width: 100%; 
height: auto; 
opacity: 0.9; 
} 

.border_section p{ 
font-family: 'Lato', sans-serif; 
padding: 2%; 
color: white; 
text-align: justify; 
} 

.border_section h3 
{ 
font-family: 'Open Sans', sans-serif; 
text-align: center; 
color: white; 
text-transform: uppercase; 
letter-spacing: 1%; 
} 

.border_section 
{ 
border: 1px solid black; 
background-color: black; 
} 


.one-third{ 
width: 27.35%; 
float: left; 
margin: 2% 0 3% 4.5%; 
text-align: center; 
background-color: white; 
} 

footer{ 
font-family: 'Open Sans', sans-serif; 
font-weight: bold; 
text-align: center; 
width: 100%; 
height: 6%; 
background-color: black; 
overflow: auto; 
} 

footer p 
{ 
margin-top: 1%; 
color: white; 
} 
+0

認真嗎? -3?大聲笑!在這裏得到了許多來自css怪胎的仇恨。 – BrunoEarth

回答

1

添加到您的CSS: 它會幫助你有你想要的結果。當然,對於您的偏好還有一些改變。

/* Without this line, the submenu elements are black on black background */ 
#nav_menu .sub-menu li a { 
    color: #fff; 
} 
/* With this line you will remove the opacity:0; of the submenu when you hover the parent li */ 
#nav_menu li:hover .sub-menu { 
    opacity: 1; 
} 
/* Don't set a width so you have a better output */ 
#nav_menu li .sub-menu { 
    width: auto; 
} 
/* This lines make the submenu li dislay verticaly and not inline */ 
#nav_menu li .sub-menu li { 
    display: block; 
} 

編輯:

而是改變opacity屬性來顯示/隱藏子菜單中,你應該使用display屬性。 目前,子菜單只是透明的,但始終打開。如果菜單的高度較大,可以將鼠標懸停在打開位置的滑塊上打開。 通過使用display屬性,您實際上隱藏了它,只有當您懸停菜單元素時(它應該是),它纔會打開。

要做到這一點,你必須更換opacity: 0;.sub-menu類:display: none;

然後更改代碼opacity: 1;#nav_menu li:hover .sub-menu是:display: block;(代碼我之前給你)。

它會比用opacity處理它更乾淨。

+0

這就行了。最後! – BrunoEarth

+0

還有一個問題,下拉菜單就在我的圖像滑塊後面。我可以添加哪些代碼以便下拉菜單位於圖像滑塊的頂部? – BrunoEarth

+0

我不能完全告訴你,因爲我看不到你的網站的工作示例,但我猜JavaScript滑塊正在幻燈片中某處添加一些「z-index」。您應該嘗試在'.sub-menu'中添加一個'z-index'。 另一點,你應該處理下拉菜單不同,我會解釋它編輯答案。 – Cladiuss