我正在一個網站上工作。添加回調到jQuery CSS操作
在某些時候,我需要用照片替換div的背景圖像。但是,這大約需要5秒鐘,因此我想在完成此過程時顯示並隱藏加載屏幕。我試過了:
function changeBackgroundImage(imageURL) {
showLoadingScreen();
$("#imagePlaceholder").css('background-image', imageURL);
hideLoadingScreen();
}
但是hideLoadingScreen()在jQuery css發生之前執行。
我想添加hideLoadingScreen()作爲jQuery css的回調。這可能嗎?
這裏是我的其他2個功能:
function showLoadingScreen(){
$('#pleaseWait').removeClass('hidden');
console.log('show loading');
}
function hideLoadingScreen(){
$('#pleaseWait').addClass('hidden');
console.log('hide loading');
}
[的.css()](http://api.jquery.com/css/)沒有回調函數。 –
我從來沒有用過它,但jquery有一個when()函數,我相信它基本上允許你將任意方法附加到任何耗時的方法。 [鏈接](https://api.jquery.com/jQuery.when/) – user3334690
@ user3334690耗時的方法必須返回延遲或承諾,或者實現承諾接口的東西..'.css()'纔不是。 –