2016-08-05 12 views
0

こんにちは "mouseover"または "click"イベントのいずれかで選択できるメニューを作成しました。jQueryマウスオーバー時のメニュー選択に基づいて要素のフェード表示を切り替える

は、以下を参照してください。

選択を行った結果、私はそれが前/既存の選択された要素をフェードアウトしていることと一致するIDを持つ選択された要素をフェードインしたいということですdata-displaypanel属性で指定します。あなたはそれがより多く残しので完了する機会を持つ前にフェードアウト機能をキャンセルしているように見える別のメニュー項目間をすばやくマウスオーバーしかし

これは正常に動作します...

<ul class="topic-blocks list-unstyled"> 
<li class="keyword-panel" id="subjectcollection_A_C" style="display: list- 
.... 
</li> 
<li class="keyword-panel" id="subjectcollection_D_H" style="display: list- 
.... 
</li> 

1つの要素が表示されます....

これを修正する方法はありますか?私は運がない、いろいろな方法を試みました。 https://jsfiddle.net/angusgrant/ov2zezmv/

感謝アンガス

MouseEnterイベントは非常に迅速にトリガされ

答えて

0

$(".keyword-navigation li > a").on('click mouseenter', function(e) { 
    e.preventDefault(); 
    var ithis = $(this); 
    if (!ithis.parent('li').hasClass('active')){ 
     var prevActivePanel = ithis.closest('.keyword-navigation') 
         .find('.active') 
         .removeClass('active') 
         .children('a') 
         .data('displaypanel'), 
     currentActivePanel= ithis.parent('li') 
           .addClass('active') 
           .end() 
           .data('displaypanel'); 


     $(prevActivePanel).fadeOut("fast", function() { 
      $(this).addClass('hidden'); 
      $(currentActivePanel) 
       .fadeIn("fast", function() { 
        history.pushState(null, null, currentActivePanel); 
       }).removeClass('hidden'); 

     }); 

    } 

}); 

は私jsfiddleを参照してください。この場合、debounce(fn, wait)機能を使用することができます。

