[英]How to keep <li> elements fixed top and bottom of <ul> element when scrolling
I have a ul
with fixed height, which contian dynamic number of li
elements, if too many a scrollbar appaers. 我有一个固定高度的
ul
,如果有太多滚动条的话,那就是li
元素的动态数量。 You can select a li
element by clicking on it. 您可以通过单击选择
li
元素。
What I want is the li
element that is active to be fixed at the top if scrolling aboveit or fixed at the bottom if scrolling below it, so the active li
element is always showing, and the other li
elements just scrolls past the active one. 我想要的是
li
元素,如果在上方滚动则固定在顶部,或者如果在其下方滚动则固定在底部,因此活动的li
元素始终显示,而其他li
元素只是滚动到活动元素之上。
I have made a plunker . 我做了一个傻瓜 。 I am using Angular, and I dont know if this can be solved just using CSS or if I need a directive etc. for this.
我正在使用Angular,我不知道这是否可以使用CSS解决,或者我是否需要一个指令等。
Html: HTML:
div class="parent">
<div class="child1">
<input ng-model="vm.query" type="text" placeholder="Search" />
<ul >
<li ng-repeat="todo in todos" ng-class="{'active': todo.active}" ng-click="activeTodo($index)">
<a>{{todo.name}}</a>
</li>
</ul>
</div>
CSS: CSS:
.parent{
display: flex;
}
.child1{
background-color: #eeeeee;
height: 500px;
overflow:scroll;
}
.active{
background-color: red;
position:fixed;
}
Thanks in advance! 提前致谢!
I managed this using some CSS and making a directive. 我使用一些CSS并制定指令来管理它。
Made a plunker here . 在这里做了一个plu ..
I made a directive which every li
element got. 我做了一个每个
li
元素得到的指令。 Then with jquery in the directive I get the necessary elements like the div
which have the scroll, the current li
element etc. 然后在指令中使用jquery,我得到必要的元素,比如
div
,它有滚动,当前的li
元素等。
Two booleans for checking if the li
is fixed at the top or bottom. 两个布尔值用于检查
li
是固定在顶部还是底部。 And then just check if the current scrollPosition(scrollTop) is higher or lower then the current li
element. 然后检查当前的scrollPosition(scrollTop)是否高于或低于当前的
li
元素。 And it also check so the li
element got the active boolean attached too it. 它也检查所以
li
元素也附加了活动布尔值。
return {
restrict: 'A',
link: function($scope, element, attributes){
var scrollDiv = $('#scrollDiv'),
liElement = element,
liElementTop = parseInt(liElement.offset().top),
scrollDivTop = parseInt(scrollDiv.offset().top),
isFixedTop = false,
isFixedBottom = false;
liElement.width(scrollDiv.width());
scrollDiv.on('scroll', function(){
var scrollTop = scrollDiv.scrollTop();
if (!isFixedBottom && !isFixedTop && scrollTop <= liElementTop - (scrollDivTop+(scrollDiv.height()-liElement.height())) && liElement.hasClass("active")) {
isFixedBottom = true;
liElement.css({'position': 'fixed', 'top': scrollDivTop+(scrollDiv.height()-liElement.height()+1),'list-style-type':'none','z-index':'10'});
} else if (isFixedBottom && !isFixedTop && scrollTop >= liElementTop - (scrollDivTop+(scrollDiv.height()-liElement.height()))) {
isFixedBottom = false;
liElement.css({'position': 'static', 'top': 0});
}
else if (!isFixedTop && !isFixedBottom && scrollTop >= liElementTop - scrollDivTop && liElement.hasClass("active")) {
isFixedTop = true;
liElement.css({'position': 'fixed', 'top': scrollDivTop,'list-style-type':'none','z-index':'10'});
}
else if (isFixedTop && !isFixedBottom && scrollTop <= liElementTop - scrollDivTop) {
isFixedTop = false;
liElement.css({'position': 'static', 'top': 0});
}
})
}
};
If I understood you correctly you like the active div on top. 如果我理解你正确,你喜欢顶部的活跃div。
to solve this in css 在css中解决这个问题
use position:relative
on parent and position:absolute
on the child element with top:0
使用
position:relative
parent和parent的position:absolute
子元素的position:absolute
, top:0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.