I just implemented this very thing.
我只是实现了这一点。 Here's how I did it:
这是我的操作方式:
- When the page loads, I measure the available width of the target container (the container where my images will live), and call this maxWidth
当页面加载时,我测量目标容器(我的图像将在其中生存的容器)的可用宽度,并将其称为maxWidth
- I make an AJAX call, passing the maxWidth up to the server
我进行了AJAX调用,将maxWidth传递给服务器
- I determine my list of gallery images to display on the server, and process each one by one
我确定要显示在服务器上的画廊图像列表,并逐一处理
- During iteration of my list, I keep adding images to a row, the row's length being the maxWidth available (which I passed up).
在列表的迭代过程中,我一直在向行添加图像,该行的长度为可用的maxWidth(我向上传递的)。 Once an image overflows the maxWidth, I calculate the overflow amount
图像溢出maxWidth后,我将计算溢出量
- I take that overflow amount, divide it by the number of images in my row, and subtract that value off each of the images in the row (so one image doesn't get its width reduced too much)
我将溢出量除以我行中图像的数量,然后减去该行中每张图像的值(这样一幅图像的宽度不会减小太多)
- I repeat this for each row, so each row adds up to exactly maxWidth
我为每一行重复此操作,因此每一行加起来恰好是maxWidth
Some things I had to consider:
我必须考虑的一些事情:
- I had to take into account the width of the margins I wanted in between each image, and factor that into the maxWidth accumulated value when processing each row.
我必须考虑到每张图像之间所需的页边距宽度,并在处理每一行时将其作为maxWidth累积值的因素。
- We needed a corner stamp image (an image that was larger and was fixed in the top left corner. This image encompassed two rows, and was wider than my other thumbnails. I had to calculate a different maxWidth for the first 2 rows as a consequence.
我们需要一个角标记图像(一个更大的图像,并固定在左上角。该图像包含两行,并且比其他缩略图宽。因此,我必须为前两行计算不同的maxWidth 。
- I had to recalculate the dimensions when the browser was resized.
调整浏览器大小时,我不得不重新计算尺寸。 Binding to the resize event of the window caused multiple AJAX calls during dragging.
绑定到窗口的resize事件会导致在拖动过程中调用多个AJAX。 I had to fix the resize event so it only fired when done.
我必须修复resize事件,以便仅在完成后才触发。
Overall, the image gallery turned out quite well.
总体而言,图库的表现非常好。 He's a screenshot.
他是截图。
![在此处输入图片说明](https://stackoom.com/link/aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9ZU0x1eS5qcGc=)