0

I am trying to implement zoom (popup) effect on thumbnail image hover. I need to set position to center of the window height, but sometimes it fails, when the images aren't completely load (so I think the height of image is incorrect). How can I fully load image before it is shown? Thank you!

var winHeight = $(window).height();

//image popup on hover
$(".imageZoom").hover(function() {
    $(this).parent().append("<div class='bigImage'><img src='"+ $(this).attr("href") +"'></div>");
    $(".bigImage").css("width", "50vw").css("max-width", "800").css("position", "absolute").css("display", "none");
    var imageHeight = $(".bigImage").height();
    var top = $(window).scrollTop();
    var imageTop = top;
    if(imageHeight < winHeight) {
        imageTop += Math.ceil((winHeight - imageHeight) / 2);
    }

    $(".bigImage").css("top", imageTop).css("margin-left", 100).show(600);
}, function() {
    $(this).parent().find("div").remove();
});

1 Answer 1

1

Using the load event of the images, you can execute code when your image as been loaded.

Exemple :

$(".bigImage").on('load', function() {
    //code
});

If you run into cache issues and the function not being executed, take a look at this link : http://mikefowler.me/2014/04/22/cached-images-load-event/

Sign up to request clarification or add additional context in comments.

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.