简体   繁体   English

jQuery fadeOut回调函数未调用

[英]jQuery fadeOut callback function not being called

I am trying to swap divs by fading them in and out when another div is clicked. 我试图通过单击另一个div时淡入和淡出它们来交换div。 The only problem is: first the callback function wouldn't get called, so I took it out completely and the divs are acting funny now. 唯一的问题是:首先不会调用回调函数,所以我完全将其取出,并且div现在表现得很有趣。 Meaning when I click one of the divs the corresponding div that should fadeIn doesn't and the div that should have fadedOut did not. 意思是当我单击一个div时,相应的div应该淡入而不是div,应该淡出的div没有。

HTML: HTML:

<ul id="tour"> 
   <li id="pitch_link" class="selected">
      <h1>Pitch</h1>
      <p>Pitch a ball.</p>
   </li>
   <li id="publish_link">   
      <h1>Publish</h1>
      <p>Publish the spin.</p>
   </li>     

<div id="pitch">  
  <ul>
    <li><h2>pitch Stuff</h2></li>
    <li><h2>Graphics</h2></li>
  </ul>
</div>

<div id="publish">  
  <ul>
    <li><h2>publish Stuff</h2></li>
    <li><h2>Graphics</h2></li>
  </ul>
</div>

jQuery w/out callback: jQuery w / out回调:

$("#tour li").click( function(event) {

    if( !$(this).is( ".selected" ) ) {

        // find the link that was previously selected and fade it out
        var prevSelectedLink = $(".selected").attr( 'id' );
        prevSelectedID = "#" + prevSelectedLink.substr( 0, prevSelectedLink.length-5 );

        // fade the previously selected div out
        $(prevSelectedID).fadeOut( "fast" );

        // Deselect the previously selected link (remove selected class)
        $(prevSelectedLink).removeClass( "selected" );  

        // Select the new Link          
        $(this).addClass("selected");

        // Fade the new div content in
        var linkID = $(this).attr( 'id' );  // linkID = pitch_link
        contentID = "#" + linkID.substr( 0, linkID.length-5 );  // contentID = #pitch

        $(contentID).fadeIn( "slow" );  

    }
});

jQuery w/ callback: if( !$(this).is( ".selected" ) ) { 带有回调的jQuery: if(!$(this).is(“ .selected”)){

// find the link that was previously selected and fade it out
var prevSelectedLink = $(".selected").attr( 'id' );
        prevSelectedID = "#" + prevSelectedLink.substr( 0, prevSelectedLink.length-5 );

        // fade the previously selected div out
        $(prevSelectedID).fadeOut( "fast" , function() {

            // Deselect the previously selected link (remove selected class)
            $(prevSelectedLink).removeClass( "selected" );  

            // Select the new Link          
            $(this).addClass("selected");

            // Fade the new div content in
            var linkID = $(this).attr( 'id' );  // linkID = pitch_link
            contentID = "#" + linkID.substr( 0, linkID.length-5 );  // contentID = #pitch

            $(contentID).fadeIn( "slow" );
        }); 

    }
});

Without the CSS, I can't really tell what the problem is but the code can be cleaned up like so: 没有CSS,我无法真正分辨出问题所在,但是可以像这样清理代码:

$('#tour li').click(function(){

   if( !$(this).hasClass('selected') ){

       //Get the ID of the DIV you want to show
       var div_id = $(this).attr( 'id' ).replace('_link','');
       $('#tour li').removeClass('selected');
       $(this).addClass('selected');

       $('div').fadeOut("fast", function(){
            $('#'+div_id).fadeIn("fast");
       });

   }else{
       return false;
   }

});

I haven't tested this but what it does is if the link is not selected, it gets the ID of the div using the link ID, removes the 'selected' class from all other links and adds the 'selected' class to the li clicked. 我还没有测试过,但是如果未选择链接,它会执行的操作是,它使用链接ID获取div的ID,从所有其他链接中删除“ selected”类,并将“ selected”类添加到li点击。 All div's are then faded out and finally the required div is faded In. 然后淡出所有div,最后淡入所需的div。

You can also use the .not() operator to prevent the fadeOut() for the div with 'div_id'. 您还可以使用.not()运算符来防止带有'div_id'的div的fadeOut()。

This is the code I ended up with that worked thanks a lot to Sagar. 这是我最终使用的代码,非常感谢Sagar。

$("#tour li").click( function(event) {

    // make sure we are not already selected
    if( !$(this).hasClass( "selected" ) ) {

        // find the tab link that was previously selected and the corresponding div content
        var prevTab = '#' + $(".selected").attr( 'id' );    // prevTab = #pitch_link
        prevTabCont = prevTab.replace( '_link', '' );   // prevTabCont = #pitch

        // Deselect the previously selected tab link (remove selected class)
        $(prevTab).removeClass( "selected" );   

        // Find the currently selected tab and its corresponding div content
        var selectedTab = '#' + $(this).attr( 'id' );   // selectedTab = #publish_link
        selectedTabCont = selectedTab.replace( '_link', '' );   // selectedTabCont = #publish

        // Make the tab link selected       
        $(this).addClass("selected"); // this -> #publish_link

        // fade the previously selected div out
        $(prevTabCont).fadeOut( "slow", function() {                        
            $(selectedTabCont).fadeIn( "slow" );
        });
    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM