javascript - 基于屏幕尺寸可见的图库图像

标签 javascript jquery gallery

我需要一个图片库来显示 3 行,每行显示不同数量的图像。并非所有图像都具有相同的宽度。当屏幕调整大小时,我希望显示更多图像,但比例相同(增长到 3/5/4、4/6/5 等...)。比例应该是这样的(括号代表一张图片):

[][]
[][][][]
[][][]

Visual example here.

除了偏移之外,它部分工作。我尝试编写一个偏移循环,但现在它根本不起作用。这是代码:

<script>
jQuery(document).ready(function() {
jQuery('.gallery').wrap('<div class="gal-wrapper" />');
  resizeGallery("#gallery-1",2);
  resizeGallery("#gallery-2",4);
  resizeGallery("#gallery-3",3);
});
jQuery(window).resize(function() {
  resizeGallery("#gallery-1",2);
  resizeGallery("#gallery-2",4);
  resizeGallery("#gallery-3",3);
});

function resizeGallery(gall, initpos){
var x = 0;
var y = 0;
var accum_width = 0;
var final_width = accum_width;
var offset = initpos;
var wW = jQuery(window).width()-jQuery("#nav").width();

while(accum_width < wW){

    var new_width = jQuery(gall).find("img.attachment-medium:eq("+x+")").width();
    if((accum_width + new_width) > wW){
        break;
    }else{
        accum_width += new_width+4;
    }
    x++;
}
/* worked partly to this point... onto the offset! */ 
while(y < offset){
    var subtract_width = jQuery(gall).find("img.attachment-medium:eq("+x+")").width();
    final_width -= subtract_width+4;
    y++;
    x--;
}


jQuery(gall).parent().width(final_width);
}
</script>

Here is a jsfiddle example .

最佳答案

Your working JS fiddle

如果那个 fiddle 链接不起作用...

jQuery(document).ready(function() {
    jQuery('.gallery').wrap('<div class="gal-wrapper" />');
    var picCount = resizeGallery("#gallery-2", Number.POSITIVE_INFINITY);
    resizeGallery("#gallery-3", picCount-1);
    resizeGallery("#gallery-1", picCount-2);
});
jQuery(window).resize(function() {
    var picCount = resizeGallery("#gallery-2", Number.POSITIVE_INFINITY);
    resizeGallery("#gallery-3", picCount-1);
    resizeGallery("#gallery-1", picCount-2);
});

function resizeGallery(gall, picCount) {
    var x = 0;
    var y = 0;
    var accum_width = 0;
    var wW = jQuery(window).width() - jQuery("#nav").width();

    while (accum_width < wW && x < picCount) {

        var new_width = jQuery(gall).find("img.attachment-medium:eq(" + x + ")").width();
        if ((accum_width + new_width) > wW) {
            break;
        } else {
            accum_width += new_width + 4;
        }
        x++;
    }

    jQuery(gall).parent().width(accum_width);

    return x;
}

关于javascript - 基于屏幕尺寸可见的图库图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6629611/

相关文章:

php - Wordpress:通过ajax调用插件php文件

android - 从gridview中选择图像并将其显示在其他 Intent 上

javascript - 使用 Jquery 在 HTML 中填充 "p"标记

javascript - Angular HttpModule 如何处理从远程 API 接收到的不适当数据

javascript - 单击时可下载文件的超链接,并为每次下载增加一个字段

jquery - 如何在 Meteor 框架内安全地使用 jQuery UI 小部件?

javascript - jQuery CSV 插件不会将行拆分为数组的数组

jquery - "rel"的多个 Fancybox 2 画廊问题

java - 单击时预览图像- Android

javascript - 用于检查字符串格式的正则表达式