jquery 判断滚动方向 向上还是向下

前端之家收集整理的这篇文章主要介绍了jquery 判断滚动方向 向上还是向下前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<table class="javascript"><tr class="li1">
<td class="ln"><pre class="de1">1
2
3
4
5
6
7
8
9
10
11
12
13
@H_4041@</td>
<td class="de1"><pre class="de1">scroll<span class="br0">(<span class="kw1">function<span class="br0">(direction<span class="br0">) <span class="br0">{ console.<span class="me1">log<span class="br0">(direction<span class="br0">) <span class="br0">}<span class="br0">)<span class="sy0">;    
<span class="kw1">function scroll<span class="br0">( fn <span class="br0">) <span class="br0">{
    <span class="kw1">var beforeScrollTop <span class="sy0">= document.<span class="me1">body.<span class="me1">scrollTop<span class="sy0">,
        fn <span class="sy0">= fn <span class="sy0">|| <span class="kw1">function<span class="br0">(<span class="br0">) <span class="br0">{<span class="br0">}<span class="sy0">;
    window.<span class="me1">addEventListener<span class="br0">(<span class="st0">"scroll"<span class="sy0">, <span class="kw1">function<span class="br0">(<span class="br0">) <span class="br0">{
        <span class="kw1">var afterScrollTop <span class="sy0">= document.<span class="me1">body.<span class="me1">scrollTop<span class="sy0">,
            delta <span class="sy0">= afterScrollTop <span class="sy0">- beforeScrollTop<span class="sy0">;
        <span class="kw1">if<span class="br0">( delta <span class="sy0">=== <span class="nu0">0 <span class="br0">) <span class="kw1">return <span class="kw2">false<span class="sy0">;
        fn<span class="br0">( delta <span class="sy0">> <span class="nu0">0 <span class="sy0">? <span class="st0">"down" <span class="sy0">: <span class="st0">"up" <span class="br0">)<span class="sy0">;
        beforeScrollTop <span class="sy0">= afterScrollTop<span class="sy0">;
    <span class="br0">}<span class="sy0">, <span class="kw2">false<span class="br0">)<span class="sy0">;
<span class="br0">}
 @H
404_1@</td>
</tr></table>

原文链接:https://www.f2er.com/note/421979.html

猜你在找的程序笔记相关文章