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。


慕标5832272
浏览 1921回答 1
1回答

慕婉清6462132

$('div').scroll(function(){&nbsp; &nbsp; var sl=this.scrollLeft,&nbsp; &nbsp; &nbsp; &nbsp; st=this.scrollTop,&nbsp; &nbsp; &nbsp; &nbsp; d=$(this).data('slt');&nbsp; &nbsp; &nbsp; &nbsp; console.log(sl)&nbsp; &nbsp; if($(this).scrollLeft() == 100){&nbsp; &nbsp; &nbsp; &nbsp; console.log(1)&nbsp; &nbsp; }})因为你用的是jquery的scrollLeft方法,而不是DOM中的scrollLeft,所以你需要是调用方法的方式。补充var time = 0;$('div').scroll(function(){&nbsp; &nbsp; var sl=this.scrollLeft,&nbsp; &nbsp; &nbsp; &nbsp; st=this.scrollTop,&nbsp; &nbsp; &nbsp; &nbsp; d=$(this).data('slt');&nbsp; &nbsp; &nbsp; &nbsp; console.log(sl);&nbsp; &nbsp; if (time == 0) {&nbsp; &nbsp; &nbsp; &nbsp; if(sl > 100){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(1)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }})其实就是加个开关的意思。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript