简体   繁体   English

jQuery或CSS在MVC中动态图像的固定大小

[英]jquery or css for dynamic image fixed size in mvc

Im looking for a jquery script or css that can resize all my images to 700x400 at full window height and width but still keep bootstraps image-responsive capability when resizing the browser. 我正在寻找一个jQuery脚本或css,该脚本或css可以在全窗口高度和宽度下将所有图像的大小调整为700x400,但在调整浏览器大小时仍保持引导图像响应功能。 Currently my images are displaying at there full size and I am dynamically adding the images, there output looks like this: 目前,我的图片以全尺寸显示,并且我正在动态添加图片,输出看起来像这样:

在此处输入图片说明

However it should look like this: 但是它应该看起来像这样:

在此处输入图片说明

My View looks like this: 我的视图如下所示:

            <div class="col-md-4 portfolio-item">
                <a href="@Url.Action("Details", "Post", new { urlslug = item.UrlSlug })">
                    <img class="img-responsive" src="@Html.Raw("data:image/jpeg;base64," + images.Single(image => image.Key == item.Id).Value)" alt="">
                </a>
                <h3>
                    <a href="@Url.Action("Details", "Post", new { urlslug = item.UrlSlug })">@Html.DisplayFor(modelItem => item.Title)</a>
                </h3>

                <p>@Html.DisplayFor(modelItem => item.ShortDescription)</p>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { urlslug = item.UrlSlug }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </div>

May I suggest if you want to go the CSS route, using a container for the images. 我可以建议您是否要使用图像容器来走CSS路线。 Essentially what I mean is something like the following: 本质上,我的意思是类似以下内容:

::::HTML:::: :::: HTML ::::

<!--  creates a container for your image -->
<div id="imageDiv">
     <img src='yourImageHere'>
</div>

::::CSS:::: :::: CSS ::::

/* modify the container you created to be a set width-height */
#imageDiv{
  width: 400px;
  height: 700px;
  overflow: hidden; /* essential, hides the overflow from the image */
}

#imageDiv img{
  width: 100%;  /* makes image 100% width of the container */
  position: relative; /* allows movement inside the container */
      /* OPTIONAL: use this to adjust where the 'center' starts */
      /* only useful on images that will be clipped */
  top: -__px;  
}

I'm not 100% on the code, thats from memory. 我不是代码的100%,多数民众赞成在内存上。 But essentially in English what its supposed to do is create a container for the image that you have control over the size. 但实际上,用英语来说,它应该做的是为您可以控制尺寸的图像创建一个容器。 After you create the container you add an image inside it. 创建容器后,可以在其中添加图像。

The CSS is a little bit of a trick. CSS有点技巧。 Essentially the overflow: hidden; 本质上是overflow: hidden; allows you to hide the scroll bars on the container for any pictures you input larger then your needed size. 允许您隐藏容器上的滚动条,以防止输入的图片大于所需大小。

