Ryan King
Ryan King

Reputation: 3696

Variable scope & event triggered anonymous functions

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.

Upvotes: 0

Views: 93

Answers (1)

go-oleg
go-oleg

Reputation: 19480

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.

Upvotes: 1

Related Questions