gpt4 book ai didi

javascript - CSS 列(照片网格)从左到右而不是从上到下显示

转载 作者:行者123 更新时间:2023-11-28 16:27:59 25 4
gpt4 key购买 nike

我正在使用列创建一个照片网格,将所有提供的照片放在一起,它们之间没有任何空白。

这是我正在使用的 CSS:

.autofit-photo-grid {
/* Prevent vertical gaps */
line-height: 0;
-webkit-column-count: 3;
-webkit-column-gap: 0;
-moz-column-count: 3;
-moz-column-gap: 0;
column-count: 3;
column-gap: 0;
}

.autofit-photo-grid img {
/* Just in case there are inline attributes */
width: 100% !important;
height: auto !important;
border: 1px solid #777;
opacity: 0.85;
}

这是一个显示问题的 fiddle 示例:https://jsfiddle.net/es7hLuq4/

如果将鼠标悬停在每张图片上,您会看到他们说“照片 1”、“照片 2”等,但顺序是这样的

1    4    7
2 5 8
3 6 9

但我希望它是这样的

1    2    3
4 5 6
7 8 9

这可以用纯 CSS 实现吗?如果没有,是否有任何干净的 jQuery 解决方案可以提供帮助?我想遍历每个图像元素并尝试检查它在哪一列,但这看起来很乱(必须在调整大小时重复)而且我也不知道如何检查元素当前在哪一列。

关于如何实现相同的自动调整网格效果但从左到右而不是从上到下列出连续元素的任何想法?

最佳答案

我已经通过为每个图像使用矩阵索引解决了这个问题。第一个完成的版本有点粗糙,当相邻图像的纵横比截然不同时,有时会稍微偏离重新排序。

我将开发一个考虑图像尺寸的更新版本,并在完成后更新答案和 fiddle 。

我将这个 JS/jQuery 用于样本重排功能:

样本文件:https://jsfiddle.net/069f72ep/

// define currentColumnCount outside the function scope so it can be compared when window is resized
var currentColumnCount;

var attributeToIndex; // switch to src for actual, title for debug/human-readable

function rearrangePhotos(performRearrange) {
// if not performing rearragement, index titles instead of src for debug/human
attributeToIndex = (!performRearrange) ? 'title' : 'src';

// get current number of columns being displayed
currentColumnCount = $('.autofit-photo-grid').css('column-count');

// create arrays to store values in
var originalPhotoMatrix, reversePhotoMatrix, columnPerimeters;
originalPhotoMatrix = new Array();
reversePhotoMatrix = new Array();
columnPerimeters = new Array();

// loop through all the images to compare their left & top perimeters to determine their cell position
$('.autofit-photo-grid img').each(function() {
// if the left perimeter is not found in the array, its a new column
if (columnPerimeters.indexOf($(this).offset().left) == -1)
columnPerimeters[columnPerimeters.length] = $(this).offset().left; // create new columnPerimeter record

}); // end loop

// sort the columns to be in consecutive order
columnPerimeters = columnPerimeters.sort(function(a, b) {
return a - b
});

// now that we have the perimeters of each column, let's figure out which cell each photo belongs to
var colCounter = 1;
var rowCounter = 1;
// loop through all the raw image objects again
$('.autofit-photo-grid img').each(function() {
// get the current position of the image again to determine which column it's in
var currentOffset = $(this).offset().left;
// get the column based on the position of the current image
var currentCol = columnPerimeters.indexOf(currentOffset) + 1;

// if we encounter a new column, reset and increment our col/row counters accordingly
if (currentCol != colCounter) {
colCounter++;
rowCounter = 1;
}

// assign the matrix index to a data attr on the img
$(this).attr('data-matrix-key', rowCounter + ',' + colCounter);
// assign original and reverse matrix values
originalPhotoMatrix[rowCounter + ',' + colCounter] = $(this).attr(attributeToIndex);
reversePhotoMatrix[colCounter + ',' + rowCounter] = $(this).attr(attributeToIndex);

// increment the row counter before restarting the loop
rowCounter++;
}); // end loop



// sort the original matrix by key so that it's in the proper order before generating key list for
// final version of reversePhotoMatrix
originalPhotoMatrix = ksort(originalPhotoMatrix);

// assign matrix id by looping through existing matrix keys/indexes
var availableKeys = new Array(); // create array to store keys/indexes in
for (var key in originalPhotoMatrix) { // loop through the keys and contents of the original matrix
availableKeys[availableKeys.length] = key; // add this key to the list of available keys
} // end loop


// generate the new photo matrix based on the next available key from the old matrix
var c = 0; // intialize counter
reversePhotoMatrix = new Array(); // clear array to prevent any unused/leftover/nonexistent values from lingering
// loop through the images....AGAIN
$('.autofit-photo-grid img').each(function() {
// determine the next available matrix key/index based on the current counter value
var nextAvailableKey = availableKeys[c];
// reassign new key/index and value to reverse photo matrix
reversePhotoMatrix[nextAvailableKey] = $(this).attr(attributeToIndex);

c++; // increment the counter before restarting the loop
}); // end loop



// finally, loop through all the original matrix items and grab the corresponding item from the reverse matrix
console.log("\n\nRearrange ready\n\n"); // just a console comment
// loop through all keys and values in the original matrix
for (var key in originalPhotoMatrix) {
if (!performRearrange) // if performRearrange=false, only display console output
console.log('Swap ' + originalPhotoMatrix[key] + ' with ' + reversePhotoMatrix[key]);
if (performRearrange) // if performRearrange=true, perform the actual rearrangement
$('.autofit-photo-grid img[data-matrix-key="' + key + '"]').attr(attributeToIndex, reversePhotoMatrix[key]);
} // end loop


} // end rearrangePhotos function



// This is just to attach the click event to the text/link at the top
$('.rearrange-link').click(function() {
// on the first click, call rearrangePhotos(performRearrange=false)
rearrangePhotos(false);
// Now change the link text to instruct the next click
$(this).text('Now rearrange them');
// Now attempt to rearrange the actual photo sources with rearrangePhotos(performRearrange=true)
$(this).click(function() {
rearrangePhotos(true);
});
});

// Run the rearrangePhotos() function everytime the window size is adjusted
$(window).resize(function() {
if ( currentColumnCount != $('.autofit-photo-grid').css('column-count') )
rearrangePhotos(false);
});

关于javascript - CSS 列(照片网格)从左到右而不是从上到下显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35735877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com