2014-02-27 36 views
0

我試圖在懸停時創建一個下拉菜單,如下所示。如何知道兩個元素上的mouseLeave

在網站的標題下。

enter image description here

林與jQuery,林這樣使用事件的mouseenter和鼠標離開。 使用以下代碼。

var LastThis = null; 
       $(".divOpener, #floatingNewNav") 
       .mouseleave(function(event) 
       { 
        console.log(event); 
        if($(event.toElement).attr("id") != "floatingNewNav") // do not close since we leaved the element but we got on the floating nav. 
        { 
         $("#floatingNewNav").hide(0); 

         if(LastThis.attr("id") == "ShopByBrand") 
         { 
          LastThis.removeClass("NavSelected"); 
          $("#"+LastThis.attr("id")+"_Nav").css("display","none"); 
         } 
         if(LastThis.attr("id") == "ShopByCategory") 
         { 
          LastThis.removeClass("NavSelected"); 
          $("#"+LastThis.attr("id")+"_Nav").css("display","none"); 
         } 
         if(LastThis.attr("id") == "ShopByPrice") 
         { 
          LastThis.removeClass("NavSelected"); 
          $("#"+LastThis.attr("id")+"_Nav").css("display","none"); 
         } 
        } 
       }); 
       $(".divOpener") 
       .mouseenter(function() 
       { 
        LastThis = $(this); 
        if($(this).attr("id") == "ShopByBrand") 
        { 
         $("#"+$(this).attr("id")+"_Nav").css("display","block"); 
         $(this).addClass('NavSelected'); 
        } 
        if($(this).attr("id") == "ShopByCategory") 
        { 
         $("#"+$(this).attr("id")+"_Nav").css("display","block"); 
         $(this).addClass('NavSelected'); 
        } 
        if($(this).attr("id") == "ShopByPrice") 
        { 
         $("#"+$(this).attr("id")+"_Nav").css("display","block"); 
         $(this).addClass('NavSelected'); 
        } 
        var DivPosition = $(this).parent().position(); 
        var Position = $(this).position(); 

        var curTop = DivPosition.top; 
        var curLeft = Position.left; 
        var curWidth = $(this).width(); 
        var curHeight = $(this).parent().height(); 
        var DivWidth = $(this).parent().width(); 

        var WidthOfNav = 400; 

        var OffSetLeft = (curLeft+(curWidth/2)-(WidthOfNav/2)+WidthOfNav)-(DivPosition.left+DivWidth); 
        var OffSetLeft = (OffSetLeft>0?OffSetLeft:0); 

        $("#floatingNewNav").css("position","absolute"); 
        $("#floatingNewNav").css("height","100px"); 
        $("#floatingNewNav").css("top",(curTop+curHeight)+"px"); 
        $("#floatingNewNav").css("left",((curLeft+(curWidth/2))-(WidthOfNav/2))-OffSetLeft+"px"); 
        $("#floatingNewNav").css("width",WidthOfNav+"px"); 

        $("#floatingNewNav").show(0); 
       }); 

的Html

<div id="newNavDiv"> 
         <span><form style="display: inline-block;" action="search.php" method="get"><input id="SearchBar" name="q" type="text"></form></span> 
         <div class="SearchButtonDiv"><input id="SearchButton" type="button" value="SEARCH"></div> 
       <span class="NewNavSeparator"></span> 
       <div id="Special" style="color: red;"> 
        SPECIALS 
       </div> 
       <span class="NewNavSeparator"></span> 
       <div id="ShopByBrand" class="divOpener"> 
        SHOP BY<br/>BRAND 
       </div> 
       <span class="NewNavSeparator"></span> 
       <div id="ShopByCategory" class="divOpener"> 
        SHOP BY<br/>CATEGORY 
       </div> 
       <span class="NewNavSeparator"></span> 
       <div id="ShopByPrice" class="divOpener"> 
        SHOP BY<br/>PRICE 
       </div> 
      </div> 
      <div id="floatingNewNav"> 
       <div id="ShopByBrand_Nav"></div> 
       <div id="ShopByCategory_Nav"></div> 
       <div id="ShopByPrice_Nav"></div> 
      </div> 

