Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在函数完成后使用jquery设置动画-让第二个函数等待?_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何在函数完成后使用jquery设置动画-让第二个函数等待?

Javascript 如何在函数完成后使用jquery设置动画-让第二个函数等待?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个简单的jQuery代码,它通过隐藏一个图像和显示另一个图像来交换两个图像,我试图使用淡入淡出效果来交换图像,但是由于两个图像不是相互重叠的,我不能简单地淡出顶部图像,从而显示底部图像, 我想淡入第一幅图像,然后将css显示属性设置为无,然后以0不透明度显示第二幅图像,并逐渐将第二幅图像不透明度设置为100。但是当我添加淡入淡出图像的代码时,它不起作用,并且显示none不会等待淡入完成。如何使函数等待函数完成 $('.thumbs').hover( function() {

我有一个简单的jQuery代码,它通过隐藏一个图像和显示另一个图像来交换两个图像,我试图使用淡入淡出效果来交换图像,但是由于两个图像不是相互重叠的,我不能简单地淡出顶部图像,从而显示底部图像, 我想淡入第一幅图像,然后将css显示属性设置为无,然后以0不透明度显示第二幅图像,并逐渐将第二幅图像不透明度设置为100。但是当我添加淡入淡出图像的代码时,它不起作用,并且显示none不会等待淡入完成。如何使函数等待函数完成

$('.thumbs').hover(
        function() {
           console.info('in');
           $(this).children('.first').css('display','none'); 
           $(this).children('.second').css('display','block')
        },
        function() {
           console.info('out');
           $(this).children('.second').css('display','none'); 
           $(this).children('.first').css('display','block')
         }
);
HTML代码:

<div class='thumbs'>
            <div class='first'><?php the_post_thumbnail()?></div>
            <div class='second'><?php MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image');?></div>
                 </div>

像这样:

setTimeout(function() {
    console.log('out');
    $(this).children('.second').css('display', 'none');
    $(this).children('.first').css('display', 'block');
}, 1000);
像这样:

setTimeout(function() {
    console.log('out');
    $(this).children('.second').css('display', 'none');
    $(this).children('.first').css('display', 'block');
}, 1000);
1)
delay()
方法允许我们延迟队列中跟随它的函数的执行。

2)使用
回调

$("#divId1").animate({opacity:.1},1000,function(){
    $("#divId2").animate({opacity:.1},1000);    
});​
1)
delay()
方法允许我们延迟队列中跟随它的函数的执行。

2)使用
回调

$("#divId1").animate({opacity:.1},1000,function(){
    $("#divId2").animate({opacity:.1},1000);    
});​

我还没有测试过,但这应该可以做到:

$('.thumbs').hover(
    function(){
       var $that = $(this);
       $(this).children('.first').fadeOut(1000, function(){
           $(this).css('display','none');
           $that.children('.second').fadeIn(500);
       });
    }
    ,
   function(){
       var $that = $(this);
       $(this).children('.second').fadeOut(1000, function(){
           $(this).css('display','none');
           $that.children('.first').fadeIn(500);
       });
    }
);

我还没有测试过,但这应该可以做到:

$('.thumbs').hover(
    function(){
       var $that = $(this);
       $(this).children('.first').fadeOut(1000, function(){
           $(this).css('display','none');
           $that.children('.second').fadeIn(500);
       });
    }
    ,
   function(){
       var $that = $(this);
       $(this).children('.second').fadeOut(1000, function(){
           $(this).css('display','none');
           $that.children('.first').fadeIn(500);
       });
    }
);
试一试

试试看


您也可以发布您的HTML吗?另外,逐渐改变不透明度的代码在哪里?您发布的代码只是即时隐藏/显示div。@Osiris:我添加了html,至于不透明度更改,它与代码相同,只有$(this).children('.first').css('opacity','0');在显示前添加:无部分,然后在显示块后添加$(this).children('.second').css('opacity','100')。请记住,第二个图像的不透明度最初设置为0。您是否也可以发布HTML?另外,逐渐改变不透明度的代码在哪里?您发布的代码只是即时隐藏/显示div。@Osiris:我添加了html,至于不透明度更改,它与代码相同,只有$(this).children('.first').css('opacity','0');在显示之前添加:无部分,然后在显示块之后添加$(this).children('.second').css('opacity','100')。请记住,第二个图像的不透明度最初设置为0。使用回调函数听起来是正确的方法,但问题是我想使用$(this).children('.first'))在回调函数中($this)语句似乎没有引用它应该引用的包装器。使用回调函数听起来是正确的做法,但问题是我想使用$(this).children('.first'),在回调函数中($this)语句似乎是语句没有引用“它应该”中的包装。我尝试了代码,但不知道为什么它不会褪色,只会消失并重新出现,@Farzan检查小提琴示例我尝试了代码,但不知道为什么它不会褪色,只会消失并重新出现,@Farzan检查小提琴示例