在其他div旁边滚动div

时间:2018-03-12 19:19:10

标签: javascript jquery html css

我的布局是这样的:

enter image description here

滚动到底部时,左列应向下滚动(直到下一个类别)。

我的HTML看起来像这样:

    <div class="wrapper gray-bg">
   <div class="centered">
      <div class="row">
         <div class="col-xs-12 col-sm-12 col-md-5 col-lg-4 information group">
            <h1>Glas</h1>
            <hr>
            <p class="info">
            </p>
            <p>Wil je een maximale <strong>lichtinval</strong> en maximale <strong>isolatie</strong>, maar hou je ook van een <strong>strak design</strong>?&nbsp;Kies dan voor onze vlakke lichtkoepels met dubbelwandig of 3-dubbel glas. Ze zien er niet alleen geweldig uit, maar scoren op alle vlakken ongelooflijk goed.</p>
            <p></p>
            <div class="buttons">
               <a href="" class="button">bekijk referenties</a>
               <a href="/nl/professional/contact" class="button gray">Vraag offerte</a>
            </div>
         </div>
         <div class="col col-xs-12 col-sm-12 col-md-7 col-lg-8 product-container flex-row">
            <div class="col-xs-12 col-md-12 col-lg-6 flex-container">
               <div class="product">
                  <div class="image" style="background-image: url('https://exmple.com/media/cache/product_thumbnail/uploads/fa5256f2004f96761a87427be6db1e8d2e2fd983.jpeg');">
                     <span>new</span>
                  </div>
                  <h1>exmple iWindow2 ™</h1>
                  <hr>
                  <h2>Superisolerende lichtkoepel met 2-wandig glas</h2>
                  <ul class="findplace">
                     <li>Scoort erg goed qua isolatie: Ut-waarde 1,0 W/m²K</li>
                     <li>Strak, eigentijds design</li>
                     <li>Doorvalveilig</li>
                     <li>Slanke omkadering, slechts 28 mm</li>
                     <li>Vaste of opengaande uitvoering</li>
                  </ul>
                  <div class="bottom-buttons">
                     <a href="/nl/professional/product/exmple-iwindow2#prijs" class="col col-xs-3 col-md-6 col-lg-3">
                        <i class="fa fa-euro"></i>
                        <p>Prijs</p>
                     </a>
                     <a href="/nl/professional/product/exmple-iwindow2#technische_specs" class="col col-xs-3 col-md-6 col-lg-3 custom">
                        <i class="fa fa-drafting-compass"></i>
                        <p>Technische specs</p>
                     </a>
                     <a href="/nl/professional/product/exmple-iwindow2#brochures" class="col col-xs-3 col-md-6 col-lg-3">
                        <i class="fa fa-file-text"></i>
                        <p>Brochures</p>
                     </a>
                     <a href="/nl/professional/product/exmple-iwindow2#montage" class="col col-xs-3 col-md-6 col-lg-3">
                        <i class="fa fa-map"></i>
                        <p>Montage</p>
                     </a>
                  </div>
               </div>
            </div>
            <div class="col-xs-12 col-md-12 col-lg-6 flex-container">
               <div class="product">
                  <div class="image" style="background-image: url('https://exmple.com/media/cache/product_thumbnail/uploads/e2b4180f8d9109c79350817d46e9c184080e8353.jpeg');">
                     <span>new</span>
                  </div>
                  <h1>exmple iWindow3 ™</h1>
                  <hr>
                  <h2>Superisolerende lichtkoepel met 3-wandig glas</h2>
                  <ul class="findplace">
                     <li>Scoort erg goed qua isolatie: Ut-waarde 0,5 W/m²K</li>
                     <li>Strak, eigentijds design</li>
                     <li>Doorvalveilig</li>
                     <li>Slanke omkadering, slechts 55mm</li>
                     <li>Vaste of opengaande uitvoering</li>
                  </ul>
                  <div class="bottom-buttons">
                     <a href="/nl/professional/product/exmple-iwindow3#prijs" class="col col-xs-3 col-md-6 col-lg-3">
                        <i class="fa fa-euro"></i>
                        <p>Prijs</p>
                     </a>
                     <a href="/nl/professional/product/exmple-iwindow3#technische_specs" class="col col-xs-3 col-md-6 col-lg-3 custom">
                        <i class="fa fa-drafting-compass"></i>
                        <p>Technische specs</p>
                     </a>
                     <a href="/nl/professional/product/exmple-iwindow3#brochures" class="col col-xs-3 col-md-6 col-lg-3">
                        <i class="fa fa-file-text"></i>
                        <p>Brochures</p>
                     </a>
                     <a href="/nl/professional/product/exmple-iwindow3#montage" class="col col-xs-3 col-md-6 col-lg-3">
                        <i class="fa fa-map"></i>
                        <p>Montage</p>
                     </a>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
</div>

但我不知道如何确保左侧内容向下滚动,而右侧的其他内容则不会。我可以用一些javascript代码吗?

你能帮助我吗?

5 个答案:

答案 0 :(得分:9)

您可以像这样添加滚动到产品区域

.product-container {
  height: 100vh; /* limit it to the size of your window */
  overflow: auto; /* add scroll when necessary */
}

请参阅此处的示例: https://codepen.io/oriadam/pen/BrWqNw

答案 1 :(得分:1)

也许你需要粘性工具包这里是链接:http://leafo.net/sticky-kit/

答案 2 :(得分:1)

你不需要Jquery来使div可滚动。只需将以下样式放在任何具有您希望能够向下滚动的内容的容器中。滚动条将出现在div的侧面。如果您需要帮助隐藏栏或其他任何内容,请告诉我们。

overflow-y: scroll;

答案 3 :(得分:1)

如果您可以创建一个小提琴并与我们分享,那将是很棒的,这样我们就可以为您提供快速解决方案,理想情况下,在任何元素需要“固定”的情况下,它通过以下方式定位

#element {
    position:fixed;
    top:0;
    left:0
   }

以这种方式,它将被取消页面的正常流程,可以设置为

#remainingpage {
 height : 'n'px;
 overflow-y : scroll;
}

其中'n'是您要指定的高度的度量。

希望这会有所帮助,否则如果你能分享一个jsfiddle就会很棒。

答案 4 :(得分:1)

是的,如果您使用JavaScript scroll事件,这完全可行!

我们假设我们得到了这个HTML:

<div class="left col-md-3">
  <article class="sticky">
    <h2>Lorem Ipsum</h2>
    <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</p>
  </article>
</div>
<div class="right col-md-9 text-center">
     <!-- Right side Content Here -->
</div>

我们只会在left方面工作,这是简单的CSS:

.left {
  position: relative
}
.sticky {
  position: absolute
}

让我们写一些JavaScript:

// Select our `sticky` div
const stickyDiv = document.querySelector(".sticky");
/*
 * Detect the `scroll` event
 * Set the PageXOffset ( The space that we scrolled top )
 * as the `top` CSS Property value
 * This would force the `sticky` div to stay on top!
 */
window.addEventListener("scroll", function() {
   stickyDiv.style.top = window.pageYOffset + "px";
});

如果您不喜欢简单的答案,我已经在CodePen上做了一个有效的例子。

如果您需要一些解释,请告诉我们!

祝你好运!