2012-09-29 177 views
16

我不是程序員,但我使用下面的代碼將頁面滾動到頂部。如何向下滾動 - jQuery

我該如何調整它以使其向下滾動?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> 

<a class="btnMedio" href="javascript:;"> 
    <img src="http://desmond.imageshack.us/Himg41/scaled.php?server=41&filename=deixeseuemail.png&res=landing"/> 
</a> 

<script> 
    $('.btnMedio').click(function(){ 
     $('html, body').animate({scrollTop:1000},'50'); 
    }); 
</script> 

回答

42
$('.btnMedio').click(function(event) { 
    // Preventing default action of the event 
    event.preventDefault(); 
    // Getting the height of the document 
    var n = $(document).height(); 
    $('html, body').animate({ scrollTop: n }, 50); 
//          | | 
//          | --- duration (milliseconds) 
//          ---- distance from the top 
}); 
+0

感謝您的幫助 如何控制德距離頁面往下走? – user1301037

+0

@ user1301037你可以用你想要的任何值替換n,你也可以使用'offset'方法。 – undefined

+0

哇,你做了我的一天。 –

13

試試這個:

window.scrollBy(0,180); // horizontal and vertical scroll increments 
+0

OP提到了** jQuery **解決方案。 – crmpicco

+14

OP可能會假設爲此需要jQuery。這個答案可能會有所幫助。 –

4

這可以用於解決這個問題

<div id='scrol'></div> 

JavaScript中使用這個

jQuery("div#scrol").scrollTop(jQuery("div#scrol")[0].scrollHeight); 
6

我大多使用下面的代碼向下滾動

$('html, body').animate({ scrollTop: $(SELECTOR).offset().top - 50 }, 'slow'); 
0
jQuery(function ($) { 
     $('li#linkss').find('a').on('click', function (e) { 
      var 
       link_href = $(this).attr('href') 
       , $linkElem = $(link_href) 
       , $linkElem_scroll = $linkElem.get(0) && $linkElem.position().top - 115; 
      $('html, body') 
       .animate({ 
        scrollTop: $linkElem_scroll 
       }, 'slow'); 
      e.preventDefault(); 
     }); 
    }); 
+2

你能解釋一下你的答案嗎? – Eria