2012-06-14 67 views
0

我需要創建像我們一樣的下拉菜單。根據下拉菜單使用jQuery

enter image description here

在suppage,幻燈片必須是開放的。

例如,如果我點擊服務2,在服務2頁面幻燈片將會打開。服務2將作爲選擇的菜單。

我附上我的代碼。

我需要在子頁面中打開子菜單。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<link rel="stylesheet" type="text/css" href="droplinetabs.css" /> 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 

<script src="droplinemenu.js" type="text/javascript"></script> 

<script type="text/javascript"> 

droplinemenu.buildmenu("droplinetabs1") 

</script> 
</head> 

<body> 
<div id="droplinetabs1" class="droplinetabs"> 
<ul> 
<li><a href="#"><span>Home</span></a></li> 
<li><a href="#"><span>About</span></a> 
    <ul> 
    <li><a href="#">About 1</a></li> 
    <li><a href="#">About 2</a></li> 
    <li><a href="#">About 3</a></li> 
    <li><a href="#">About 4</a></li> 
    </ul> 
</li> 
<li><a href="#"><span>Services</span></a> 
    <ul> 
    <li><a href="#">Services 1</a></li> 
    <li><a href="#">Services 2</a></li> 
    <li><a href="#">Services 3</a></li> 
    <li><a href="#">Services 4</a></li> 
    <li><a href="#">Services 5</a></li> 
    </ul> 
</li> 
<li><a href="#"><span>Gallery</span></a></li> 
</ul> 
</div> 
</body> 
</html> 

CSS文件

.droplinetabs{ 
    overflow: hidden; 
    border-bottom: 1px solid gray; /*underline across bottom of main tabs*/ 
} 

.droplinetabs ul{ 
    font: bold 11px Verdana, sans-serif; 
    margin: 0; 
    padding: 0; 
    width: 100%; 
    list-style: none; 
} 

.droplinetabs li{ 
    display: inline; 
    margin: 0 2px 0 0; 
    padding: 0; 
    text-transform: uppercase; 
} 


.droplinetabs a{ 
    float: left; 
    color: white; 
    background: #c76023 url(leftedge.gif) no-repeat left top; /*default background color of tabs, left corner image*/ 
    margin: 0 4px 0 0; 
    padding: 0 0 4px 3px; 
    text-decoration: none; 
    letter-spacing: 1px; 
} 

.droplinetabs a:link, .droplinetabs a::visited, .droplinetabs a:active{ 
    color: white; 
} 

.droplinetabs a span{ 
    float: left; 
    display: block; 
    background: transparent url(rightedge.gif) no-repeat right top; /*right corner image*/ 
    padding: 7px 9px 3px 6px; 
    cursor: pointer; 
} 

.droplinetabs a span{ 
    float: none; 
} 


.droplinetabs a:hover{ 
    background-color: #b05016; /*background color of tabs onMouseover*/ 
    color: white; 
} 

.droplinetabs a:hover span{ 
    background-color: transparent; 
} 

/* Sub level menus*/ 
.droplinetabs ul li ul{ 
    position: absolute; 
    z-index: 100; 
    left: 0; 
    top: 0; 
    background: #c76023; /*sub menu background color */ 
    visibility: hidden; 
} 

/* Sub level menu links style */ 
.droplinetabs ul li ul li a{ 
    font: normal 13px Verdana; 
    padding: 6px; 
    padding-right: 8px; 
    margin: 0; 
    background: #c76023; /*sub menu background color */ 
} 

.droplinetabs ul li ul li a span{ 
    background: #c76023; /*sub menu background color */ 
} 

.droplinetabs ul li ul li a:hover{ /*sub menu links' background color onMouseover. Add rounded edges in capable browsers */ 
    background: #714421; 
    -webkit-border-radius: 5px; 
    -moz-border-radius: 5px; 
    -khtml-border-radius: 5px; 
    border-radius: 5px; 
} 

JQUERY FILE

