0

I'm creating an image zoom via the scroll wheel but am having trouble passing variables between functions.

Before I call the img_scroll() function I need to get the minimum and maximum dimensions of the image I want to zoom in and out of. to get the dimensions I need to wait for the image to load first.

So right now I wait for the image to load, call the dimensions function, then once the dimensions are loaded, I call the scroll functions. But right now I'm having trouble passing the variables down the chain to the scroll function.

zoom = 0.01;

$(document).ready(function(){
    $("#img_wrapper").imagesLoaded(function(){

        getImgSizes();

    });


});

function getMinImgSize() {
    if ($("#img_wrapper img").width() <= $("#img_wrapper img").height()) {
        $("#img_wrapper img").css("width", "100%");
    } else {
        $("#img_wrapper img").css("height", "100%");
    }
    min_width = $("#img_wrapper img").width();
    min_height = $("#img_wrapper img").height();
    return {height: min_height, width: min_width};
}

function getImgSizes() {
    var newImg = new Image();
    var width, height;
    imgSrc = $("#img_wrapper img")[0].src;

    minDimensions = getMinImgSize();

    newImg.onload = function(minDimensions) { //incorect way to pass varaible to anonymous function
        originalHeight = newImg.height;
        originalWidth = newImg.width;
        maxDimensions = {height: originalHeight, width: originalWidth};
        img_scroll(minDimensions, maxDimensions);
    }

    newImg.src = imgSrc; // this must be done AFTER setting onload

}

function img_scroll(minDimensions, maxDimensions){
    var minDimensions = minDimensions;
    var maxDimensions = maxDimensions;
    $('#img_wrapper').mousewheel(function(event, delta, deltaX, deltaY) {
        console.log(delta, deltaX, deltaY);
        if (deltaY < 0) {
            if ($("#img_wrapper img").width < maxDimensions.width) {
                $("#img_wrapper img").width($("#img_wrapper img").width()*(1+zoom));
                $("#img_wrapper img").height($("#img_wrapper img").height()*(1+zoom));  
            } else {
                                    $("#img_wrapper img").width(maxDimensions.width);
                                    $("#img_wrapper img").height(maxDimensions.height);
                                }
        } else {
            if ($("#img_wrapper img").width > minDimensions.width) {
                $("#img_wrapper img").width($("#img_wrapper img").width()*(1-zoom));
                $("#img_wrapper img").height($("#img_wrapper img").height()*(1-zoom));  
            } else {
                            $("#img_wrapper img").width(minDimensions.width);
                            $("#img_wrapper img").height(minDimensions.height);
                        }
        }
    });
}

Any help on how to pass the min & max dimension variables down the chain would be great.

1 Answer 1

1

Through the way closures work in Javascript, minDimensions is available to your anonymous function without being explicitly passed in. This should work:

function getImgSizes() {
    var newImg = new Image();
    var width, height;
    var imgSrc = $("#img_wrapper img")[0].src;

    var minDimensions = getMinImgSize();

    newImg.onload = function() {
        var originalHeight = newImg.height;
        var originalWidth = newImg.width;
        var maxDimensions = {height: originalHeight, width: originalWidth};
        img_scroll(minDimensions, maxDimensions);
    }

    newImg.src = imgSrc; // this must be done AFTER setting onload

}

EDIT: Use var to prevent global variables.

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.