我需要为滑块设置一个可调整大小的背景
我尝试了background:url()
和background-size:cover
$(window).resize(function() {
$('.item > .image').css('height', $(window).height());
}).trigger('resize');
.image {
background-size:contain;
background-position:center;
background-repeat:no-repeat;
background-image: url('data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0gAAAFCCAMAAAD8PQ2mAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAABwlBMVEUAAACaVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq+aVq8AAAAHu1F8AAAAlHRSTlMAAREVIiozPkRSVmZqd3+Ik5mnq7u/zNTd6O78+MaRTAXrgBBYlgOtlFfp6gbF+fbkz72Ecl84JhT+AuzayLOgjnxoVUMxHZIJxPXeyrWfi3RgSjUhChxxxyAbg3PZMLSYQpdJTYH6JI30Lhqmzu8MBG3xbuFeEtNI4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+zZyqG1DlzOvfNMG5OV+7oplO+4wA9zlX8/9EtJ1/w8yTYt/JCpe/X9Tk+eXn33Iu5/wSQ14vndi9PVvg5LKT72te/MeJz91Lu06BNvvmtJ0pDejj3YdAud3+7LKTvfDf3XdAuL3/v+yUl/SD3WdA2T68Oh/TDl3NfBa1z6thQST/KfRO0z4/XBkP6yV25b4IW+unPBkr6ee6LoI1+8cv+74T/KvdB0E73/LqvpN/kvgfa6bfP9f6j9Lvc50Bb/f6B2yE98Yfc10Bb/fFPfz4o6S+5j4H2+uvfboX0yt9z3wLt9Y/7Xtkv6UjuU6DN/vmvvZBmlnNfAm12fmrv9wJdy30ItNtL9xchnUj+hVxAmZf/PXOzpHtznwFt95//3rjx6pO5r4DWe/7VG//LfQO03/l713Kf8Mb5P/0vRh1DAybCAAAAAElFTkSuQmCC');
}
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<div class="image"></div>
</div>
<div class="item">
<div class="image"></div>
</div>
<div class="item">
<div class="image"></div>
</div>
<div class="item">
<div class="image"></div>
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
我需要自适应的背景图片以适合台式机和移动设备
答案 0 :(得分:0)
.hero-section {
background-image: url('https://instapage.com/wp-content/uploads/2018/02/adwords-final-url-770x384.jpg');
background-size: cover;
background-repeat: no-repeat;
background-position: center;
height: 35vh;
width: auto;
}
<div class="hero-section"></div>