【问题标题】:jQuery Overflow: Hidden on Parent, Detect if Child is Actually On VisiblejQuery 溢出:隐藏在父对象上,检测子对象是否实际可见
【发布时间】:2015-06-25 12:55:39
【问题描述】:

我对这个让我大吃一惊的 jQuery 有疑问。我已经尝试了人们在网上建议的三种不同的 JS 和 jQuery 函数来完成此任务,但似乎没有任何效果。

我试图在 .first 在屏幕上实际可见时隐藏 .arrow-up 类,并在 .last 在屏幕上可见时隐藏 .arrow-down 类。

听起来很简单,对吧?

父元素已经溢出:隐藏在它上面(就像大多数轮播一样——它们真的来自地狱)。有人知道怎么做吗?我真的很感激任何帮助,无论如何,JS真的不是我最强大的......

这是我当前的 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);
        });
 });

在此,.vid-list-container 是具有溢出的父级:隐藏在其上,并且 .first 和 .last 都在容器内。箭头类都在容器之外。

为任何想玩它的人打造这支笔。 http://codepen.io/seancrater/pen/waPNEW

谢谢!

【问题讨论】:

    标签: javascript jquery overflow hidden visible


    【解决方案1】:

    这应该可行。但是请注意,我使用了 opacity:0,因此仍然可以单击箭头。你需要改变它!

    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();
            });
     }); 
    

    【讨论】:

    • 我检查过了,它成功了!但是,只需将 5 值更改为更大的值!它仍然不是最佳解决方案,但您可以改进它! :)
    • 功能比我昨天出的垃圾好多了,真的要多学JS了哈哈
    • 只是想知道,声明中的 5 实际上代表什么?商品数量?
    • 在较小的视口上让向下箭头消失很困难,我想我需要更改它计算与顶部距离的方式?
    • 5 只是我添加的一个数字,因为上边距或下边距将每个项目彼此分开。这是一个随机数。不过这很奇怪,因为当我尝试它时,它对双方都有效。但是,尝试获取 margin-top,项目之间的空间并添加它而不是 5。它可能会有所帮助。
    【解决方案2】:

    编辑

    好的,我知道您有不同的问题...我可以建议使用不同的方法吗?像这样的。

    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');
        });
    

    【讨论】:

    • :visible 检查它们是否在屏幕上/屏幕外。只是它们是否被隐藏(例如display: none)。您需要根据视口检查元素的位置。
    • 这就是我一直遇到的问题,我希望 :visible 实际上确实检查了每种类型的可见性,或者至少可以选择不使用外部选项。我确信我看到的那些脚本可以工作,我只是不知道如何使用它们。 github.com/customd/jquery-visible 这似乎很有希望,但我仍然不知道从哪里开始使用它,因为文档太简单了......
    • 我还试图找到代码来检测 .first 是否在它的起始位置,或者 .last 是否向上移动,但没有找到任何东西。它必须使用父元素作为参考。
    • 我更新了我的答案,我是在这里写的,所以可能有一些语法错误,寻找它的逻辑:)
    • 所以现在它变得更加复杂,试图让它保持响应,猜测这是基于像素高度计算的?不知道我通过尝试修改滑块进入了这里,为编写这些东西的人提供了更多权力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 2011-08-18
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多