仅滚动正文文档中的选定内容

我想使用页面滚动效果,以便在布局中仅滚动#right 元素的内容。滚动帖子时可以在 Facebook 上看到类似的效果(在左侧面板和上栏保持原位的同时滚动帖子)。你们中的任何人都知道如何在代码中为我的 #right 容器获得相同的效果?


#header,#footer{

  width:100%;

  height:80px;

  background:blue;

}


#content{

  width:1000px;

  margin:0 auto;

  overflow:hidden;

}


#left{

  width:200px;

  float:left;

  background:green;

  margin:20px 0;

  height:100vh;

}


#right{

  float:right;

  width:calc(100% - 220px);

}


.item{

  height:80px;

  margin:20px 0;

  background:red;

}

<div id="header">

    header

</div>


<div id="content">

    <div id="left">

        left

    </div>

    <div id="right"> 

    <!-- this content is scrolling-->

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

        <div class="item">item</div>

    </div>

</div>


<div id="footer">

    footer

</div>


交互式爱情
浏览 162回答 3
3回答

守着一只汪

使用 JQuery,您可以添加样式为position:fixedand的类,top:0并使用滚动功能触发,以获得更好的侧面元素视图/效果。$(window).scroll(function(){&nbsp; &nbsp; var header_height = $('#header').height();&nbsp; &nbsp; &nbsp; if ($(this).scrollTop() > header_height) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#left').addClass('fixed');&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#left').removeClass('fixed');&nbsp; &nbsp; &nbsp; }});#header,#footer{&nbsp; width:100%;&nbsp; height:80px;&nbsp; background:blue;}#content{&nbsp; width:1000px;&nbsp; margin:0 auto;&nbsp; overflow:hidden;}#left{&nbsp; width:200px;&nbsp; float:left;&nbsp; background:green;&nbsp; margin:20px 0;&nbsp; height:100vh;}#right{&nbsp; float:right;&nbsp; width:calc(100% - 220px);}.item{&nbsp; height:80px;&nbsp; margin:20px 0;&nbsp; background:red;}.fixed{&nbsp; position:fixed;&nbsp; top:0;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="header">&nbsp; &nbsp; header</div><div id="content">&nbsp; &nbsp; <div id="left">&nbsp; &nbsp; &nbsp; &nbsp; left&nbsp; &nbsp; </div>&nbsp; &nbsp; <div id="right">&nbsp;&nbsp; &nbsp; <!-- this content is scrolling-->&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; </div></div><div id="footer">&nbsp; &nbsp; footer</div>

收到一只叮咚

position: fixed在CSS 中使用属性#header,#footer{&nbsp; width:100%;&nbsp; height:80px;&nbsp; background:blue;}#content{&nbsp; width:1000px;&nbsp; margin:0 auto;&nbsp; overflow:hidden;}#left{&nbsp; width:200px;&nbsp; float:left;&nbsp; background:green;&nbsp; /* Enter The Position Fixed Property Here! */&nbsp; position:fixed;&nbsp; margin:20px 0;&nbsp; height:100vh;}#right{&nbsp; float:right;&nbsp; width:calc(100% - 220px);}.item{&nbsp; height:80px;&nbsp; margin:20px 0;&nbsp; background:red;}<div id="header">&nbsp; &nbsp; header</div><div id="content">&nbsp; &nbsp; <div id="left">&nbsp; &nbsp; &nbsp; &nbsp; left&nbsp; &nbsp; </div>&nbsp; &nbsp; <div id="right">&nbsp;&nbsp; &nbsp; <!-- this content is scrolling-->&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="item">item</div>&nbsp; &nbsp; </div></div><div id="footer">&nbsp; &nbsp; footer</div>有一些位置错位,因为你有你的头。将此属性用于要滚动的 div。根据需要进行放置

白猪掌柜的

使用位置:固定并降低其“左”类的高度。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript