Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

jquery - show/hide DIV when passed the other div

I want a hidden div show if he passed the another DIV. for example show in html below if div.passedMe bottom! meet the top of the window, the div.showHide will show and when scroll up and the div.passedMe top! meet the top of the window the div.showHide will hide.

Html

<div class="passedMe">If you passed this div another div will show/hide</div>

<div class="showHide"> this div will show/hide</div>

so far this is what I have but this only work when passed a certain PIXEL on A page

$(document).scroll(function () {
    var y = $(this).scrollTop();
    if (y > 100) {
        $('.showHide').fadeIn();
    } else {
        $('.showHide').fadeOut();
    }

});

this is the fiddle

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<html>
<head>
</head>
<body>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>

<div class="passedMe">If you passed this div another div will show/hide</div>

<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>

<div class="showHide" style="display:none;"> this div will show/hide</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(function(){
        $(document).scroll(function(){
                    var vis = ($(document).scrollTop() > ($('.passedMe').offset().top+$('.passedMe').height()));
                            $('.showHide').css('display', vis?'':'none')
                                });
        });
</script>

<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>

</body>
</html>

And if you wish to fadein/fadeout then instead of:

$('.showHide').css('display', vis?'':'none');

use

if (vis) $('.showHide').fadeIn(); else $('.showHide').fadeOut();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...