2012-09-29 107 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