javascript - MVC 图像根据文件夹中的数量调整大小

标签 javascript c# html css asp.net-mvc

我最近开始使用 MVC 5,我已经坚持了一段时间 我想做的是根据文件夹中的图像数量调整图像大小 例如
如果一张图片的最大宽度为 100%
如果两个图像的最大宽度为 50%
如果三个图像的最大宽度为 33%
等等 但我也希望根据页面大小调整图像大小
我尝试过像

这样的样式

<div style="position: relative; max-width: 24%; background: White; vertical-align: top; display: inline-block; *display: inline; zoom: 1">
  <img style="margin: 10%; height: 80%; width: 80%;" src="~/Images/Example1.jpg" />
</div>
如果只使用了 4 张图像,这很好,但我想根据使用的图像数量重新调整大小 我也尝试过像

这样的foreach循环

@foreach (var imgPath in Directory.GetFiles(Server.MapPath("~/Images/ExampleFolder"), "*.jpg")) { var img = new FileInfo(imgPath);

<div style="position: relative; max-width: ; background: White; vertical-align: top; display: inline-block; *display: inline; zoom: 1">
  <img style="margin: 10%; height: 80%; width: 80%;" src="@Url.Content(String.Format(" ~/Images/ExampleFolder/{0} ", img.Name))" />
</div>


}

最佳答案

您可以尝试这样的操作(未测试):

@{
    var files = Directory.GetFiles(Server.MapPath("~/Images/ExampleFolder"), "*.jpg");
    if (files.Length > 0) 
    {
        double width = 100d / (double)files.Length;
        foreach (var imgPath in files)
        {
            var img = new FileInfo(imgPath);

            <div style="position: relative; background: White; vertical-align: top; display: inline-block; *display: inline; zoom: 1">
                <img style="margin: 10%; height: 80%; width: @(width)%;" src="@Url.Content(String.Format(" ~/Images/ExampleFolder/{0} ", img.Name))" />
            </div>


        }
    }
}

关于javascript - MVC 图像根据文件夹中的数量调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28408377/

相关文章:

javascript - 使用 javascript onclick 激活 Bootstrap 模式

javascript - 如何重写Jquery动态选择框脚本,实现5个选择框?

javascript - 使用 node.js 从麦克风检测音频

javascript - 如何在 Laravel 中的我的删除按钮上开发 javascript 按摩?

c# - 在 C# 中使用 {#} 打印整数?

html - 将 Elm-Element 嵌入到现有的 HTML 项目中

javascript - 用逗号替换空格和空格,用JS中的连字符?

c# - 如何读取 C# app.config 文件中的多个值?

c# - Selenium ChromeDriver 和 IE 驱动程序无法使用 WIndows 7/TFS 启动

html - 修剪字符串长度忽略 HTML