【发布时间】:2015-05-02 15:35:56
【问题描述】:
我使用Slick 作为全宽背景滑块。我只想要一个覆盖整个背景的幻灯片。我的问题是图像似乎只能自动调整到页面的宽度。这意味着如果浏览器变得太窄,图像将无法覆盖整个高度。我想知道是否有一些好的方法可以调整图像大小以始终覆盖背景。我希望图像根据浏览器的纵横比水平或垂直溢出。
显示滑块的 div 样式为:
#background {
position: fixed;
z-index: -2000;
top:0;
left:0;
width:100%;
}
我有一个似乎可以工作的 jquery 解决方案,但我真的不知道这是否是一个好的解决方案:
$($('#background').find('img').first()).on('load', function(){
var bgWidth =
$(window).height() /
$('#background').find('img').first().height() *
$('#background').find('img').first().width();
// Initiate background slider
$('#background')
.slick({
dots: false,
autoplay: true,
autoplaySpeed: 6000,
pauseOnHover: false,
fade: true,
arrows: false,
mobileFirst: true,
})
.css({
'height':'100%',
'min-width': bgWidth,
});
});
是否有更好的解决方案,或者这是否足够好?
【问题讨论】:
标签: jquery