如何使侧边栏固定时滚动像粘性
How to make sidebar fixed when scroll like sticky
好。我的侧边栏有问题。当我向下滚动到"你好,世界?"时,我想固定左侧边栏并向上滚动,侧边栏将像STICKY一样向上滚动。我不想使用插件。我需要一些短代码。这只是用来做的。帮我拉小提琴。谢谢
*/i want use javascript.../
#header { clear:both; width: 100%; background-color: darkorange; border-top: 1px solid #d2d2d3; border-bottom: 1px solid #d2d2d3; margin: 20px 0px 20px 0px;}
h1.head {font-family: verdana; font-size: 200%;}
a { color: orange;}
.sidebar { float: left; width: 25%; height: 100%;}
#text { float: left; width: 70%;}
body { text-align:center;}
p, p a {text-align: left; font-size: 90%;}
<body>
<div id="header">
<h1 class="head"><b>Hello, world ?</b></h1>
</div>
<div id="bar-fixed">
<div class="sidebar">
<p class="bar"><a href="#">
Make this fixed when scroll</a></p>
<p class="bar"><a href="#">
Make this fixed when scroll</a></p>
<p class="bar"><a href="#">
Make this fixed when scroll</a></p>
<p class="bar"><a href="#">
Make this fixed when scroll</a></p>
<p class="bar"><a href="#">
Make this fixed when scroll</a></p>
</div>
</div>
<div id="text">
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
</div>
</body>
更新:
我想我知道你想要什么。使用Javascript更新了代码。
我正在设置一个滚动限制,超过这个限制,我将在侧边栏中添加一个名为.stickIt
的类,并将其固定在一个位置。
演示:
/* it seems javascript..*/
var topLimit = $('#bar-fixed').offset().top;
$(window).scroll(function() {
//console.log(topLimit <= $(window).scrollTop())
if (topLimit <= $(window).scrollTop()) {
$('#bar-fixed').addClass('stickIt')
} else {
$('#bar-fixed').removeClass('stickIt')
}
})
#header {
clear: both;
width: 100%;
background-color: darkorange;
border-top: 1px solid #d2d2d3;
border-bottom: 1px solid #d2d2d3;
margin: 20px 0px 20px 0px;
}
h1.head {
font-family: verdana;
font-size: 200%;
}
a {
color: orange;
}
#bar-fixed {
width: 30%;
}
#bar-fixed.stickIt {
position: fixed;
top: 0px;
}
.sidebar {
float: left;
width: 100%;
height: 100%;
}
#text {
float: right;
width: 70%;
}
body {
text-align: center;
}
p,
p a {
text-align: left;
font-size: 90%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="header">
<h1 class="head"><b>Hello, world ?</b></h1>
</div>
<div id="bar-fixed">
<div class="sidebar">
<p class="bar"><a href="#">
Make this fixed when scroll</a>
</p>
<p class="bar"><a href="#">
Make this fixed when scroll</a>
</p>
<p class="bar"><a href="#">
Make this fixed when scroll</a>
</p>
<p class="bar"><a href="#">
Make this fixed when scroll</a>
</p>
<p class="bar"><a href="#">
Make this fixed when scroll</a>
</p>
</div>
</div>
<div id="text">
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
</div>
</body>
使用position: fixed;
作为CSS中的ID bar-fixed
。
*/i want use javascript.../
#header { clear:both; width: 100%; background-color: darkorange; border-top: 1px solid #d2d2d3; border-bottom: 1px solid #d2d2d3; margin: 20px 0px 20px 0px;}
h1.head {font-family: verdana; font-size: 200%;}
a { color: orange;}
.sidebar { float: left; width: 25%; height: 100%;}
#text { float: right; width: 70%;}
body { text-align:center;}
p, p a {text-align: left; font-size: 90%;}
#bar-fixed { position: fixed; }
<body>
<div id="header">
<h1 class="head"><b>Hello, world ?</b></h1>
</div>
<div id="bar-fixed">
<div class="sidebar">
<p class="bar"><a href="#">
Make this fixed when scroll</a></p>
<p class="bar"><a href="#">
Make this fixed when scroll</a></p>
<p class="bar"><a href="#">
Make this fixed when scroll</a></p>
<p class="bar"><a href="#">
Make this fixed when scroll</a></p>
<p class="bar"><a href="#">
Make this fixed when scroll</a></p>
</div>
</div>
<div id="text">
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
<p>I want the sidebar fixed when i scroll down over the Header. and the sidebar will go up LIKE STICKY SIDEBAR</p>
</div>
</body>
跨越到2020年,现在更容易了:
#bar-fixed {
height: 100vh;
top: 10px; /* or whatever top you need */
position: -webkit-sticky;
position: sticky;
}
相关文章:
- 幻灯片滚动javascript不起作用
- 将视口底部滚动到元素底部
- jQuery Lazy加载动画滚动
- 设置滑块分区上的滚动
- 结合jQuery和jetpack无限滚动
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- 滚动到容器中的下一个元素-几乎到了
- 只覆盖箭头键滚动事件
- Javascript如何找到滚动事件的来源
- Image赢得't隐藏在滚动jQuery上
- Safari(Mac OS)上的jQuery平滑滚动问题
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- 绑定时将Parsley minlength消息作为选项传递时,未对其进行自定义
- 禁用(而不是隐藏)浏览器滚动条
- JavaScript上下滚动不可预测
- 将菜单项与滚动绑定时出现Jquery错误
- 滚动调整在非ie浏览器加载图像(JavaScript/DOM)后不能正常工作;定时问题w/innerHTML
- 使用ng-html绑定时无法滚动
- jQuery滚动后的定时延迟
- 当滚动过去时位置更改为固定时,导航栏使内容跳跃