当我使用具有background-image
属性的cover
图像时,我会获得具有完美比率的全屏图像。是否可以使用img
获得相同的结果,而不使用javascript?
我想创建一个视差背景。当用户滚动下面的内容时,图像会在图像上滑动。
问题在于图片的大小,我添加了width: 100%
,但它不能用作background-size: cover
。我应该增加宽度吗?什么是最佳做法?
The JSFiddle version is available here.
$(document).ready(function(){
function contentHeight() {
var wheight = $(window).height();
$('#content').css({
'margin-top' : wheight
});
}
contentHeight();
$(window).resize(function(){
contentHeight();
});
});
html, body {
margin: 0;
}
#image {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
#image img {
width: 100%;
}
#content {
background: #ccc;
position: relative;
z-index: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="image"><img src="http://www.planwallpaper.com/static/images/canberra_hero_image.jpg"></div>
<div id="content">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
</div>
答案 0 :(得分:4)
如果您想使用background-size: cover
标记模仿img
,可以使用css属性object-fit
和值cover
来完成此操作。 You can check support here - 注意它是有限的(目前没有MS支持)。
<强> DEMO 强>
您只需将background-size: cover
img
和height
设置为width
,就可以使用纯css模拟auto
;这将保留img
的宽高比。但它不会使img
完美,因此一个简单的解决方法是添加一些规则以确保它位于父容器的中间;确保父母有overflow: auto
。
<强> DEMO 强>
您可以在此处看到background-size: cover
与第二种技术之间的比较: COMPARISON
对于此特定img
,第二种技术是可以接受的,但如果图像大小发生任何变化,则会出现问题,as you can see in this fiddle。
答案 1 :(得分:3)
我认为没有JS的唯一跨浏览器替代方案是内联svg
并使用<image>
:
<svg viewbox="0 0 800 531" preserveAspectRatio="xMidYMid slice">
<image xlink:href="/path/image.jpg" width="800" height="531"/>
</svg>
svg {
width: 100%;
height: 100vh;
}
svg
可以是任意大小,但viewbox
必须定义<image>
的原始尺寸。
诀窍在于preserveAspectRatio
属性:
我还使用常规图像的宽高比和一些变换为此编写了一个小脚本:
<div id="wrap">
<img id="layer" src="/path/image.jpg" alt="">
</div>
#wrap {
overflow: hidden;
}
#wrap img {
position: relative;
}
.wide {
top: 50%;
-webkit-transform: translateY(-50%);
transform: translateY(-50%)
}
.tall {
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%)
}
$(function() {
var parent = $('#wrap'),
tableau = $('#layer'),
proportion = 800/531; // aspect ratio of the image
$(window).on('load resize', coverSpace);
function coverSpace() {
var range = parent.width(),
term = parent.height(),
broad = term*proportion,
aerial = range/proportion;
if (range/term >= proportion) tableau.css({width: '100%', height: aerial}).attr('class', 'wide');
else tableau.css({width: broad, height: term}).attr('class', 'tall');
}
});
答案 2 :(得分:0)
当然是:
使用此CSS
html, body {
margin: 0;
overflow-x:hidden;
}
#image {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
#image img {
height:100%;
width:auto;
}