尝试使图像适合屏幕而不扭曲时,调整大小时滚动条出现问题
我正在尝试制作全屏幻灯片效果jsFiddle 代码,但我有疑问至于如何做。从我的示例小提琴中可以看出,我希望图片尽可能大而不扭曲(适合屏幕)。我还希望它垂直和水平居中。
水平居中是在 css 中处理的,但我必须使用 JavaScript 来实现垂直居中。
我的问题是:
- 有没有更好的方法来做到这一点(例如全部在 CSS 中)?
- 第一次加载时,如果图片(在缩放之前)比视口宽,则在我的脚本计算视口高度时会出现滚动条。这意味着当我的脚本将 div 和 img 适合窗口时,底部有一个白色间隙,即滚动条的高度。我可以通过指定溢出来解决这个问题:隐藏,但这似乎是一个解决方法。有更好的办法吗?切换图像会更好吗?
- 当我调整大小以使 div 比图像宽时,我会在黑色 div 下看到一个白色部分,这会创建一个垂直滚动条。我可以再次使用溢出:隐藏来摆脱这个问题,但我不喜欢这种方法。我想知道它为什么存在以及如何摆脱它?
- 有时我可以使水平滚动条出现,并且当我调整大小时它会闪烁开/关。 Overflow:hidden 解决了这个问题,但我想要一个更干净的解决方案。
- 有没有更好的编码方法,或者我的 jQuery/Javascript 可以进一步优化吗?
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. 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.
My questions are:
- Is there a better way to do any of this (e.g. all in CSS)?
- 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. I can get around this by specifying overflow:hidden, but it seems a bit of a work-around. Is there a better way? Would toggling the image be better?
- 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. Again I can get rid of this with overflow:hidden, but I don't like that approach. I want to know why it is there and how to get rid of it?
- 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.
- Are there any better ways of coding this, or can my jQuery/Javascript be optimised any further?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道纯 html/css 中有任何跨浏览器解决方案可以完成您的要求。当浏览器决定显示/隐藏滚动条时,任何具有长宽比和流体宽度的元素都会触发“竞争条件”。当浏览器检测到其控制下的元素的这种情况时,它将强制滚动条。这也可以用 javascript 来模拟。我也遇到了这个问题,创建了一个小库:
https://github.com/ocbnet/layout
那里也是一个实现OP要求的演示:
http://www.ocbnet.ch/github/layout/demo/fullscreen.html
I'm not aware of any crossbrowser solution in pure html/css to accomplish what you've asked for. 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. I also encountered this problem and created a small library:
https://github.com/ocbnet/layout
There is also a demo that implements what the OP was asking for:
http://www.ocbnet.ch/github/layout/demo/fullscreen.html
嗯……这很棘手。我发现执行此操作的最佳方法是将您的内容实际放入单个单元格表格中,并将垂直和水平对齐属性设置为......我认为是中间和中心?但无论如何,这是可以做到的。我会尝试一下,看看是否可以一起举一个例子。
编辑:
好的,我建议的第一件事是让浏览器按比例调整大小。您不需要调整 div 的大小,只需调整图像的大小即可。您也可以让浏览器计算出垂直对齐方式,这是比计算垂直对齐方式更好的选择。这可以通过将内容放置在单个单元格表格中来完成。下面的示例代码是纯 html 和 css。您可以添加一些内容,以达到基于图像高度与窗口高度在 100% 自动和自动 100% 之间切换图像高度和宽度的效果。希望这能让您更接近您的目标。
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. 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. 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. Hopefully this gets you a little closer to your goal.
html,body,div {height:100%}
时出现奇怪的滚动条是由于边框效应,当您从
if
切换到时,滚动条就会出现。else
或反之亦然。使用 这个 jsFiddle 我永远不会得到水平滚动条,但我仍然时不时地得到垂直的。
html,body,div {height:100%}
<script>document.write('<img src="..." width="'+window.width()+'" [...] />
(document.write should be avoided however)<img src="" width="1" height="1" />
(ie, define a small default size -> no scrollbar)So yeah, the weird scrollbar on resizing happens because of border effect, the scrollbar appear when you switch from the
if
to theelse
or vice-versa.With this jsFiddle I never get horizontal scrollbar, but I still get vertical one from time to time.
您可以这样做:
都是纯 HTML 和 CSS。如果您不想在结构中使用表格,则可以使用约瑟夫的答案的替代方案。
You can do this:
It's all pure HTML and CSS. An alternative to Joseph's answer of using table if you don't want to use table in your structure.