JQuery overflow: hidden on parent device, detecting if child is actually visible

I have a problem with this jQuery blowing in my head. I've tried three different JS and jQuery features that users have suggested on the internet to achieve this and can't seem to get anything to work.

I am trying to hide the .arrow-up class when .first is actually displayed on screen and hides the .arrow-down class when .last is displayed on screen.

Sounds simple enough, right?

Well, the parent has an overflow: hidden on it (like most carousels - they really are from hell). Does anyone know how to do this? I would really appreciate any help, JS really isn't my strongest in any way ...

Here's my current jQuery -

jQuery(document).ready(function ($) {
        $(".arrow-down").bind("click", function (event) {
            event.preventDefault();
            $(".vid-list-container").stop().animate({
                scrollTop: "+=300"
            }, 300);

        });
        $(".arrow-up").bind("click", function (event) {
            event.preventDefault();
            $(".vid-list-container").stop().animate({
                scrollTop: "-=300"
            }, 300);
        });
 });

      

In this, .vid-list-container is the parent with overflow: hidden on it, and .first and .last are inside the container. The arrow classes are outside the container.

Built this pen for anyone who wants to play with it. http://codepen.io/seancrater/pen/waPNEW

Thank!

+3


source to share


2 answers


This should work. Note, however, that I used opacity: 0, so the arrow can still be clicked. You need to change that!



function checkDownArrow() {
  setTimeout(function() {
       if($(".vid-list-container").scrollTop() != 0){
          $('.arrow-up').css('opacity',1);
        }
      if(($(".vid-list-container").scrollTop() + $(".vid-item").height()+5) >= $(".vid-item").length * $(".vid-item").height()) {
          $('.arrow-down').css('opacity',0);
        }
      },350); 
}

function checkUpArrow() {
 setTimeout(function() {
       if($(".vid-list-container").scrollTop() == 0){
          $('.arrow-up').css('opacity',0);
        }
      if(($(".vid-list-container").scrollTop() + $(".vid-item").height()+5) < $(".vid-item").length * $(".vid-item").height()) {
          $('.arrow-down').css('opacity',1);
        }
      },350);
 }
 checkDownArrow();
 checkUpArrow();
 jQuery(document).ready(function ($) {
        $(".arrow-down").bind("click", function (event) {
            event.preventDefault();
            $(".vid-list-container").stop().animate({
                scrollTop: "+=173"
            }, 300);
           checkDownArrow();

        });
        $(".arrow-up").bind("click", function (event) {
            event.preventDefault();
            $(".vid-list-container").stop().animate({
                scrollTop: "-=173"
            }, 300);
            checkUpArrow();
        });
 }); 

      

+1


source


EDIT

Ok I see you have a different problem ... may I suggest using a different approach? Something like that.

HTML:

<div class="outer-wrapper">
    <div class="inner-wrapper">
        <div class="vid-item">
            ...
        </div>
        <div class="vid-item">
            ...
        </div>
    </div>
</div>

      



CSS

.outer-wrapper {width:200px; height:150px; overflow:hidden;}
.inner-wrapper {height:auto; margin-top:0;}
.vid-item {width:200px; height:150px;}

      

JS:

    var itemHeight = $('.vid-item').first().height();
    var wrapperHeight = $('.inner-container').height();

    $(".arrow-down").bind("click", function (event) {
        event.preventDefault();
        var margin = parseInt($('.inner-container').css('margin-top'));
        if(itemHeight - margin > wrapperHeight) {
            $('.inner-container').css('margin-top', (itemHeight-wrapperHeight) + 'px');
            $('.arrow-down').addClass('hidden');
        }
        else {
            $('.inner-container').css('margin-top', (margin-itemHeight) + 'px');
        }
        $('.arrow-up').removeClass('hidden');
    });

    $(".arrow-up").bind("click", function (event) {
        event.preventDefault();
        var margin = parseInt($('.inner-container').css('margin-top'));
        if(margin + itemHeight >= 0) {
            $('.inner-container').css('margin-top', '0');
            $('.arrow-up').addClass('hidden');
        }
        else {
            $('.inner-container').css('margin-top', (margin+itemHeight) + 'px');
        }
        $('.arrow-down').removeClass('hidden');
    });

      

0


source







All Articles