2015-06-25 43 views
3

我遇到了這個讓我非常頭疼的jQuery問題。我嘗試了三種不同的JS和jQuery功能,人們在網上建議完成此功能,並且似乎無法獲得任何工作。jQuery溢出:隱藏在父類中,檢測子類是否實際上可見

我試圖隱藏類.arrow-up,當.first實際上在屏幕上可見並隱藏類時.arrow-down當.last在屏幕上可見時。

聽起來很簡單吧?那麼父元素有溢出:隱藏在其上(像大多數傳送帶 - 他們真的是來自地獄)。有人知道怎麼做嗎?我真的很感謝所有幫助,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列表容器與父溢出:隱藏在其上,並且.first和.last都在容器內。箭頭類都在容器外部。

爲任何想玩弄它的人打造了這支筆。 http://codepen.io/seancrater/pen/waPNEW

謝謝!

回答

1

這應該工作。不過請注意,我使用了不透明度: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(); 
     }); 
}); 
+0

它工作真棒!你太棒了! – Sean

+0

讓我再檢查一遍!它在我測試它時起作用! –

+0

我檢查,它的工作!然而,只需將5值更改爲更大的值即可!它不是最佳解決方案,但您可以改進它! :) –

0

編輯

好了,我看到你有一個不同的問題...我可以建議使用一種不同的方法?像這樣的東西。

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

':visible' * does not * they are they are onscreen/offscreen。只是它們是否隱藏(例如'display:none')。您需要檢查元素的位置,而不是視口。 –

+0

這就是我一直在打的問題,我希望:可見實際上確實檢查了每種可見性,或者至少可以選擇沒有外部選項。我確信我看到的那些腳本工作,我只是不知道如何使用它們。 https://github.com/customd/jquery-visible 這似乎很有前途,但我仍然不知道從哪裏開始使用它的腳本,因爲文檔太輕... – Sean

+0

我也試圖找到代碼來檢測.first是否處於起始位置,或者如果.last在那裏移動,但沒有任何運氣可以找到相應的東西。它必須使用父元素作爲參考。 – Sean

相關問題