elliott.david 2016-01-08 06:55 采纳率: 25%
浏览 9

jQuery链接错误

I feel like i'm missing something super simple, but am banging my head against the wall. I have this function called "galleryImageSize", and also a parse of some JSON data. What I would like to have happen is after the data is parsed and the div is given a class of 'active' - run the "galleryImageSize" function to resize the image. I've chained the function call after the click event, but it gives me an error that the whole thing is not a function. Any ideas from the ether?

   function galleryImageSize () {
    var height = $("#gallery-overlay .activeImage figure img").data('height');
    var width = $("#gallery-overlay .activeImage figure img").data('width');
    var divHeight = $("#gallery-overlay .activeImage").height();
    var divWidth = $("#gallery-overlay .activeImage").width();
    var portraitRatio = height / width;
    var newWidth = divHeight / portraitRatio;
    var ratio = width / height;
    var newHeight = divWidth / ratio;

    if (height > width) {

        if (divHeight > height && divWidth > width) {
            $( "#gallery-overlay .activeImage figure img" ).width( width ).height( height );
        }

        if (divHeight > height && width > divWidth) {
            $( "#gallery-overlay .activeImage figure img" ).width( divWidth ).height( height * portraitRatio );
        }

        if (newWidth > divWidth) {
             $( "#gallery-overlay .activeImage figure img" ).width( divWidth ).height( newHeight );
        }

        else {
            $( "#gallery-overlay .activeImage figure img" ).width( newWidth ).height( divHeight );
        }
    }

    else {

        if (divWidth > width && divHeight > height) {
            $( "#gallery-overlay .activeImage figure img" ).width( width ).height( height );
        }

        else if (divWidth > width && height > divHeight) {
            $( "#gallery-overlay .activeImage figure img" ).width( width * ratio ).height( divHeight );
        }

        else if (newHeight > divHeight) {
            $( "#gallery-overlay .activeImage figure img" ).width( newWidth ).height( divHeight );
        }

        else {
            $( "#gallery-overlay .activeImage figure img" ).width( divWidth ).height( newHeight );
        }
    }

 }

$('.gallery-fire').each(function(e) {
    $(document).on('click', '.gallery-fire', function(event){
        var request = $(this).data('gallery');
        var title = $(this).data('title');
        $('#gallery-overlay .postTitle h1').html(title);
        $.getJSON(request, function(data) {
            for (var i = 0; i < data.length; i++) {
                if (i == 0) {
                    $("#gallery-overlay .activeImage figure").html('<img data-height="'+data[i].height+'" data-width="'+data[i].width+'" src="'+data[i].image+'">');
                    $("#gallery-overlay .activeImage figcaption").html(data[i].caption+'<small>'+data[i].date+'</small>');                      
                }
            }
        });
        $( '#gallery-overlay' ).addClass( 'active');
    }).galleryImageSize (); 
});

Updated Code still doesn't work:

$('.gallery-fire').each(function(e) {
    $(document).on('click', '.gallery-fire', function(event){
        var request = $(this).data('gallery');
        var title = $(this).data('title');
        $('#gallery-overlay .postTitle h1').html(title);
        var get = function() {
            $.getJSON(request, function(data) {
                for (var i = 0; i < data.length; i++) {
                    if (i == 0) {
                        $("#gallery-overlay .activeImage figure").html('<img data-height="'+data[i].height+'" data-width="'+data[i].width+'" src="'+data[i].image+'">');
                        $("#gallery-overlay .activeImage figcaption").html(data[i].caption+'<small>'+data[i].date+'</small>');                      
                    }
                }
            });
        };

        $.when( get() ).done(function() {
            $('#gallery-overlay').addClass( 'active');
            galleryImageSize("#gallery-overlay .activeImage figure img");
        });
    });
});     

    function galleryImageSize (value) {
    var height = $(value).data('height');
    var width = $(value).data('width');
    var divHeight = $("#gallery-overlay .activeImage").height();
    var divWidth = $("#gallery-overlay .activeImage").width();
    var portraitRatio = height / width;
    var newWidth = divHeight / portraitRatio;
    var ratio = width / height;
    var newHeight = divWidth / ratio;

    if (height > width) {

        if (divHeight > height && divWidth > width) {
            $( value ).width( width ).height( height );
        }

        if (divHeight > height && width > divWidth) {
            $( value ).width( divWidth ).height( height * portraitRatio );
        }

        if (newWidth > divWidth) {
             $( value ).width( divWidth ).height( newHeight );
        }

        else {
            $( value ).width( newWidth ).height( divHeight );
        }
    }

    else {

        if (divWidth > width && divHeight > height) {
            $( value ).width( width ).height( height );
        }

        else if (divWidth > width && height > divHeight) {
            $( value ).width( width * ratio ).height( divHeight );
        }

        else if (newHeight > divHeight) {
            $( value ).width( newWidth ).height( divHeight );
        }

        else {
            $( value ).width( divWidth ).height( newHeight );
        }
    }

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 linux驱动,linux应用,多线程