2014-01-21 35 views
0

我用這個代碼來獲取元素淡入滾動時:如何檢測,如果視圖在頂部位置

<script language="JavaScript"> 
     $(document).ready(function() { 
      $(window).scroll(function() { 
       $('#floatingDIV4').each(function() { 
       var bottom_of_object = $(this).position().top + $(this).outerHeight(); 
       var bottom_of_window = $(window).scrollTop() + $(window).height(); 

       if (bottom_of_window > bottom_of_object) { 
        $(this).animate({'opacity':'1'}, 500); 
       } 
       }); 
      }); 
     }); 
    </script> 

我試圖找到使被出現在滾動的元素代碼淡出在頂部的位置,但我沒有找到任何。你有什麼想法嗎?


編輯:
這些問題的答案是非常好的,但代碼沒有工作。

+0

性能比較X,Y的值,嘗試'debounce'&'緩存elements'。 – rab

回答

0

使用本

if (bottom_of_window > bottom_of_object) { 
       $(this).animate({ scrollTop: 0, opacity: 0 },'slow'); 
      } 

如果這個答案是正確的,那麼請標記爲答案爲他人....

0
if (bottom_of_window > bottom_of_object) { 
      $(this).animate({ scrollTop: 0, 
           opacity: 0 
          },'slow'); 
     } 

這將滾動到頂部,並在同一時間而褪色。 但是,如果你正在尋找的代碼褪色,只有當它已經完成移動到頂部,你可以使用:

if (bottom_of_window > bottom_of_object) { 
      $(this).animate({ scrollTop: 0,}, 
        complete: function(){ 
         $(this).animate({opacity:0}) 
        )}; 
     } 

但是,這樣的盒子是可見的,因爲它正在消失,在scrollTop:值就需要高於0,例如。盒子的高度px。

0

可能是這個幫助你

function show_coords(event) 
{ 
    var x=event.clientX; 
    var y=event.clientY; 
    alert("X coords: " + x + ", Y coords: " + y); 
} 

一些預定義的值

相關問題