var droplinemenu={ 

arrowimage: {classname: 'downarrowclass', src: 'down.gif', leftpadding: 5}, //customize down arrow image 
animateduration: {over: 200, out: 100}, //duration of slide in/ out animation, in milliseconds 

buildmenu:function(menuid){ 
    jQuery(document).ready(function($){ 
     var $mainmenu=$("#"+menuid+">ul") 
     var $headers=$mainmenu.find("ul").parent() 
     $headers.each(function(i){ 
      var $curobj=$(this) 
      var $subul=$(this).find('ul:eq(0)') 
      this._dimensions={h:$curobj.find('a:eq(0)').outerHeight()} 
      this.istopheader=$curobj.parents("ul").length==1? true : false 
      if (!this.istopheader) 
       $subul.css({left:0, top:this._dimensions.h}) 
      var $innerheader=$curobj.children('a').eq(0) 
      $innerheader=($innerheader.children().eq(0).is('span'))? $innerheader.children().eq(0) : $innerheader //if header contains inner SPAN, use that 
      $innerheader.append(
       '<img src="'+ droplinemenu.arrowimage.src 
       +'" class="' + droplinemenu.arrowimage.classname 
       + '" style="border:0; padding-left: '+droplinemenu.arrowimage.leftpadding+'px" />' 
      ) 

      $curobj.hover(
       function(e){ 
        var $targetul=$(this).children("ul:eq(0)") 
        if ($targetul.queue().length<=1) //if 1 or less queued animations 
         if (this.istopheader) 
          $targetul.css({left: $mainmenu.position().left, top: $mainmenu.position().top+this._dimensions.h}) 
         if (document.all && !window.XMLHttpRequest) //detect IE6 or less, fix issue with overflow 
          $mainmenu.find('ul').css({overflow: (this.istopheader)? 'hidden' : 'visible'}) 
         $targetul.dequeue().slideDown(droplinemenu.animateduration.over) 
       }, 
       function(e){ 
        var $targetul=$(this).children("ul:eq(0)") 
        $targetul.dequeue().slideUp(droplinemenu.animateduration.out) 
       } 
      ) //end hover 
     }) //end $headers.each() 
     $mainmenu.find("ul").css({display:'none', visibility:'visible', width:$mainmenu.width()}) 
    }) //end document.ready 
} 
} 
+1

你的問題是什麼?什麼不行? – Thomas

+0

我需要在子頁面中打開子菜單。 (這是滑出將打開) – Velmurugan

+1

我建議這是封閉的「太寬」。評論表明文字不清楚。下面的答案下的評論變成了免費代碼的請求。 – halfer

回答

2

我看看你的代碼。我不得不說它有點笨拙,因爲太多的樣式很難直接寫入JavaScript。理想情況下,你會想要分開儘可能多的樣式和使用類。下拉菜單的絕對位置並不是真正必要的,並且會使解決方案變得更加複雜。

如果您希望其中一個下拉菜單自動出現,您可以在該UL上添加一個on類。所以說你在服務頁面上。菜單是這個,而不是揭示服務選項:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<link rel="stylesheet" type="text/css" href="droplinetabs.css" /> 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 

<script src="droplinemenu.js" type="text/javascript"></script> 

<script type="text/javascript"> 

droplinemenu.buildmenu("droplinetabs1") 

</script> 
</head> 

<body> 
<div id="droplinetabs1" class="droplinetabs"> 
<ul> 
<li><a href="#"><span>Home</span></a></li> 
<li><a href="#"><span>About</span></a> 
    <ul> 
    <li><a href="#">About 1</a></li> 
    <li><a href="#">About 2</a></li> 
    <li><a href="#">About 3</a></li> 
    <li><a href="#">About 4</a></li> 
    </ul> 
</li> 
<li><a href="#"><span>Services</span></a> 
    <ul class="on"> 
    <li><a href="#">Services 1</a></li> 
    <li><a href="#">Services 2</a></li> 
    <li><a href="#">Services 3</a></li> 
    <li><a href="#">Services 4</a></li> 
    <li><a href="#">Services 5</a></li> 
    </ul> 
