2016-11-23 40 views
0

主要問題scrollTop的()不工作時,html和車身樣式看起來是這樣的:jQuery的scrollTop的不工作時,html和身體溢出自動

html 
    margin: 0 
    padding: 0 
    border: 0 
    overflow: auto 

body 
    padding: 0 
    margin: 0 
    background-image: url('/imgs/bg1.jpg') 
    overflow: auto 
    background-repeat: no-repeat 

我的js代碼:

$('.get_method').click(function() { 

    $('body').animate({ 
    scrollTop: $(".test").offset().top 
    }); 

}); 

當我從html或body元素(或任何地方)中刪除溢出屬性時,所有工作都完美無缺,但是當我滾動文檔BG圖像結束時,我得到空白區域。當我點擊.get_method按鈕時,我需要的是固定BG圖像和工作scrollTop動畫效果。

編輯:

元素.test它的div元素。 CSS:

.test 
    position: relative 
    top: 900px 
+0

如果使用'$(window)'更改'$('body')''? –

+0

不能再工作 –

回答

0

試試這個

demo

HTML

<h1>Top of the page</h1> 

<article style="height: 1000px"> 
    <p style="margin-bottom: 600px">Scroll down the page&hellip;</p> 
    <p>Then click the box.</p> 
    <a href="#" class="scrollup">Scroll</a> 
</article> 

jQuery的

$(document).ready(function() { 

    $(window).scroll(function() { 
     if ($(this).scrollTop() > 100) { 
      $('.scrollup').fadeIn(); 
     } else { 
      $('.scrollup').fadeOut(); 
     } 
    }); 

    $('.scrollup').click(function() { 
     $("html, body").animate({ 
      scrollTop: 0 
     }, 600); 
     return false; 
    }); 

}); 
+0

它的作品,但BG圖像結束,我得到空白 –

+0

現在請檢查@SergeiKobets – falguni

+0

所有的作品,但我不知道如何使FIXED BG的主要問題。我需要使用固定bg滾動內容...當我使html溢出隱藏和身體溢出滾動我會得到固定的BG,但您的腳本不起作用 –