2012-10-19 39 views
0

我使用下面的js顯示我的網站上的菜單,它工作正常,除了當瀏覽器窗口很小時,下拉菜單顯示向上。我怎麼能讓這個JS這樣,無論窗口大小,下拉菜單顯示向下?當瀏覽器窗口大小很小時,菜單向上顯示

我試着讓y = 0在代碼的最後,但沒有運氣。

var flexdropdownmenu = { 
    arrowpath: '../Uploadedfiles/System/Scripts/flexdropdown/arrow.gif', //full URL or path to arrow image 
    animspeed: 200, //reveal animation speed (in milliseconds) 
    showhidedelay: [150, 150], //delay before menu appears and disappears when mouse rolls over it, in milliseconds 

    //***** NO NEED TO EDIT BEYOND HERE 
    startzindex: 1000, 
    ismobile: navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i) != null, //boolean check for popular mobile browsers 
    builtflexmenuids: [], //ids of flex menus already built (to prevent repeated building of same flex menu) 

    positionul: function ($, $ul, e, $anchor) { 
     var istoplevel = $ul.hasClass('jqflexmenu') //Bool indicating whether $ul is top level flex menu DIV 
     var docrightedge = $(document).scrollLeft() + $(window).width() - 40 //40 is to account for shadows in FF 
     var docbottomedge = $(document).scrollTop() + $(window).height() - 40 
     if (istoplevel) { //if main flex menu DIV 
      var offsets = $anchor.offset() 
      var anchorsetting = $anchor.data('setting') 
      var x = offsets.left + anchorsetting.useroffsets[0] + (anchorsetting.dir == "h" ? $anchor.outerWidth() : 0) //x pos of main flex menu UL 
      var y = offsets.top + anchorsetting.useroffsets[1] + (anchorsetting.dir == "h" ? 0 : $anchor.outerHeight()) 
      x = (x + $ul.data('dimensions').w > docrightedge) ? x - (anchorsetting.useroffsets[0] * 2) - $ul.data('dimensions').w + $anchor.outerWidth() + (anchorsetting.dir == "h" ? -($anchor.outerWidth() * 2) : 0) : x //if not enough horizontal room to the ridge of the cursor 
      y = (y + $ul.data('dimensions').h > docbottomedge) ? y - (anchorsetting.useroffsets[1] * 2) - $ul.data('dimensions').h - $anchor.outerHeight() + (anchorsetting.dir == "h" ? ($anchor.outerHeight() * 2) : 0) : y 
      y = y - 5; 
     } 
     else { //if sub level flex menu UL 
      var $parentli = $ul.data('$parentliref') 
      var parentlioffset = $parentli.offset() 
      var x = $ul.data('dimensions').parentliw //x pos of sub UL 
      var y = 0 
      x = (parentlioffset.left + x + $ul.data('dimensions').w > docrightedge) ? x - $ul.data('dimensions').parentliw - $ul.data('dimensions').w : x //if not enough horizontal room to the ridge parent LI 
      y = (parentlioffset.top + $ul.data('dimensions').h > docbottomedge) ? y - $ul.data('dimensions').h + $ul.data('dimensions').parentlih : y 
      //To show the submenu downwards irrespective of the browser resolution or size 
      y = 0; 
     } 

     $ul.css({ left: x, top: y }) 
    } 
+0

難道你需要在'if'塊還有'else'塊的變化?但爲什麼你想要強制菜單下降到屏幕底部? (用戶在看不到它時會怎樣點擊它?)您能否在http://jsfiddle.net上提供演示? – nnnnnn

+0

@nnnnnn即使我把y = 0放在if條件中,結果也是一樣的。問題是隻有第一層是向上的,另一層是向下的。 – Anuya

回答

0
if (istoplevel){ //if main flex menu DIV 
     var offsets=$anchor.offset() 
     var anchorsetting=$anchor.data('setting') 
     var x=offsets.left+anchorsetting.useroffsets[0]+(anchorsetting.dir=="h"? $anchor.outerWidth() : 0) //x pos of main flex menu UL 
     var y=offsets.top+anchorsetting.useroffsets[1]+(anchorsetting.dir=="h"? 0 : $anchor.outerHeight()) 
     x=(x+$ul.data('dimensions').w > docrightedge)? x-(anchorsetting.useroffsets[0]*2)-$ul.data('dimensions').w+$anchor.outerWidth()+(anchorsetting.dir=="h"? -($anchor.outerWidth()*2) : 0) : x //if not enough horizontal room to the ridge of the cursor 
     //y=(y+$ul.data('dimensions').h > docbottomedge)? y-(anchorsetting.useroffsets[1]*2)-$ul.data('dimensions').h-$anchor.outerHeight()+(anchorsetting.dir=="h"? ($anchor.outerHeight()*2) : 0) : y 
    } 
    else{ //if sub level flex menu UL 
     var $parentli=$ul.data('$parentliref') 
     var parentlioffset=$parentli.offset() 
     var x=$ul.data('dimensions').parentliw //x pos of sub UL 
     var y=0 
     x=(parentlioffset.left+x+$ul.data('dimensions').w > docrightedge)? x-$ul.data('dimensions').parentliw-$ul.data('dimensions').w : x //if not enough horizontal room to the ridge parent LI 
     //y=(parentlioffset.top+$ul.data('dimensions').h > docbottomedge)? y-$ul.data('dimensions').h+$ul.data('dimensions').parentlih : y 
    } 
+0

你必須刪除兩行 – Jose

相關問題