0

Is there a way to hide an object until my script is done loading? Using ajaxcomplete() or ajaxSuccess() didn't work. Below an example script.

there is an image with the id: imagefocus, every time i change the image via ajax, you see for a split second the original image size.

$("#imageFocus").bind("load", function(){
    var width = $(this).width();
    var height = $(this).height();

   if(height > 443) {
        var scaleRatio  = height / 443,
            width       = Math.round(width / scaleRatio),
            mLeft       = Math.round((590 - width) / 2);
        $(this).css({
            height:     "443px",
            width:      width+"px"
        });
        $(this).parent().css({
                marginTop:  "0px",
                marginLeft: mLeft+"px"
        });
    }
}

3 Answers 3

3

Hide the image through css and then show it on it's load event. Also before you make ajax call hide the image, the below code will take care of showing it once the image is loaded.

Css

#imageFocus{
    display:none;
}

JS

$("#imageFocus").bind("load", function(){
    var $this = $(this);
    var width = $this.width();
    var height = $this.height();

   if(height > 443) {
        var scaleRatio  = height / 443,
            width       = Math.round(width / scaleRatio),
            mLeft       = Math.round((590 - width) / 2);

        $this
        .css({
            height:     "443px",
            width:      width+"px"
        })
        .show() //<---------Show the image
        .parent()
        .css({
                marginTop:  "0px",
                marginLeft: mLeft+"px"
        });
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much. So simple yet so effective. :)
0

Yes, start with:

$("#imageFocus").hide()

and end with (in your load function)

$("#imageFocus").show()

2 Comments

i tried this, didn't work. #imagefocus is a image, every time the image changes via ajax, you still see for a split second the original image size.
Before hiding, use: $("#imageFocus").attr('src','')
0

That happens because the load finishes and auto-inserts your image. You cannot achieve the behavior you want in the actualcallback. Instead create a function, that hides the image and after that loads the new one. Then in the callback, make it visible again

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.