$(prevActivePanel).fadeOut("fast", function() { 

あなたは(デバウンスなし)MouseEnterイベントの不要な効果を停止する場合はまた、あなたのアニメーションに can-someone-explain-the-debounce-function-in-javascript

に見ても、あなたは次の行を変更する必要があります

$(prevActivePanel).stop().fadeOut("fast", function() { 

.stop()の役割は次のとおりです。一致した要素で現在実行中のアニメーションを停止します。

スニペット:

$(document).ready(function() { 
 
    $(".js-keyword-navigation li > a:not(.disabled)").on('click mouseenter', function(e) { 
 
    e.preventDefault(); 
 
    var ithis = $(this), 
 
     keywordNav = ithis.closest('.js-keyword-navigation'); 
 

 

 
    if (!ithis.parent('li').hasClass('active')) { 
 
     keywordNav.find('a').addClass('disabled'); 
 
     var prevActivePanel = keywordNav 
 
     .find('.active') 
 
     .removeClass('active') 
 
     .children('a') 
 
     .data('displaypanel'), 
 
      currentActivePanel = ithis.parent('li') 
 
     .addClass('active') 
 
     .end() 
 
     .data('displaypanel'); 
 

 

 
     $(prevActivePanel).stop().fadeOut("fast", function() { 
 
     var ithis = $(this); 
 
     ithis.addClass('hidden'); 
 
     //console.log('previous Active Panel faded out:' + prevActivePanel); 
 
     $(currentActivePanel) 
 
     .fadeIn("fast", function() { 
 
      history.pushState(null, null, currentActivePanel); 
 
      $(this).removeClass('hidden'); 
 
      //console.log('current Active Panel faded in:' + currentActivePanel); 
 
      keywordNav.find('a').removeClass('disabled'); 
 

 
     }) 
 

 
     }); 
 

 
    } 
 

 
    }); 
 
    //check if hash url exists and check an element exists in the DOM that also has the class 'keyword-panel'. 
 
    if (document.location.hash.length > 0 && $(document.location.hash).hasClass('keyword-panel')) { 
 
    // console.log('hash detected:' + document.location.hash); 
 
    $(".js-keyword-navigation li > a[data-displaypanel=" + document.location.hash + "]").trigger('click'); 
 
    } 
 

 
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
 
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 

 
<div class="subjectscontainer panel panel-default"> 
 
    <div class="panel-heading"> 
 
     <h3 class="panel-title"> 
 
      <i class="fa fa-hashtag"></i> 
 
      Topics 
 
     </h3> 
 
    </div> 
 
    <div class="panel-body keyword-display"> 
 
     <ul class="nav nav-pills nav-justified js-keyword-navigation"> 
 
      <li class=""><a href="#" data-displaypanel="#subjectcollection_A_C" class="">A-C</a></li> 
 
      <li><a href="#" data-displaypanel="#subjectcollection_D_H" class="">D-H</a></li> 
 
      <li><a href="#" data-displaypanel="#subjectcollection_I_N" class="">I-N</a></li> 
 
      <li class="active"><a href="#" data-displaypanel="#subjectcollection_O_R" class="">O-R</a></li> 
 
      <li><a href="#" data-displaypanel="#subjectcollection_S_Z" class="">S-Z</a></li> 
 
     </ul> 
 
     <ul class="topic-blocks list-unstyled"> 
 
      <li class="keyword-panel hidden" id="subjectcollection_A_C" style="display: none;"> 
 
       <ul class="col-sm-3 col-xs-6 list-unstyled"> 
 
        <li><a href="/pub2web/search?value1=Acoustics&amp;option1=fulltext" title="">Acoustics</a></li> 
 
        <li><a href="/pub2web/search?value1=Agricultural+Economics+%26+Policy&amp;option1=fulltext" title="">Agricultural Economics &amp; Policy</a></li> 
 
        <li><a href="/pub2web/search?value1=Agricultural+Engineering&amp;option1=fulltext" title="">Agricultural Engineering</a></li> 
 
        <li><a href="/pub2web/search?value1=Agriculture&amp;option1=fulltext" title="">Agriculture</a></li> 
 
        <li><a href="/pub2web/search?value1=Agriculture%2C+Dairy+%26+Animal+Science&amp;option1=fulltext" title="">Agriculture, Dairy &amp; Animal Science</a></li> 
 
        <li><a href="/pub2web/search?value1=Agriculture%2C+Multidisciplinary&amp;option1=fulltext" title="">Agriculture, Multidisciplinary</a></li> 
 
        <li><a href="/pub2web/search?value1=Agronomy&amp;option1=fulltext" title="">Agronomy</a></li> 
 
        <li><a href="/pub2web/search?value1=Allergy&amp;option1=fulltext" title="">Allergy</a></li> 
 
        <li><a href="/pub2web/search?value1=Anatomy+%26+Morphology&amp;option1=fulltext" title="">Anatomy &amp; Morphology</a></li> 
 
        <li><a href="/pub2web/search?value1=Andrology&amp;option1=fulltext" title="">Andrology</a></li> 
 
        <li><a href="/pub2web/search?value1=Anesthesiology&amp;option1=fulltext" title="">Anesthesiology</a></li> 
 
        <li><a href="/pub2web/search?value1=Anthropology&amp;option1=fulltext" title="">Anthropology</a></li> 
 
        <li><a href="/pub2web/search?value1=Archaeology&amp;option1=fulltext" title="">Archaeology</a></li> 
 
        <li><a href="/pub2web/search?value1=Architecture&amp;option1=fulltext" title="">Architecture</a></li> 
 
        <li><a href="/pub2web/search?value1=Area+Studies&amp;option1=fulltext" title="">Area Studies</a></li> 
 
        <li><a href="/pub2web/search?value1=Art&amp;option1=fulltext" title="">Art</a></li> 
 
       </ul> 
 
       <ul class="col-sm-3 col-xs-6 list-unstyled"> 
 
        <li><a href="/pub2web/search?value1=Arts+%26+Humanities+-+Other+Topics&amp;option1=fulltext" title="">Arts &amp; Humanities - Other Topics</a></li> 
 
        <li><a href="/pub2web/search?value1=Asian+Studies&amp;option1=fulltext" title="">Asian Studies</a></li> 
 
        <li><a href="/pub2web/search?value1=Astronomy+%26+Astrophysics&amp;option1=fulltext" title="">Astronomy &amp; Astrophysics</a></li> 
 
        <li><a href="/pub2web/search?value1=Audiology+%26+Speech-Language+Pathology&amp;option1=fulltext" title="">Audiology &amp; Speech-Language Pathology</a></li> 
 
        <li><a href="/pub2web/search?value1=Automation+%26+Control+Systems&amp;option1=fulltext" title="">Automation &amp; Control Systems</a></li> 
 
        <li><a href="/pub2web/search?value1=Behavioral+Sciences&amp;option1=fulltext" title="">Behavioral Sciences</a></li> 
 
        <li><a href="/pub2web/search?value1=Biochemical+Research+Methods&amp;option1=fulltext" title="">Biochemical Research Methods</a></li> 
 
        <li><a href="/pub2web/search?value1=Biochemistry+%26+Molecular+Biology&amp;option1=fulltext" title="">Biochemistry &amp; Molecular Biology</a></li> 
 
        <li><a href="/pub2web/search?value1=Biodiversity+Conservation&amp;option1=fulltext" title="">Biodiversity Conservation</a></li> 
 
        <li><a href="/pub2web/search?value1=Biodiversity+%26+Conservation&amp;option1=fulltext" title="">Biodiversity &amp; Conservation</a></li> 
 
        <li><a href="/pub2web/search?value1=Biology&amp;option1=fulltext" title="">Biology</a></li> 
 
        <li><a href="/pub2web/search?value1=Biomedical+Social+Sciences&amp;option1=fulltext" title="">Biomedical Social Sciences</a></li> 
 
        <li><a href="/pub2web/search?value1=Biophysics&amp;option1=fulltext" title="">Biophysics</a></li> 
 
        <li><a href="/pub2web/search?value1=Biotechnology+%26+Applied+Microbiology&amp;option1=fulltext" title="">Biotechnology &amp; Applied Microbiology</a></li> 
 
        <li><a href="/pub2web/search?value1=Business&amp;option1=fulltext" title="">Business</a></li> 
 
        <li><a href="/pub2web/search?value1=Business+%26+Economics&amp;option1=fulltext" title="">Business &amp; Economics</a></li> 
 
       </ul> 
 
       <ul class="col-sm-3 col-xs-6 list-unstyled"> 
 
        <li><a href="/pub2web/search?value1=Business%2C+Finance&amp;option1=fulltext" title="">Business, Finance</a></li> 
 
        <li><a href="/pub2web/search?value1=Cardiac+%26+Cardiovascular+Systems&amp;option1=fulltext" title="">Cardiac &amp; Cardiovascular Systems</a></li> 
 
        <li><a href="/pub2web/search?value1=Cardiovascular+System+%26+Cardiology&amp;option1=fulltext" title="">Cardiovascular System &amp; Cardiology</a></li> 
 
        <li><a href="/pub2web/search?value1=Cell+Biology&amp;option1=fulltext" title="">Cell Biology</a></li> 
 
        <li><a href="/pub2web/search?value1=Cell+%26+Tissue+Engineering&amp;option1=fulltext" title="">Cell &amp; Tissue Engineering</a></li> 
 
        <li><a href="/pub2web/search?value1=Clinical+Neurology&amp;option1=fulltext" title="">Clinical Neurology</a></li> 
 
        <li><a href="/pub2web/search?value1=Communication&amp;option1=fulltext" title="">Communication</a></li> 
 
       </ul> 
 
       <ul class="col-sm-3 col-xs-6 list-unstyled"> 
 
        <li><a href="/pub2web/search?value1=Computer+Science&amp;option1=fulltext" title="">Computer Science</a></li> 
 
        <li><a href="/pub2web/search?value1=Computer+Science%2C+Artificial+Intelligence&amp;option1=fulltext" title="">Computer Science, Artificial Intelligence</a></li> 
 
        <li><a href="/pub2web/search?value1=Computer+Science%2C+Cybernetics&amp;option1=fulltext" title="">Computer Science, Cybernetics</a></li> 
 
        <li><a href="/pub2web/search?value1=Computer+Science%2C+Hardware+%26+Architecture&amp;option1=fulltext" title="">Computer Science, Hardware &amp; Architecture</a></li> 
 
        <li><a href="/pub2web/search?value1=Crystallography&amp;option1=fulltext" title="">Crystallography</a></li> 
 
        <li><a href="/pub2web/search?value1=Cultural+Studies&amp;option1=fulltext" title="">Cultural Studies</a></li> 
 
       </ul> 
 
      </li> 
 
      <li class="keyword-panel hidden" id="subjectcollection_D_H"> 
 
       <ul class="col-sm-3 col-xs-6 list-unstyled"> 
 
        <li><a href="/pub2web/search?value1=Dance&amp;option1=fulltext" title="">Dance</a></li> 
 
        <li><a href="/pub2web/search?value1=Demography&amp;option1=fulltext" title="">Demography</a></li> 
 
        <li><a href="/pub2web/search?value1=Dentistry%2C+Oral+Surgery+%26+Medicine&amp;option1=fulltext" title="">Dentistry, Oral Surgery &amp; Medicine</a></li> 
 
        <li><a href="/pub2web/search?value1=Dermatology&amp;option1=fulltext" title="">Dermatology</a></li> 
 
        <li><a href="/pub2web/search?value1=Developmental+Biology&amp;option1=fulltext" title="">Developmental Biology</a></li> 
 
        <li><a href="/pub2web/search?value1=Ecology&amp;option1=fulltext" title="">Ecology</a></li> 
 
        <li><a href="/pub2web/search?value1=Engineering%2C+Aerospace&amp;option1=fulltext" title="">Engineering, Aerospace</a></li> 
 
       </ul> 
 
       <ul class="col-sm-3 col-xs-6 list-unstyled"> 
 
        <li><a href="/pub2web/search?value1=Engineering%2C+Biomedical&amp;option1=fulltext" title="">Engineering, Biomedical</a></li> 
 
        <li><a href="/pub2web/search?value1=Engineering%2C+Chemical&amp;option1=fulltext" title="">Engineering, Chemical</a></li> 
 
        <li><a href="/pub2web/search?value1=Engineering%2C+Civil&amp;option1=fulltext" title="">Engineering, Civil</a></li> 
 
        <li><a href="/pub2web/search?value1=Engineering%2C+Electrical+%26+Electronic&amp;option1=fulltext" title="">Engineering, Electrical &amp; Electronic</a></li> 
 
        <li><a href="/pub2web/search?value1=Engineering%2C+Environmental&amp;option1=fulltext" title="">Engineering, Environmental</a></li> 
 
        <li><a href="/pub2web/search?value1=Engineering%2C+Geological&amp;option1=fulltext" title="">Engineering, Geological</a></li> 
 
       <li><a href="/pub2web/search?value1=Environmental+Sciences&amp;option1=fulltext" title="">Environmental Sciences</a></li> 
 
        <li><a href="/pub2web/search?value1=Environmental+Sciences+%26+Ecology&amp;option1=fulltext" title="">Environmental Sciences &amp; Ecology</a></li> 
 
       </ul> 
 
       <ul class="col-sm-3 col-xs-6 list-unstyled"> 
 
        <li><a href="/pub2web/search?value1=Environmental+Studies&amp;option1=fulltext" title="">Environmental Studies</a></li> 
 
        <li><a href="/pub2web/search?value1=Ergonomics&amp;option1=fulltext" title="">Ergonomics</a></li> 
 
        <li><a href="/pub2web/search?value1=Ethics&amp;option1=fulltext" title="">Ethics</a></li> 
 
        <li><a href="/pub2web/search?value1=Ethnic+Studies&amp;option1=fulltext" title="">Ethnic Studies</a></li> 
 
       <li><a href="/pub2web/search?value1=Genetics+%26+Heredity&amp;option1=fulltext" title="">Genetics &amp; Heredity</a></li> 
 
        <li><a href="/pub2web/search?value1=Geochemistry+%26+Geophysics&amp;option1=fulltext" title="">Geochemistry &amp; Geophysics</a></li> 
 
       </ul> 
 
       <ul class="col-sm-3 col-xs-6 list-unstyled"> 
 
        <li><a href="/pub2web/search?value1=Geography&amp;option1=fulltext" title="">Geography</a></li> 
 
        <li><a href="/pub2web/search?value1=Geography%2C+Physical&amp;option1=fulltext" title="">Geography, Physical</a></li> 
 
        <li><a href="/pub2web/search?value1=Geology&amp;option1=fulltext" title="">Geology</a></li> 
 
        <li><a href="/pub2web/search?value1=Geosciences%2C+Multidisciplinary&amp;option1=fulltext" title="">Geosciences, Multidisciplinary</a></li> 
 
        <li><a href="/pub2web/search?value1=Geriatrics+%26+Gerontology&amp;option1=fulltext" title="">Geriatrics &amp; Gerontology</a></li> 
 
        <li><a href="/pub2web/search?value1=Gerontology&amp;option1=fulltext" title="">Gerontology</a></li> 
 
       <li><a href="/pub2web/search?value1=Humanities%2C+Multidisciplinary&amp;option1=fulltext" title="">Humanities, Multidisciplinary</a></li> 
 
       </ul> 
 
      </li> 
 
     </ul> 
 
    </div> 
 
</div>