2012-07-05 66 views
4

我正在使用滾動到我網站上的頂部按鈕。我使用這個jQuery它僅當向下滾動到***時纔可見元素

$(window).scroll(function() { 
    if ($(this).scrollTop()) { 
     $('#cttm:hidden').stop(true, true).fadeIn(); 
    } else { 
     $('#cttm').stop(true, true).fadeOut(); 
    } 
}); 


    $(document).ready(function(){ 
     var bottom = ($(window).outerHeight() - $(window).height()) - 150; // 150 pixel to the bottom of the page; 
     $(window).scroll(function(){ 
      if ($(window).scrollTop() >= bottom) { 
        $("#cttm").fadeTo("slow",.95); 
      } else { 
        $("#cttm").fadeOut("slow"); 
      } 
     }); 

     $("#cttm").click(function(){ 
      $('html, body').animate({scrollTop:0}, 'slow'); 
      $("#cttm").fadeOut("slow"); 
     }); 
    }); 

這個jQuery的偉大工程,但我想,當我們從頂部或類似的東西滾動到200像素的元素纔會出現。有沒有辦法用JQuery來做到這一點?

+0

的代碼應該工作,是什麼問題? – undefined 2012-07-05 08:52:05

+0

您可以查看此頁面http://template-designfbapp.blogspot.in/2012/06/test-post-with-text.html,滾動到頂部右上角的按鈕出現時,你只需向下滾動不在具體點。 – 2012-07-05 08:55:04

回答

4

你不需要窗口高度來做到這一點。

var isVisible = false; 
$(window).scroll(function(){ 
    var shouldBeVisible = $(window).scrollTop()>200; 
    if (shouldBeVisible && !isVisible) { 
      isVisible = true; 
      $('#mybutton').show(); 
    } else if (isVisible && !shouldBeVisible) { 
      isVisible = false; 
      $('#mybutton').hide(); 
    } 
}); 

示範:http://jsfiddle.net/dystroy/gXSLE/

相關問題