2015-02-08 61 views
0

我有以下功能,这是应该改变背景颜色与淡入淡出,然后使用延迟返回到原来的颜色。并不断重复。jQuery改变背景颜色与淡入淡出,并返回原来的延迟

$(function() { 
    $('div').animate({background-color: 'blue'}, 'slow', function() { 
    $(this).delay(500).animate({background-color: 'red'}, 'slow'); 
}); 
}); 

然而,它似乎并没有工作... 任何想法如何解决呢?

+1

是吗? http://stackoverflow.com/questions/4713477/how-to-make-a-jquery-infinite-animation – Stickers 2015-02-08 00:53:53

+0

你有没有解决你的问题? – 2015-02-08 09:35:04

回答

0

请不要忘记包含jquery-ui库。否则,这是行不通的

$(document).ready(function() { 
 

 
    $('.sample').click(function() { 
 

 
    $(this).animate({ 
 
     backgroundColor: "green" 
 
    }, 1000).delay(2000).queue(function() { 
 
     $(this).animate({ 
 
     backgroundColor: "red" 
 
     }, 1000).dequeue(); 
 
    }); 
 

 
    }); 
 

 
});
.sample { 
 
    font: normal 12px/18px 'Arial'; 
 
    color: #fff; 
 
    background: red; 
 
    padding: 20px; 
 
    cursor: pointer 
 
}
<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 
</head> 
 
<body> 
 
<div class="sample"> 
 
Click Me to see smooth background color transition and back to original background 
 
</div> 
 
</body>