CSS

#WebsiteHeader 
     { 
      height: 170px; 
      background: url("Photo/header.png") no-repeat top; 
      background-size:100%; 
     } 
     #NewNavBar 
     { 
      height: 42px; 
      background: url("Photo/newNavigator.png") no-repeat top; 
      background-size:100%; 

      padding: 4px; 
      text-align: center; 
     } 
     #newNavDiv 
     { 
      display: inline-block; 
      width: 960px; 
      text-align: right; 
     } 
     #SearchBar 
     { 
      font-size: 16px; 
      color: grey; 

      width: 245px; 
      height: 24px; 
      padding-left: 5px; 
      background-color: #ffffff; 

      border-radius: 4px 4px 4px 4px; 
      -moz-border-radius: 4px 4px 4px 4px; 
      -webkit-border-radius: 4px 4px 4px 4px; 
      border: 1px solid #c7c7c7; 
     } 
     .SearchButtonDiv 
     { 
      display: inline-block; 
     } 
     #SearchButton 
     { 
      color:#ffffff; 
      font-size: 13px; 

      height: 30px; 
      background-color: red; 

      padding: 8px; 

      border-radius: 4px 4px 4px 4px; 
      -moz-border-radius: 4px 4px 4px 4px; 
      -webkit-border-radius: 4px 4px 4px 4px; 
      border: 0px solid; 
     } 
     #Special 
     { 
      vertical-align: middle; 
      width: 130px; 
      display: inline-block; 
      text-align: center; 

      color: #ffffff; 
      font-family: "Arial";/* for firefox*/ 
      font-family: "Arial Black";/* if browser have the font it will overide arial*/ 
      font-weight:900;/* for firefox*/ 
      font-size: 13px; 
      font-style: italic; 
     } 
     .divOpener 
     { 
      vertical-align: middle; 
      width: 140px; 
      display: inline-block; 
      text-align: center; 

      color: #ffffff; 
      font-family: "Arial";/* for firefox*/ 
      font-family: "Arial Black";/* if browser have the font it will overide arial*/ 
      font-weight:900;/* for firefox*/ 
      font-size: 13px; 
      font-style: italic; 
     } 
     .NewNavSeparator 
     { 
      border-right: 1px rgba(245, 245, 245, 0.70) solid; 
      margin-right: 5px; 
      margin-left: 6px; 
      height: 30px; 
     } 
     #MainPagesLinks 
     { 
      padding-bottom: 0; 
     } 
     #MainPagesLinks a 
     { 
      text-align: center; 
      color:#ffffff; 
      text-decoration: none; 

      font-size: 13px; 

      width: 75px; 

      display: inline-block; 
      background-color: red; 

      padding-left: 4px; 
      padding-right: 4px; 

      border-radius: 4px 4px 0px 0px; 
      -moz-border-radius: 4px 4px 0px 0px; 
      -webkit-border-radius: 4px 4px 0px 0px; 


      border-top: 1px solid rgba(255,240,240,0.4); 
      border-right: 1px solid rgba(255,240,240,0.4); 
      border-left: 1px solid rgba(255,240,240,0.4); 
      box-shadow: 
       inset 0 3px 2px rgba(255,255,255,.22), 
       inset 0 20px 10px rgba(255,255,255,.12), 
       0 0 4px 1px rgba(0,0,0,.1), 
       0 3px 2px rgba(0,0,0,.2); 

      /*border: 1px solid #000000;*/ 
     } 
     #floatingNewNav 
     { 
      background-color: #aaaac6; 
      margin-top: 0px; 

      border-radius: 0px 0px 6px 6px; 
      -moz-border-radius: 0px 0px 6px 6px; 
      -webkit-border-radius: 0px 0px 6px 6px; 
     } 
     .NavSelected 
     { 
      color:black; 
      background-color: #aaaac6; 
      border-bottom: 0px #aaaac6 solid; 

      border-radius: 6px 6px 0px 0px; 
      -moz-border-radius: 6px 6px 0px 0px; 
      -webkit-border-radius: 6px 6px 0px 0px; 
     } 

