jq 如何判断水平滚动条距左侧的距离?

<style>
.c{height:120px;width:1200px;overflow:auto;border:solid 1px black;margin-bottom:5px;}
</style>
<div class="c"><p style="width: 1600px;">bb</p></div>
$('div').scroll(function(){
    var sl=this.scrollLeft,
        st=this.scrollTop,
        d=$(this).data('slt');
        console.log(sl)
    if($(this).scrollLeft == 100){
        console.log(1)
    }
    
})

console.log(sl)可以打印出水平滚动条距离左侧的距离,但是后面的if判断就不行,不打印那个1。

阅读 4.5k
1 个回答
$('div').scroll(function(){
    var sl=this.scrollLeft,
        st=this.scrollTop,
        d=$(this).data('slt');
        console.log(sl)
    if($(this).scrollLeft() == 100){
        console.log(1)
    }
})

因为你用的是jqueryscrollLeft方法,而不是DOM中的scrollLeft,所以你需要是调用方法的方式。

补充

var time = 0;
$('div').scroll(function(){
    var sl=this.scrollLeft,
        st=this.scrollTop,
        d=$(this).data('slt');
        console.log(sl);
    if (time == 0) {
        if(sl > 100){
            time++;
            console.log(1)
        }
    }
})

其实就是加个开关的意思。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题