</li> 
<li><a href="#"><span>Gallery</span></a></li> 
</ul> 
</div> 
</body> 
</html> 

然後在你的JS後,所有的藏身已經發生了,你可以遍歷上行線路,以檢查它是否具有「對」級,它會動畫馬上。

所以$ mainmenu.find(「UL」)......部分後添加以下部分

var droplinemenu={ 

arrowimage: {classname: 'downarrowclass', src: 'down.gif', leftpadding: 5}, //customize down arrow image 
animateduration: {over: 200, out: 100}, //duration of slide in/ out animation, in milliseconds 

buildmenu:function(menuid){ 
    jQuery(document).ready(function($){ 
     var $mainmenu=$("#"+menuid+">ul") 
     var $headers=$mainmenu.find("ul").parent() 
     $headers.each(function(i){ 
      var $curobj=$(this) 
      var $subul=$(this).find('ul:eq(0)') 
      this._dimensions={h:$curobj.find('a:eq(0)').outerHeight()} 
      this.istopheader=$curobj.parents("ul").length==1? true : false 
      if (!this.istopheader) 
       $subul.css({left:0, top:this._dimensions.h}) 
      var $innerheader=$curobj.children('a').eq(0) 
      $innerheader=($innerheader.children().eq(0).is('span'))? $innerheader.children().eq(0) : $innerheader //if header contains inner SPAN, use that 
      $innerheader.append(
       '<img src="'+ droplinemenu.arrowimage.src 
       +'" class="' + droplinemenu.arrowimage.classname 
       + '" style="border:0; padding-left: '+droplinemenu.arrowimage.leftpadding+'px" />' 
      ) 

      $curobj.hover(
       function(e){ 
        var $targetul=$(this).children("ul:eq(0)") 
        if ($targetul.queue().length<=1) //if 1 or less queued animations 
         if (this.istopheader){ 
          $targetul.css({left: $mainmenu.position().left, top: $mainmenu.position().top+this._dimensions.h}) 
         } 
         if (document.all && !window.XMLHttpRequest) //detect IE6 or less, fix issue with overflow 
          $mainmenu.find('ul').css({overflow: (this.istopheader)? 'hidden' : 'visible'}) 
         $targetul.dequeue().slideDown(droplinemenu.animateduration.over) 
       }, 
       function(e){ 
        var $targetul=$(this).children("ul:eq(0)") 
        $targetul.dequeue().slideUp(droplinemenu.animateduration.out) 
       } 
      ) //end hover 
     }) //end $headers.each() 

     $mainmenu.find("ul").css({display:'none', visibility:'visible', width:$mainmenu.width()}) 
     $($mainmenu.find("ul")).each(function(index) { 
      if($(this).hasClass('on')){ 
       $(this).css({left:$mainmenu.position().left,top:$mainmenu.position().top+$(this).height()}) 
       $(this).slideDown(droplinemenu.animateduration.over) 
      } 
     }); 
    }) //end document.ready 
} 
} 

它並不像你應該能夠簡單地做一個「顯示器的理想解決方案:阻止「你的UL類,但因爲在手風琴JS中進行了如此多的風格計算,所以不能這麼容易地完成。 這個「黑客」應該爲你工作,但我希望它。

我會建議一個更優雅的jQuery手風琴解決方案,網上有很多東西要看。它看起來像你可能一直在尋找你正在使用的jQuery版本的一個相對較舊的例子。

+0

對不起,我不知道如何實現此代碼。請幫幫我。非常緊急 – Velmurugan

+1

我已經在答案中複製了這兩個文件。這對我有用。我能做的事情不多。就像我說的,理想情況下,您會想要使用更易於理解的不同手風琴菜單插件。你所擁有的太過集成到造型/ html中。這很可能是一個定製的,很難理解。祝你好運。 –