简体   繁体   English

在调整大小时将图像宽度设置为窗口宽度(没有 jQuery)

[英]set image width quals to window width on resize (without jQuery)

I need some help :).我需要帮助 :)。

I have a wrapper-container with, let say 60% width on desktop devices (on mobile 100% width).我有一个包装容器,比如桌面设备上的 60% 宽度(移动设备上的 100% 宽度)。 Then I have different images on this page, they all have same width as wrapper.然后我在这个页面上有不同的图像,它们都与包装器具有相同的宽度。

Now on window resize and wanna set the width of those images equal to the window width and place this images horizontally in the middle of the page.现在调整窗口大小并希望将这些图像的宽度设置为等于窗口宽度并将这些图像水平放置在页面中间。

I know I have to deal with window resize , window width and then move images with CSS transform to the left.我知道我必须处理窗口调整大小窗口宽度,然后将带有 CSS变换的图像向左移动。 But how?但是如何? The image must then have inline-style with all these parameters.图像必须具有所有这些参数的内联样式。

Dummy:假的:

 window.addEventListener('resize', function(){ var img = document.getElementsByClassName("fullwidth"); var windowWidth = window.innerWidth; }, true);
 .wrapper { padding: 1em; } .fullwidth { max-width: 100%; height: auto; } @media (min-width: 800px) { body { background: #555; color: #fff; } .wrapper { max-width: 60%; margin: 0 auto; } }
 <div class="wrapper"> <p> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </p> <img class="fullwidth" src="http://via.placeholder.com/2000x300"> <p> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </p> </div>

Thanks谢谢

BTW: I need this kind of layout for Wordpress theme, since I didn't found any solution for WP to set images to full width, wich are placed in a post, while there is an wrapper-container.顺便说一句:我需要这种 Wordpress 主题的布局,因为我没有找到任何解决方案让 WP 将图像设置为全宽,将图像放置在帖子中,而有一个包装容器。

Here is a solution using plain js.这是使用普通js的解决方案。 You could improve it by using jQuery or ES6.您可以使用 jQuery 或 ES6 改进它。 For the fullscreen image, I set the width to calc(100vw - 2em) which corresponds to the full size of the viewport width minus the padding set on the wrapper.对于全屏图像,我将宽度设置为calc(100vw - 2em) ,它对应于视口宽度的全尺寸减去包装器上设置的填充。

The handleResize() method does the following: get the wrapper width and subtract the window width. handleResize()方法执行以下操作:获取包装器宽度并减去窗口宽度。 Divide that by two and you should obtain a negative offset which is then applied to the image's marginLeft style attribute.将其除以二,您应该获得一个负偏移量,然后将其应用于图像的marginLeft样式属性。 This pushes the image to the left edge of the window and the css calc() above handles the expansion.这会将图像推到窗口的左边缘,并且上面的 css calc()处理扩展。

Hope this helps.希望这可以帮助。

 var images = document.getElementsByClassName("fullwidth"); var wrapper = document.getElementById("wrapper"); handleResize = function() { var displacement = (wrapper.offsetWidth - window.innerWidth) / 2; for (i = 0; i < images.length; i++) { images[i].style.marginLeft = displacement + "px"; } } window.addEventListener('resize', function() { handleResize(); }, true); handleResize();
 html, body { margin: 0px; padding: 0px; } #wrapper { padding: 1em; } .fullwidth { width: calc(100vw - 2em); height: auto; } @media (min-width: 800px) { body { background: #555; color: #fff; } #wrapper { max-width: 60%; margin: 0 auto; } }
 <div id="wrapper"> <p> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </p> <img class="fullwidth" src="http://via.placeholder.com/2000x300"> <p> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </p> </div>

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

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