In the next chunk we set the image to width: 100%; 在下一个块中,我们将图像设置为width: 100%; which makes the image fill the container we created (If most of your images are PORTRAIT and not LANDSCAPE I recommend changing this to height: 100%; instead, this will allow it to fill 100% of the height of the container. 这样可以使图像填充我们创建的容器(如果您的大多数图像是纵向图像而不是横向图像,我建议将其更改为height: 100%;相反,这将使其填充容器高度的100%。

The position: relative; position: relative; just allows us to move around our image inside the container we just created. 只是允许我们在刚创建的容器内移动图像。

From there we use positional elements in this case top: -__px to move the image up. 在此情况下,我们从top: -__px使用位置元素top: -__px将图像向上移动。 IF you ended up using height: 100%; 如果您最终使用height: 100%; due to portrait images, you may want to tweak images either left: or right: to get the desired look. 由于肖像图片,你可能需要调整图像或者left:right:获得所需的外观。


I hope this helps. 我希望这有帮助。 Because these elements have a set size, you WILL most likely have to recreate media queries for them, unless however you put them within a BOOTSTRAP styled col-sm-3 or some other column amount, which would automatically do the reorganizing of the elements, just not the resizing. 由于这些元素的大小是固定的,因此您很可能必须为其重新创建媒体查询,除非您将它们放在BOOTSTRAP样式的col-sm-3或其他某些列中,这将自动对元素进行重新组织,只是没有调整大小。

The nice thing is though, for the media query you'd write, you'd only need to change the width: 400px; 不错的是,对于要编写的媒体查询,您只需更改width: 400px; and height: 700px; height: 700px; of the container, all the image elements would automatically resize due to being set to 100% of its parent element, in this case #imageDiv 容器的大小,由于将所有图像元素设置为其父元素的100% ,因此该图像元素会自动调整大小,在这种情况下为#imageDiv

Hope that helps, or is what you were looking for. 希望对您有所帮助,或者是您正在寻找的东西。 It is a nice way to create a container for images, that won't distort them upon rendering if they aren't the same size. 这是为图像创建容器的一种好方法,如果它们的大小不相同,则在渲染时不会使它们变形。 I DO HOWEVER RECOMMEND USING IMAGES WITH A SIMILAR ASPECT RATIO, just makes life easier. 我确实建议您使用具有类似纵横比的图像,这只会使生活更轻松。 Please leave a comment if it isn't and maybe I'd be able to help more. 如果没有,请发表评论,也许我可以提供更多帮助。 If it is what you wanted, glad I could help! 如果这是您想要的,很高兴我可以提供帮助!


EDIT 编辑

Bootstrap Responsive Images may be useful in styling the images. Bootstrap响应式图像在样式化图像时可能很有用。 But you'd still need to alter the size of the parent element in CSS. 但是您仍然需要更改CSS中父元素的大小。 So kind of moot. 真有意思。 However, using % or vh/vw based widths and heights for the parent container would make it all resize automatically. 但是,对父容器使用基于vh / vw的宽度和高度将使其全部自动调整大小。

If you are unfamiliar with 'vh/vw' its a new unit of measure in CSS3, essentially it means viewport height/width. 如果您不熟悉“ vh / vw”,它是CSS3中的新度量单位,从本质上讲,它意味着视口的高度/宽度。 So 100vh would set something to be the full height of the screen, without knowing the actual pixel size of it(sounds alot like %, I know, but it comes in really handy when say trying to make a sidebar or a body at that height, you don't need to worry about the %, position:absolute or container hacks to fill the window. It was made to dynamically resize as well, but, as it stands I believe only Firefox dynamically resizes vh/vw elements. 因此100vh会将某东西设置为屏幕的整个高度,而不知道它的实际像素大小(我知道很多声音,例如%,但是当要尝试以该高度制作侧边栏或主体时,它非常方便,您不必担心%,position:absolute或容器hack来填充窗口,它也可以动态调整大小,但是就目前而言,我相信只有Firefox可以动态调整vh / vw元素的大小。

https://css-tricks.com/viewport-sized-typography/ https://css-tricks.com/viewport-sized-typography/

If I understand the question correctly, you want to have an image of any size displayed in a 700 x 400 element at maximum width or height. 如果我正确理解了这个问题,则希望以700 x 400元素的最大宽度或高度显示任何尺寸的图像。

  • The best way to do that would be to resize the images dynamically on the server. 最好的方法是在服务器上动态调整图像大小。 Since you're using ASP, this Stack Overflow thread addresses that 由于您使用的是ASP, 因此该Stack Overflow线程可以解决以下问题:
  • The easiest way to do this in the browser, if you're going to require users to use modern (IE 9+) browsers, is to use the CSS Background Size property , which allows you various ways to fill the element 如果要要求用户使用现代(IE 9+)浏览器,则在浏览器中执行此操作的最简单方法是使用CSS Background Size属性 ,该属性允许您通过多种方式填充元素
  • If you really want to do it programmatically, you can use a JQuery plugin like Image Resize 如果您确实想以编程方式进行操作,则可以使用JQuery插件,例如Image Resize

Best of luck! 祝你好运! Let us know what you wind up doing. 让我们知道您最终要做什么。

I would use a photo editing program to resize the images to 700x400. 我将使用照片编辑程序将图像调整为700x400。 Your images are all different heights and widths so when you resize them automatically they will either get cut off or stretched out. 您的图片的高度和宽度都是不同的,因此当您自动调整它们的大小时,它们会被切除或拉长。 It would be better if you made a new file in an image editor that was 700x400 and resized your images how you want them. 如果您在700x400的图像编辑器中制作了一个新文件,并根据需要调整图像大小,则效果会更好。 That way it will work just how you have it set up in bootstrap right now, with the responsive image resizing and they will never be larger than your maximum width and height of 700x400. 这样一来,它就可以像您现在在自举程序中进行设置一样工作,并具有自适应图像大小,并且它们永远不会大于您的最大宽度和高度700x400。

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

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