javascript - Show button if root/parent element scroll up/down and hide that button if not -
how show button if root or parent element need scrolled , hide button if root/parent element not need scrolled ?.
for details:
- if have element max-height 200px (example) , overflow-y:auto, element can scrolled if childrens exceeds or greater max-height above, , can't scrolled if not.
now, how show/hide button (example) according root or parent element scroll.
here code :
var contentstatusprofile = $(".main_profile"); var scrollingstatus = $(".scroll-down-status-profile"); if ( ???? ) { /* here qestion showing , hiding button corresponding parent element need scrolled or not */ scrollingstatus.on('click',function(){ if ($(this).hasclass('more-up-status-profile')) { var lengthscroll = contentstatusprofile[0].scrolltop; lengthscroll -= 150; contentstatusprofile.animate({ scrolltop:lengthscroll }, 500); } else { var lengthscroll = contentstatusprofile[0].scrolltop; lengthscroll += 150; contentstatusprofile.animate({ scrolltop:lengthscroll }, 500); if(lengthscroll >= contentstatusprofile[0].scrollheight - contentstatusprofile.height()){ scrollingstatus.addclass('more-up-status-profile').children().addclass('glyphicon-chevron-up').removeclass('glyphicon-chevron-down'); } } }); }
any answer appreciated. thank.
you have compare 2 different hieghts of .main_profile
.
the jquery .outerheight()
include padding.
, javascript .scrollheight
.
you noticed first has ()
, not second...
because first jquery method , second javascript element property.
so 2 ones compare know if there content scrolled.
i got click
event handler out of if/else block.
has defined separately because if/else used show/hide button.
i changed bit way add , remove more-up-status-profile
class. made turn red when has class...
don't hesitate put ugly obvious colors when debugging! ;)
var contentstatusprofile = $(".main_profile"); var scrollingstatus = $(".scroll-down-status-profile"); var csp_height = contentstatusprofile.outerheight(); var csp_scrollableheight = contentstatusprofile[0].scrollheight; console.log( csp_height ); console.log( csp_scrollableheight ); console.log( csp_scrollableheight > csp_height ); if ( csp_scrollableheight > csp_height ) { /* here qestion showing , hiding button corresponding parent element need scrolled or not */ $(".scroll-down-status-profile").show(); }else{ $(".scroll-down-status-profile").hide(); } scrollingstatus.on('click',function(){ var lengthscroll = contentstatusprofile[0].scrolltop; if ($(this).hasclass('more-up-status-profile')) { lengthscroll -= 150; contentstatusprofile.animate({ scrolltop:lengthscroll }, 500); if(lengthscroll <= 0){ scrollingstatus.removeclass('more-up-status-profile').children().addclass('glyphicon-chevron-down').removeclass('glyphicon-chevron-up'); } } else { lengthscroll += 150; contentstatusprofile.animate({ scrolltop:lengthscroll }, 500); if(lengthscroll >= contentstatusprofile[0].scrollheight - contentstatusprofile.outerheight()){ scrollingstatus.addclass('more-up-status-profile').children().addclass('glyphicon-chevron-up').removeclass('glyphicon-chevron-down'); }else{ scrollingstatus.removeclass('more-up-status-profile').children().addclass('glyphicon-chevron-down').removeclass('glyphicon-chevron-up'); } } });
.main_profile{ height: 200px; border: 1px solid black; overflow-y: auto; } .more-up-status-profile{ background-color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="main_profile" style="max-height:100px; padding:20px"> <div id="test"> <ul> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> <li>lorem ipsum</li> </ul> </div> </div> <button class="scroll-down-status-profile">scroll <span class="glyphicon glyphicon-chevron-down"></span></button>
Comments
Post a Comment