BScroll 实时监听滚动位置

默认情况下,BScroll是不监听的

probe属性

0,1都是不进行实时侦测

2 在手指滚动的过程中侦测,手指离开后的惯性滚动不进行侦测

3 只要是在滚动 都侦测

<script src="./bscroll.js"></script>
<script>
    const bscroll=new BScroll(document.querySelector('.content'),{
        probeType:3
    })

bscroll.on('scroll',(position)=>{
    console.log(position);
})
</script>

 

要实现一个竖向滚动条,可以使用以下方法: 1. 设置外层容器的高度和 overflow 属性为 hidden,使得容器内的内容超出容器高度时不可见。 2. 在容器内添加一个滚动条容器,设置其高度和宽度,以及 overflow-y 属性为 scroll,使得容器内的内容超出容器高度时出现滚动条。 3. 在滚动条容器内添加一个滚动条滑块,通过监听滚动条容器的 scroll 事件,并计算滑块位置和容器滚动距离,来实现滑块移动和容器滚动。 以下是一个简单的示例代码: HTML: ```html <div class="scroll-container"> <div class="scroll-content"> <!-- 内容超出容器高度时出现滚动条 --> </div> <div class="scroll-bar"> <div class="scroll-thumb"></div> </div> </div> ``` CSS: ```css .scroll-container { height: 300px; /* 容器高度 */ overflow: hidden; position: relative; } .scroll-content { height: 100%; overflow-y: scroll; } .scroll-bar { position: absolute; top: 0; right: 0; bottom: 0; width: 10px; /* 滚动条宽度 */ } .scroll-thumb { position: absolute; top: 0; left: 0; width: 100%; height: 50px; /* 滑块高度 */ background-color: #ccc; border-radius: 5px; } ``` JavaScript: ```javascript const container = document.querySelector('.scroll-container'); const content = document.querySelector('.scroll-content'); const bar = document.querySelector('.scroll-bar'); const thumb = document.querySelector('.scroll-thumb'); // 计算滑块高度和容器高度的比例 const thumbHeightRatio = container.clientHeight / content.scrollHeight; // 设置滑块高度 thumb.style.height = `${container.clientHeight * thumbHeightRatio}px`; // 监听滚动条容器的 scroll 事件 content.addEventListener('scroll', () => { const { scrollTop, scrollHeight, clientHeight } = content; // 计算滑块位置和容器滚动距离 const thumbTop = (scrollTop / scrollHeight) * (container.clientHeight - thumb.clientHeight); const scrollDistance = (thumbTop / container.clientHeight) * scrollHeight; // 更新滑块位置和容器滚动距离 thumb.style.top = `${thumbTop}px`; content.scrollTop = scrollDistance; }); ``` 这里只是一个简单的示例,实际应用中可能需要考虑更多因素,如滑块拖动、鼠标滚轮等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值