简体   繁体   English

如何允许用户设置网格图像布局中的列数?

[英]How do I allow the user to set the number of columns in a grid image layout?

What are some ways I could achieve this layout? 我可以通过哪些方式实现此布局? I'm going to have a grid of images. 我要准备一张图像网格。 Each image will be the same size. 每个图像将具有相同的大小。 I'd like to scale the images up and down based on a user setting which defines the number of columns in the grid. 我想根据定义网格中列数的用户设置来放大和缩小图像。 The user setting will be stored in HTML5 local storage. 用户设置将存储在HTML5本地存储中。

I've looked at Masonry, but I wonder if that might be overkill since all the images are the same size and the grid will be uniform. 我看过砌体,但我想知道这是否过大,因为所有图像的大小相同并且网格将统一。 Masonry lets me define the column width, which can get me where I need to be, but I'd love something that let me define the number of columns directly. 石工让我定义列宽,这可以使我到达需要的位置,但是我很喜欢可以直接定义列数的东西。 I've also considered using Flexbox for the layout, but I'm not sure how to then change the number of columns with a user-selectable setting. 我还考虑过使用Flexbox进行布局,但是我不确定如何通过用户可选的设置更改列数。 I want to go as lightweight in terms of development overhead as possible with this solution. 我想通过此解决方案尽可能减少开发开销。

Here are some images to help visualize what I want. 以下是一些图像,可帮助可视化我想要的东西。 Here, the user has selected a 5-column layout. 在此,用户已选择5列布局。

5列布局

Here are 4- and 3-column layouts. 这是4列和3列的布局。

4列布局

3列布局

If your number of options is known, then you can create a container style for each scenario. 如果您知道选项数量,则可以为每个方案创建一个容器样式。

.five-col .col {
 float: left;
 width: 20%;  
}

And structure your html as... 并将您的html构造为...

<ul class="five-col">
   <li class="col"> image </li>
   ...
</ul>

Create a new container style for each column layout, eg. 为每个列布局创建一个新的容器样式,例如。 .four-col .eight-col and just toggle between them. .four-col .eight-col然后在它们之间切换。

This does require you to know ahead of time the number of columns you will allow them to choose between, but an 'n' solution might allow the user to create something silly like 20+ columns so vetting the options ahead of time has it's advantages. 这确实需要您提前知道允许它们选择的列数,但是“ n”解决方案可能允许用户创建像20多个列这样的愚蠢内容,因此提前审核选项具有其优势。

If you are interested in a framework that gives you something similar, check out http://unsemantic.com/ or http://960.gs/ . 如果您对提供类似功能的框架感兴趣,请访问http://unsemantic.com/http://960.gs/

Id do it like this: 这样做是这样的:

*
{
     -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

body
{
    margin:0;
    padding: 0px;
}

.gallery
{
    padding: 15px;
    margin:0;
    list-style: none;
    overflow: hidden;
}

.gallery > li
{
    margin: 0px;
    padding: 2.5px 5px;
    float: left;
    text-align: center;
}

.gallery > li img
{
    max-width: 100%;
}

.gallery.items2 > li
{
    width: 50%;
}

.gallery.items3 > li
{
    width: 33.333%;
}

.gallery.items4 > li
{
    width: 25%;
}

.gallery.items5 > li
{
    width: 20%;
}

.gallery.items6 > li
{
    width: 16.666%;
}

Some jQuery (or JS) 一些jQuery(或JS)

$("#grid-range").change(function(){
    var griditems = $(this).val();
    $("#gallery").removeClass("items2 items3 items4 items5 items6");
    $("#gallery").addClass("items"+griditems);
});

This HTML: 此HTML:

<input type="range" min="2" id="grid-range" max="6" value="2" step="1" name="power" />

<ul id="gallery" class="gallery items2">
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
    <li><img src="http://www.placehold.it/200x200" /></li>
</ul>

Here is a demo: http://jsfiddle.net/eEnkf/4/ 这是一个演示: http : //jsfiddle.net/eEnkf/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM