如何在jquery中执行某些操作然后执行另一个函数

时间:2013-11-25 07:05:16

标签: jquery html wait

我有一个div(“#ImageBingingDiv”),它显示无,我淡入它,而不是我改变它的html并添加picutre,在我这样做后我尝试添加到标签elevateZoom但因为div vas以某种方式显示没有做什么事情,所以我认为我必须以某种方式等到创建图像标签以添加elevateZoom。有什么建议?

$("#ImageBingingDiv").fadeIn(1000);
var widthD = parseInt($("#ImageBingingDiv").css('width')) - 10 + 'px';
var heightD = 0.5625 * (parseInt($("#ImageBingingDiv").css('width')) - 1);
$("#ImageBingingDiv").html('<img id="toZoom" data-zoom-image="./' + id + '" width="' + widthD + '" height="' + heightD + 'px" margin="10px" src="./' + id + '" alt=""/>');
$("#ImageBingingDiv").css('height', heightD + 12 + 'px').css('vertical-align', 'center');

$("#toZoom").elevateZoom({
    zoomWindowFadeIn: 500,
    zoomWindowFadeOut: 500,
    lensFadeIn: 500,
    lensFadeOut: 500
});

2 个答案:

答案 0 :(得分:0)

尝试使用jquery when();

或尝试回调函数。

对于ex,如果你有2个方法method1和method2 ..try

$.when(method1(), method2()).then(showData);​

只有当method1(),method2()完成执行时才会执行showdata。

例如

function showData() {
    alert(something);

}

function method1() {
    //logic
}

function method2() {
  //logic  
}

$.when(method1(), method2()).then(showData);​

答案 1 :(得分:0)

使用setTimeout,这将使它等待元素呈现。

setTimeout(function() {
$("#toZoom").elevateZoom({
    zoomWindowFadeIn: 500,
    zoomWindowFadeOut: 500,
    lensFadeIn: 500,
    lensFadeOut: 500
});
}, 10);