Javascript 缩放或裁剪背景图像,而不是拉伸它

Javascript 缩放或裁剪背景图像,而不是拉伸它,javascript,jquery,html,css,image,Javascript,Jquery,Html,Css,Image,我有一个背景图像,它占据了整个浏览器窗口的宽度。 这个背景图像应该有一个固定的高度,比如说500像素 我试图使这幅图像在纵横比接近浏览器窗口时尽可能完整,然后对零件进行缩放或裁剪 而不是在浏览器窗口变大时拉伸图像或仅显示图像的一侧 以下是我想要得到的一个例子: 以下是它目前的工作原理: 下面是当前的HTML代码: 引导示例 添加背景尺寸:封面仅此而已 正如@theblackgigant所说,您必须将背景大小属性设置为“在此处输入代码”封面。 您还可以将背景位置:底部中心设置为具有正确的行为

我有一个背景图像,它占据了整个浏览器窗口的宽度。 这个背景图像应该有一个固定的高度,比如说500像素

我试图使这幅图像在纵横比接近浏览器窗口时尽可能完整,然后对零件进行缩放或裁剪 而不是在浏览器窗口变大时拉伸图像或仅显示图像的一侧

以下是我想要得到的一个例子:

以下是它目前的工作原理:

下面是当前的HTML代码:


引导示例

添加
背景尺寸:封面
仅此而已


正如@theblackgigant所说,您必须将
背景大小
属性设置为“
在此处输入代码”
封面。 您还可以将
背景位置:底部中心设置为具有正确的行为

您的代码应该是这样的:

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://bootswatch.com/bower_components/bootstrap/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://bootswatch.com/cosmo/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>

<body>
  <div class="navbar navbar-default navbar-fixed-top ">
    <div class="container">
      <div class="navbar-header">
        <a href="#" class="navbar-brand">website</a>
        <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#navbar-main">
          <span class="icon-bar"></span>
        </button>
      </div>
    </div>
  </div>


  <div style="width:100vw; height: 677px; background-image: url(https://upload.wikimedia.org/wikipedia/commons/8/80/Paul_Gauguin_088.jpg); background-repeat: no-repeat;background-size:cover">
  </div>

引导示例