使用纯CSS的“固定”位置的可变宽度侧边栏

时间:2017-08-30 01:07:25

标签: html css css3 flexbox

enter image description here

我想使用纯CSS来完成上图所示的内容。它必须具有以下功能:

  • image-container将包含3x4宽高比图像,该图像应填充可用的视口高度。因此,image-container将具有可变宽度,具体取决于视口的高度。
  • 当窗口滚动时,
  • image-container应固定到位。
  • content-container应该可以滚动。
  • content-container应填充image-container右边缘与视口右边缘之间的所有可用空间。

在过去,我可能已经用这样的东西(使用jquery)完成了它:

// style.css
.image-container {
   position:fixed;
   top: 0px;
   left:0px;
   bottom: 0px;
}

.image-container img {
   height: 100%;
}

// script.js
$(window).load(function() {
   var width = $('.image-container').width();
   $('.content-container').css({'margin-left': width});
});

这只能使用CSS吗?也许使用flex?

2 个答案:

答案 0 :(得分:1)

css解决方案

使用flex

html,
body {
  height: 100%;
  overflow: hidden;
}
html {
  background-color: #000;
}
.body {
  margin: 0;
  width: 100%;
  height: 100%;
  background-color: #333;
  color: #999;
}
.container-wrapper {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: flex-start;
  height: 100%;
}
.image-container {
  background-size: contain;
  text-align: center;
  height: 100%;
}
.image-container img {
  height: 100%;
  width: auto;
}
.content-container {
    height: 100%;
    display: flex;
    flex-direction: column;
}
.content-inner-container {
    flex-grow: 1;
    overflow-y: auto;
    padding: 20px 20px 40px 20px;
}
<div class="body">
  <div class="container-wrapper">
  <div class="image-container">
      <img src="http://placehold.it/750x1334">
    </div>
    <div class="content-container">
      <div class="content-inner-container">
        <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
     <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
    <p>
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    </p>
      </div>
    </div>
  </div>
</div>

解决方案

$(document).ready(function(){
var width = $('.image-container').width();
   $('.content-container').css({'width': 'calc(100% - '+width+')'});
});
html,
body {
  height: 100%;
  overflow: hidden;
}
html {
  background-color: #000;
}
.body {
  margin: 0;
  width: 100%;
  height: 100%;
  background-color: #333;
  color: #999;
}
.container-wrapper {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: flex-start;
  height: 100%;
}
.image-container {
  background-size: contain;
  text-align: center;
  height: 100%;
}
.image-container img {
  height: 100%;
  width: auto;
}
.content-container {
    height: 100%;
    display: flex;
    flex-direction: column;
}
.content-inner-container {
    flex-grow: 1;
    overflow-y: auto;
    padding: 20px 20px 40px 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="body">
  <div class="container-wrapper">
  <div class="image-container">
      <img src="https://dummyimage.com/480x640/6b676b/fff">
    </div>
    <div class="content-container">
      <div class="content-inner-container">
        <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
     <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
    <p>
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    </p>
      </div>
    </div>
  </div>
</div>

答案 1 :(得分:0)

我相信我已经完成了你的尝试。查看this fiddle

.image-container{
  height:100vh;
  width:100vh - 25%;
  border:2px solid red;
  display:inline-block;
  margin:0;
  position:fixed;
}

.content-container{
 border:2px solid green;
 margin-left: 100vh - 25%;
 display:inline-block;
 width:100%;
 height:2000px;
}

<div class="image-container"></div>

<div class="content-container">
  <p>I would like to accomplish something like what's pictured above, 
using pure CSS. It must have the following features. </p>
</div>