简体   繁体   English

滚动后,检测元素何时返回原始位置

[英]after scrolling, detect when element returns to original position

I am trying to, sort of, emulate the effect here . 我试图在某种程度上模仿这里的效果。 Essentially, during scrolling, change the css (drop shadow), and when the element comes back to original position (remove shadow). 本质上,在滚动过程中,请更改css(阴影),以及在元素返回原始位置时(移除阴影)。

I am able to detect scroll, but not able to figure out how to detect the return to the original un-scrolled state. 我能够检测到滚动,但无法弄清楚如何检测返回到原始未滚动状态。

HTML 的HTML

<div id="container">
    <ul>
        <li id="one">el</li><li>el</li><li>el</li><li>el</li><li>el</li><li>el</li><li>el</li><li>el</li><li>el</li><li>el</li><li>el</li><li>el</li><li>el</li>
    </ul>
</div>

CSS 的CSS

       html, body {
        margin: 0;
        padding: 0;
        height: 100%;
    }
    #container {
        height: 100px;
        width: 500px;
        border: 1px solid #000000;
        overflow: scroll;
    }

JS (with jquery) JS(带jQuery)

var p = $('#one');
var position0 = p.position().top;
$('#container').scroll(function () {
    if (p.position().top != position0) {
        console.log('p.position: ' + p.position().top);
        $('#container').css('background-color', 'pink');
    }
});

JSFIDDLE: http://jsfiddle.net/nrao89m3/ JSFIDDLE: http : //jsfiddle.net/nrao89m3/

PS: From console.log it doesn't seem to return to its original value at all. PS:从console.log它似乎根本没有返回到其原始值。

Just add an else block: 只需添加一个else块:

var p = $('#one');
var position0 = p.position().top;
$('#container').scroll(function () {
    if (p.position().top != position0) {
        console.log('p.position: ' + p.position().top);
        $('#container').css('background-color', 'pink');
    } else {
        $('#container').css('background-color', 'white');
    }
});

http://jsfiddle.net/vyjbwne2/ http://jsfiddle.net/vyjbwne2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM