【问题标题】:Google Images heterogeneous Thumb Positioning谷歌图像异构拇指定位
【发布时间】:2011-06-16 01:38:34
【问题描述】:

Google 图片搜索返回不同尺寸的图片。甚至他们的拇指大小也不同。但它们仍然以保持干净边缘的方式排列。即使调整浏览器的大小,也可以保持左右对齐正确。我注意到的是他们将一个图像页面分组到一个 ul 中,每个图像都在一个 li 中。并非所有行都包含相同数量的图像。但是他们仍然如何设法使不同尺寸的图像正确对齐?

编辑

虽然我已经接受了一个答案,但它并不完全匹配。这可能是一场近乎匹配的比赛。但是我仍然想知道他们正在做什么的确切程序。我无法画出图案。 似乎他们将page 包装在<ol> 中并将图像放入<li> 但是当我调整大小时,图像会在页面之间重新分配。但是page <ol> 现在应该包含多少张图片还有待决定。可以使用什么程序来做到这一点?并且图像也会根据我认为的standard 高度调整大小。并且在调整大小时会更改标准高度。多少钱?这是怎么决定的?

【问题讨论】:

    标签: html layout user-interface canvas


    【解决方案1】:

    这不完全一样,但是通过查看jQuery Masonry plug-in 采用的方法,您可能会获得一些关于如何优化图像“打包”的有用想法。

    【讨论】:

    • +1 不错的选择。但是我仍然想知道(如何)谷歌的方式。
    • 如果你使用 Firebug,你可以看到他们正在做一些有趣的事情:使用<canvas> (显然)动态裁剪缩略图以实现所需的间距,直接编码 base64 图像数据进入 JS 等。由于它们都在 JavaScript 中进行裁剪和调整大小,因此不难想象它们如何将每行的元素强制为所需的宽度和间距。
    【解决方案2】:

    他们知道每个缩略图有多大,因为它存储在他们的图像数据库中。他们只是让每个<li> 向左浮动,并根据该部分图像中最大的图像使它们成为固定大小。

    【讨论】:

    • 所有拇指都是固定大小的。和拇指不是根据浏览器大小或屏幕分辨率生成的。就这么简单吗?我仍然怀疑必须在边距和填充上进行一些棘手的测量来确定较小图像之间的距离。
    • 如果 Google 有人解决了背包问题,或者至少想出一种方便的方法来降低它的复杂性,我不会感到惊讶。你会注意到,如果你最终得到不同的宽度/缩略图,部分图像会被截断,所以计算出连续图像的最小数量并调整 li 的大小以使其正确并不难间距。
    • 是啊,这东西确实不太难。这真的很简单。但它是这样的,如果有人问我可以以某种方式让他们理解非数学但它的发生有多精确是一种容易混淆的难题。
    【解决方案3】:

    我为此编写了一个小插件HERE您可以观看它的实际操作:

    (function($){
    //to arrange elements like google image
    //start of the plugin
    var tm=TweenMax;
      var positionFunc= function(options, elem){
            var setting=$.extend({
                height:150,
                container:$('body'),
                margin:5,
                borderWidth:1,
                borderColor:'#000',
                borderStyle:'solid',
                boxShadow:'0 0 0 #000',
                borderRadius:0,
                type:'img'
                },options);
            tm.set($(elem),{
                'max-height':setting.height
                });
            $(elem).wrap('<div class="easyPositionWrap"></div>');
            var winsize=setting.container.width();
            var thisrow=0;
            var elementsused=0;
            var row=0;
            tm.set($('.easyPositionWrap'),{
                border:setting.borderWidth+'px '+setting.borderStyle+' '+setting.borderColor,
                borderRadius:setting.borderRadius,
                boxShadow:setting.boxShadow,
                margin:setting.margin,
                height:setting.height,
                position:'relative',
                display:'block',
                overflow:'hidden',
                float:'left'
                });
            $('.easyPositionWrap').each(function(index, element) {
                if(thisrow<winsize){
                    thisrow+=$(this).width()+(setting.margin*2)+(setting.borderWidth*2);
                    }
                else{
                    var currentwidth=thisrow-$(this).prevUntil('.easyPositionWrap:eq('+(elementsused-1)+')').width()-(setting.margin*2)+(setting.borderWidth*2);
                    var nextimagewidth=$(this).prev('.easyPositionWrap').width()+(setting.margin*2)+(setting.borderWidth*2);
                    var elems=$(this).prevAll('.easyPositionWrap').length-elementsused;
                    var widthtobetaken=(nextimagewidth-(winsize-currentwidth))/(elems);
                    if(widthtobetaken!=0){
                        if(elementsused==0){
                            $(this).prevUntil('.easyPositionWrap:eq(0)').each(function(index, element) {
                                $(this).width($(this).width()-widthtobetaken);
                                $(this).find(setting.type+':first-child').css('margin-left','-'+(widthtobetaken/2)+'px');
                                });
                            $('.easyPositionWrap:eq(0)').width($('.easyPositionWrap:eq(0)').width()-widthtobetaken);
                            $('.easyPositionWrap:eq(0) '+setting.type).css('margin-left','-'+(widthtobetaken/2)+'px');
                            }
                        else{
                            $(this).prevUntil('.easyPositionWrap:eq('+(elementsused-1)+')').each(function(index, element) {
                                $(this).width($(this).width()-widthtobetaken);
                                $(this).find(setting.type+':first-child').css('margin-left','-'+(widthtobetaken/2)+'px');
                                });
                            }
                        }
                    elementsused+=elems;
                    thisrow=$(this).width()+(setting.margin*2)+(setting.borderWidth*2);
                    }
                });
            $(window).resize(function(){
                clearTimeout(window.thePositionTO);
                window.thePositionTO=setTimeout(function(){
                    $(elem).each(function(index, element) {
                        $(this).unwrap('.easyPositionWrap');
                        $(this).data('easyPositioned',false);
                        });
                    $(elem).easyPosition(options);
                    },200);
                });
            }
        $.fn.easyPosition= function(options){
            if($(this).data('easyPositioned')) return;
            positionFunc(options, this);
            $(this).data('easyPositioned',true);
            };
    //end of the plugin
        }(jQuery));
    
    $(window).load(function(){
        $('img').easyPosition();
    });
    

    要包含的库:

    【讨论】:

      猜你喜欢
      • 2015-06-15
      • 2011-07-16
      • 1970-01-01
      • 2011-12-05
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多