.divOpener類是<a> Shop by category</a>和其他鏈接 #floatingNewNav

這裏是活生生的例子。 https://www.newyorkpowertools.com/Template/headerTemplate.html

您將在這個例子看到,它在鉻,IE和Safari但在Firefox的不起作用。東陽由於某種原因,火狐不要在event對象返回對象toElement ..

我的問題

有沒有一種方法,我可以有兩個元素,假創建一個事件MouseEnter和鼠標離開,這是隻有一個元素等等我不需要

if($(event.toElement).attr("id") != "floatingNewNav")

感謝您的幫助,而且很抱歉的混亂問題,。

+0

它似乎是你的if語句是不必要的,因爲你只有3菜單項,並在每個如果你正在做同樣的事情。編輯:對不起,沒有看到floatingdiv,只是檢查不floatingdiv – Huangism

+0

這是需要的,因爲如果我一旦我離開.DivOpener關閉即使我去浮動導航。 –

+0

是啊看到我的編輯,只是檢查id'不浮動導航',而不是檢查所有3個ID。或者你可以使用https://api.jquery.com/not/,或者你可以檢查$(this)是否具有divOpener,有很多方法可以在一次檢查中代替3 – Huangism

回答

1

你可以簡單地這樣做http://jsfiddle.net/aheLv/1/

$(".divOpener, #floatingNewNav").mouseenter(function() { 
    if (!$(this).is('#floatingNewNav')) { 
     LastThis = $(this); 
    } 
    $("#" + LastThis.attr("id") + "_Nav").css("display", "block"); 
    LastThis.addClass('NavSelected'); 

    var DivPosition = LastThis.parent().position(); 
    var Position = LastThis.position(); 

    var curTop = DivPosition.top; 
    var curLeft = Position.left; 
    var curWidth = LastThis.width(); 
    var curHeight = LastThis.parent().height(); 
    var DivWidth = LastThis.parent().width(); 

    var WidthOfNav = 400; 

    var OffSetLeft = (curLeft + (curWidth/2) - (WidthOfNav/2) + WidthOfNav) - (DivPosition.left + DivWidth); 
    var OffSetLeft = (OffSetLeft > 0 ? OffSetLeft : 0); 

    $("#floatingNewNav").css("position", "absolute"); 
    $("#floatingNewNav").css("height", "100px"); 
    $("#floatingNewNav").css("top", (curTop + curHeight) + "px"); 
    $("#floatingNewNav").css("left", ((curLeft + (curWidth/2)) - (WidthOfNav/2)) - OffSetLeft + "px"); 
    $("#floatingNewNav").css("width", WidthOfNav + "px"); 

    $("#floatingNewNav").show(0); 
}); 

有很多事情可以做,以重構這個代碼。其實有中,我把他們從現在搗鼓一些額外的東西,我會更新很快

UPDATE:

這裏是最後的代碼

var LastThis = null; 
var openers = $(".divOpener, #floatingNewNav"); 

openers.mouseleave(function (event) { 
    $("#floatingNewNav").hide(); 
    LastThis.removeClass("NavSelected"); 
    $("#" + LastThis.attr("id") + "_Nav").hide(); 
}); 

