如图
当往上滚动,菜单栏滚动到顶部的位置时,再继续滚动,悬停在顶部。
问:
1、Vue是否有提供这样的控件?
2、如果vue没有,有什么现成的一些轮子吗?(最好兼容性好一点,webapp)
3、如果现成的轮子也没有,怎么实现?
如上,有没有大佬解答下,不胜感激
按照 @游龙翔隼的思路已解决,贴上代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div style="overflow:scroll">
<div style="width: 100%;height:100px;background:red;margin-bottom:10px"></div>
<div id="bar" style="width:100%;height:20px;background:black"></div>
<div style="width: 100%;height:1500px;background:red;margin-top:10px"></div>
</div>
</body>
<script>
var barOffSetTop = document.getElementById('bar').offsetTop;
window.addEventListener('scroll', (e) => {
if(barOffSetTop < document.body.scrollTop){
bar.classList.add('add-fixed')
}else{
bar.classList.remove('add-fixed')
}
});
</script>
</html>
<style>
.add-fixed{
position: fixed;
top: 0;
}
</style>
注意:菜单栏的offsetTop不能动态取,不然置顶后offsetTop的值就改变了,滑动回来的时候,菜单栏无法复位。
用sticky定位就行了,具体看 这里 注:目前兼容性较差
之前也做过类似的东西,实现比较简单,来说下思路:
1.监听滚动事件
2.判断菜单dom是否超出视窗
3.超出后使用绝对定位,你这里应该用fixed吧,固定在顶部