简体   繁体   English

尝试使图像适合屏幕而不变形时调整大小时滚动条的问题

[英]Problems with scrollbars on resize when trying to fit image to screen without distorting

I'm trying to make a full screen slideshow kind of effect jsFiddle code here , but I am having doubts as to how to do it. 我正在尝试在此处制作全屏幻灯片效果jsFiddle代码 ,但是我对如何执行此操作有疑问。 As can be seen from my example fiddle, I want the picture to be as big as possible without distorting it (fit to screen). 从我的示例小提琴中可以看出,我希望图片尽可能大而不失真(适合屏幕)。 I also want it to be centred vertically and horizontally. 我也希望它在垂直和水平方向上居中。

Horzontal centring it is taken care of in css, but I have had to use JavaScript for vertical centring. Horzontal居中已在CSS中解决,但我不得不使用JavaScript进行垂直居中。

My questions are: 我的问题是:

  1. Is there a better way to do any of this (eg all in CSS)? 有没有更好的方法可以做到这一点(例如CSS中的全部)?
  2. On first load, if the picture (before it has been scaled) is wider than the viewport, a scrollbar is present while my script calculates the height of the viewport. 第一次加载时,如果图片(在缩放之前)比视口宽,则在我的脚本计算视口高度时会出现滚动条。 This means that when my script fits the div and img to the window, there is a white gap at the bottom, that is the height of the scroll bar. 这意味着当我的脚本使div和img适合窗口时,在底部有一个白色的缝隙,即滚动条的高度。 I can get around this by specifying overflow:hidden, but it seems a bit of a work-around. 我可以通过指定overflow:hidden来解决此问题,但这似乎有点解决方法。 Is there a better way? 有没有更好的办法? Would toggling the image be better? 切换图像会更好吗?
  3. When I resize so that the div is wider than the image, I get a white section under the black div, which creates a vertical scrollbar. 当我调整大小以使div比图像宽时,我在黑色div下得到一个白色部分,这将创建一个垂直滚动条。 Again I can get rid of this with overflow:hidden, but I don't like that approach. 同样,我可以使用overflow:hidden摆脱它,但是我不喜欢这种方法。 I want to know why it is there and how to get rid of it? 我想知道为什么它在那里以及如何摆脱它?
  4. Sometimes I can make a horizontal scrollbar appear and as i resize it flashes on/off. 有时,我可以使水平滚动条出现,并在我调整大小时闪烁开/关。 overflow:hidden fixes this, but I want a cleaner solution. 溢出:隐藏解决此问题,但我想要一个更清洁的解决方案。
  5. Are there any better ways of coding this, or can my jQuery/Javascript be optimised any further? 有没有更好的编码方式,或者可以进一步优化我的jQuery / Javascript?

I'm not aware of any crossbrowser solution in pure html/css to accomplish what you've asked for. 我不知道任何纯html / css中的跨浏览器解决方案都能完成您所要求的。 Any element with an aspect ratio and a fluid width will trigger a "race condition" when the browser decides to show / hide the scrollbar. 当浏览器决定显示/隐藏滚动条时,任何具有长宽比和流体宽度的元素都将触发“竞赛条件”。 A browser will force a scrollbar when it detects this situation with elements under its control. 当浏览器在其控制下的元素检测到这种情况时,它将强制滚动条。 This can also be simulated with javascript. 这也可以用javascript模拟。 I also encountered this problem and created a small library: 我也遇到了这个问题,并创建了一个小库:

https://github.com/ocbnet/layout https://github.com/ocbnet/layout

There is also a demo that implements what the OP was asking for: 还有一个演示可以实现OP的要求:

http://www.ocbnet.ch/github/layout/demo/fullscreen.html http://www.ocbnet.ch/github/layout/demo/fullscreen.html

Well... that ones tricky. 好吧...那很棘手。 The best way I've found how to do this is to actually put your content in a single cell table and set the vertical- and horizontal-align properties to.... middle and center I think? 我找到方法的最佳方法是将内容实际放在单个单元格表中,并将垂直和水平对齐属性设置为...。 But anyhow, it can be done. 但是无论如何,这是可以做到的。 I'll play around with it and see if I can get an example together. 我将尝试使用它,看看是否可以得到一个示例。

EDIT: 编辑:

Ok so the first thing I would advise is to let the browser proportionately resize things. 好的,所以我建议的第一件事是让浏览器按比例调整大小。 You don't need to resize the div, just the image. 您不需要调整div的大小,只需调整图像的大小即可。 You can let the browser figure out the vertical alignment as well, which is a much better option than calculating it. 您也可以让浏览器找出垂直对齐方式,这比计算它要好得多。 This can be done by placing the content in a single cell table. 可以通过将内容放在单个单元格表中来完成。 The example code below is pure html and css. 下面的示例代码是纯HTML和CSS。 You can add something to the effect that you already have to switch the image height and width between 100% auto and auto 100% based on img height vs window height. 您可以添加一些效果,您已经必须根据img高度与窗口高度在100%自动和100%自动之间切换图像的高度和宽度。 Hopefully this gets you a little closer to your goal. 希望这可以使您更接近目标。

<table style="background-color:#ddd; width:100%; height:100%">
  <tr>
    <td align="center">
      <div id="fulldiv">
        <img style="width:100%" id="photo" src="http://assets.perfectlytimedphotos.com/hashed_silo_content/silo_content/21003/resized/coke.jpg">
      </div>
    </td>
  </tr>
</table>
  1. Yes (for the 100% height part) : html,body,div {height:100%} 是(对于100%高的部分): html,body,div {height:100%}
  2. overflow:hidden seems nice to me. 溢出:隐藏对我来说似乎很好。 Other possible solutions : 其他可能的解决方案:
    1. <script>document.write('<img src="..." width="'+window.width()+'" [...] /> (document.write should be avoided however) <script>document.write('<img src="..." width="'+window.width()+'" [...] /> (但是应避免document.write)
    2. <img src="" width="1" height="1" /> (ie, define a small default size -> no scrollbar) <img src="" width="1" height="1" /> (即,定义小的默认大小->没有滚动条)
    3. @Gerben suggestion : css max-width (best) @Gerben建议:CSS最大宽度(最佳)
  3. Seems to be the space the horizontal scrollbar would take. 似乎是水平滚动条占用的空间。 I only get this problem at a specific size+ratio too. 我也只在特定的大小+比率下遇到此问题。
  4. This is when the pictureRatio is equal or pretty close to the fullDivRatio. 这是当pictureRatio等于或非常接近fullDivRatio时。 I can't find hoo with overflow:hidden. 我找不到带有溢出:隐藏的hoo。
  5. No idea, seems good enough IMO. 不知道,IMO似乎足够好。

So yeah, the weird scrollbar on resizing happens because of border effect, the scrollbar appear when you switch from the if to the else or vice-versa. 所以,是的,由于边框效应,发生了重新调整大小的怪异滚动条,当您从if切换到else或相反时,滚动条会出现。

With this jsFiddle I never get horizontal scrollbar, but I still get vertical one from time to time. 有了这个jsFiddle,我永远都不会获得水平滚动条,但是我仍然会不时获得垂直滚动条。

You can do this: 你可以这样做:

<div>
    <img id="photo" src="">
</div>
div {
width: 100%;
height: 100%;
display: table-cell;
text-align: center;
vertical-align: middle;
}
img {
position: relative;
}

It's all pure HTML and CSS. 都是纯HTML和CSS。 An alternative to Joseph's answer of using table if you don't want to use table in your structure. 如果您不想在结构中使用表,可以替代约瑟夫使用表的答案。

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

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