openers.mouseenter(function() { 
    if (!$(this).is('#floatingNewNav')) { 
     LastThis = $(this); 
    } 
    $("#" + LastThis.attr("id") + "_Nav").show(); 
    LastThis.addClass('NavSelected'); 

    var DivPosition = LastThis.parent().position(); 
    var Position = LastThis.position(); 

    var curTop = DivPosition.top; 
    var curLeft = Position.left; 
    var curWidth = LastThis.width(); 
    var curHeight = LastThis.parent().height(); 
    var DivWidth = LastThis.parent().width(); 

    var WidthOfNav = 400; 

    var OffSetLeft = (curLeft + (curWidth/2) - (WidthOfNav/2) + WidthOfNav) - (DivPosition.left + DivWidth); 
    var OffSetLeft = (OffSetLeft > 0 ? OffSetLeft : 0); 

    $("#floatingNewNav").css({ 
     'position': 'absolute', 
     'height': '100px', 
     'top': (curTop + curHeight) + 'px', 
     'left': ((curLeft + (curWidth/2)) - (WidthOfNav/2)) - OffSetLeft + 'px', 
     'width': WidthOfNav + 'px' 
    }).show(); 
}); 

http://jsfiddle.net/aheLv/2/

作爲建議,只有在以正確的方式構建html時,才能在CSS中完成這些菜單。見凱文的答案結構

+0

哇。這是工作!我等你回覆謝謝你的時間!更新了代碼 –

+0

,查看最終版本 – Huangism

2

要創建懸停時出現的下拉菜單,就像示例中的那樣,我會使用CSS。見下面例子:

HTML

<ul id="menu"> 
    <li><a href="#">Menu 1</a></li> 
    <li><a href="#">Menu 2</a> 
     <ul class="sub-menu"> 
      <li><a href="#">Sub Menu 1</a></li> 
      <li><a href="#">Sub Menu 2</a></li> 
      <li><a href="#">Sub Menu 3</a></li> 
      <li><a href="#">Sub Menu 4</a></li> 
     </ul> 
    </li> 
    <li><a href="#">Menu 3</a></li> 
</ul> 

CSS

#menu li ul { 
    display: none; 
} 
#menu li:hover ul { 
    display: block; 
} 
+0

我不敢肯定這可以滿足我的需求,因爲我認爲元素會在菜單懸停時被推下。 –

+0

如果您在'li'容器上的'.sub-menu'和'position:relative;'上使用'position:absolute;',它不應該在懸停時按下其他元素。 –

+0

我應該澄清一點,除了我的示例之外,您還必須編寫其他CSS,以使菜單的樣式和位置與您的屏幕截圖一樣。我的例子只說明瞭你想要的懸停效果的顯示/隱藏功能。 –

0

只是拋出了一個不同的概念,你的畫面尖叫 「jQuery的標籤」 :-),所以我用簡單的標籤,併爲每個人創建一個jQuery觸發事件。

FIDDLE

JS

$('.charts').tabs(); 

$('#firstone').mouseover(function(){ 
     $(this).trigger('click'); 
}); 
$('#secondone').mouseover(function(){ 
     $(this).trigger('click'); 
}); 
$('#thirdone').mouseover(function(){ 
     $(this).trigger('click'); 
}); 

不優雅,但我會用它多一些玩,看看它是否可以 'elegantized'。

+0

嗯,這是一個不錯的主意。我試圖讓它看起來像我想要的導航。 但它必須能夠在鼠標不存在時關閉,並且它必須漂浮在網站內容上。 :S –

+0

我實際上能夠讓標籤像導航欄一樣工作(面板在命令上彈出,但標籤保持可見),但不幸的是,它不覆蓋它下面的元素,它將它們推倒。哦,只是一個想法。 – TimSPQR

0

好像你已經過於複雜的東西..

如何只追蹤鼠標的方向,而如果其向下爲首的,不要隱藏下面的菜單項..

檢查這個小提琴.. http://jsfiddle.net/ReVLN/2/

經過它的工作在Mozilla和Chrome /等

var mY = 0; 
$('div.menuItems').mousemove(function(e){ 
// moving upward 
if (e.pageY < mY) { 
    flag = "upward"; 
} else { 
    flag = "downward"; 
} 
// set new mY after doing test above 
mY = e.pageY; 
}); 

謝謝, jf_it

相關問題