简体   繁体   English

有没有一种方法可以使背景图像自动变宽,全高,高度滚动,无宽度滚动

[英]Is there a way to make background image auto width, full height, height scroll, no width scroll

I want to place a picture to background thus it was full screen, the width of image should change to body width and no horizontal scroll. 我想将图片放置在背景上,因此它是全屏的,图像的宽度应更改为主体宽度,并且没有水平滚动。 On the other hand, I want height be with ratio to width (no distortion) and had vertical scroll. 另一方面,我希望高度与宽度成比例(不失真)并具有垂直滚动。 I am now doing: 我现在在做:

html, body {
  margin: 0px;
  padding: 0px;
}
body
{
    background-color: #767676;

}
#wrapper {
    position: absolute;
    min-height: 100%;
    width: 100%;
    background: url('back.jpg') no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    border: 0;
    background-size:100% auto;
}

But it shrink height and no scroll. 但是它缩小了高度并且没有滚动。

Why not setting image background in body ? 为什么不在body设置图像background

body {
    background: url(back.jpg) no-repeat fixed center center;
    background-size: 100%;
}

Here's a fiddle ilustrating the effect: http://jsfiddle.net/Kqkyb/2/ 这是一个摆弄效果的小提琴: http : //jsfiddle.net/Kqkyb/2/

EDIT : 编辑
I'm not sure if I got the scroll idea from your question correctly but for now I changed it to fixed so the image will always stay at the top of the viewport no matter where the page is scrolled to. 我不确定我是否从您的问题中正确地获得了滚动提示,但现在我将其更改为“ fixed因此无论页面滚动到何处,图像始终位于视口顶部。
EDIT2 : 编辑2
Another update. 另一个更新。 I examined your CSS more precisely and noticed that you wanted the image to be positioned in center - both in vertical and horizontal axis. 我更精确地检查了CSS,发现您希望将图像放置在中心(垂直轴和水平轴)。 I corrected it in the code as well as in the fiddle. 我在代码以及小提琴中都对其进行了纠正。

Restless night and, finally, i've solved exactly the same trouble. 不安的夜晚,最后,我解决了同样的麻烦。 I combined two ideas: 我结合了两个想法:

  1. Scale a div to fit in window but preserve aspect ratio 缩放div以适合窗口但保留宽高比
  2. and well-known techniques, i mean first one from here http://css-tricks.com/perfect-full-page-background-image/ 和众所周知的技术,我的意思是这里的第一个http://css-tricks.com/perfect-full-page-background-image/

HTML HTML

<div class='image'>
     <div class="content"></div>
<div>

and CSS 和CSS

.image{
    position: relative;
    display: inline-block;
    width: 100%; 
}
.image::after {
  padding-top: 66.66%;
  display: block;
  content: '';
}
.content {
  position: absolute;
  background-image: url('../img/image.png');
  background-size: cover;
  top: 0; bottom: 0; right: 0; left: 0;  /* follow the parent's edges */

Important! 重要!

padding-top: 66.66%;

Percentage of containing block width(!). 包含块宽度(!)的百分比。 It depends of aspect ratio of your image. 这取决于图像的纵横比。 I used imagemap 1920x1280, so 1280 / 1920 = 0.6666666. 我使用imagemap 1920x1280,所以1280/1920 = 0.6666666。 The result is absolutely responsive and scales to any resolution. 结果是绝对响应的,并且可以缩放到任何分辨率。 And the main thing - vertical scroll appears! 最主要的是-出现了垂直滚动!

 $("#wrapper").css({'height':($("body").width() * 462 / 694 +'px')});

462 / 694 - is ratio of my image. 462/694-是我的图像比例。 So now when brouser resize image (ie width), I got new width, calculate height and make it work. 所以现在当brouser调整图像大小(即宽度)时,我得到了新的宽度,计算了高度并使之工作。

But I suppose there must be some css solution. 但是我想必须有一些CSS解决方案。

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

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