简体   繁体   English

通过Java脚本更改Div宽度

[英]Changing Div Width via Javascript

I'm making a website for my art gallery. 我正在为我的美术馆建立一个网站。 Part of what I need to do is display images to the viewers in a way in which they can view them. 我需要做的部分工作是以向观众显示图像的方式来显示它们。 And I want to do this without reducing the quality of the images, or having to save all of my images in many different sizes to cater to every user. 我想做到这一点而又不降低图像质量,也不必保存所有不同尺寸的图像以迎合每个用户。

So, I've made a Javascript function to resize my images to fit completely on the viewer's screen. 因此,我制作了一个Javascript函数来调整图像大小,使其完全适合查看器的屏幕。 My code looks like 我的代码看起来像

<img src="[image.png]" onload="setGoodHeight(this);">

where the function setGoodHeight(element) is defined as: 函数setGoodHeight(element)定义为:

function setGoodHeight (element) {
    if(window.innerHeight-50 < element.height) {
        var h = element.height;
        var w = element.width;
        element.height = window.innerHeight - 50;
        element.width = w * element.height / h;
    }
    if (window.innerWidth-100 < element.width) {
        var h = element.height;
        var w = element.width;
        element.width = window.innerWidth - 100;
        element.height = h * element.width / w;
    }   
}

In shorthand, this first checks whether the image is higher than the screen it's trying to be displayed on, and if it is (it usually is) the image is resized to fit comfortably on the screen. 简而言之,它首先检查图像是否高于试图显示的屏幕,如果是(通常是),则将图像调整大小以使其舒适地适合屏幕。 Then it checks if, after this, the image is wider than the screen, and if so it shrinks it further. 然后,在此之后,它检查图像是否比屏幕宽,如果是,则将其进一步缩小。 I have verified that this code works. 我已验证此代码有效。

However, the image is contained within a class called .post I want the post area to wrap to that of the image, at least in width, and so at the end of my javascript function, I added this code: 但是,图像包含在名为.post的类中,我希望发布区域至少以宽度包装到图像的区域,因此在我的javascript函数末尾,我添加了以下代码:

element.parentNode.width = element.width + 40;

But the post doesn't resize itself. 但是帖子不会自动调整大小。 For reference, the code on the actual webpage concerning this can be boiled down to 作为参考,可以将与此相关的实际网页上的代码简化为

<div class="post">
    <img src="[image.jpg]" onload="setGoodHeight(this);">
</div>

and if you need to look around it a little more it can be found at this link . 如果您需要查看更多信息,可以在此链接中找到它。

How about a pure CSS solution, it will also update magically if the user resizes their browser. 纯CSS解决方案怎么样,如果用户调整浏览器的大小,它也会神奇地更新。

 html, body, #fullscreen { margin: 0; padding: 0; width: 100%; height: 100%; } #fullscreen { background: url('http://www.nathanrouse.org/wp-content/uploads/2014/01/CrashTestDummy.jpg') center center no-repeat; background-size: cover; } 
 <div id="fullscreen"></div> 

Check the doc for background-size . 检查文档的background-size There are other values like "contain" that might suit you better. 还有其他一些值,例如“ contain”,可能更适合您。

I think you're looking for 我想你在找

item.naturalHeight
item.naturalWidth

I was using these in a function to set the max-height & max-width 我在功能中使用这些来设置最大高度和最大宽度

function imageLoad(item) {
     $(item).attr("max-height", item.naturalHeight);
     $(item).attr("max-width", item.naturalWidth);
}

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

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