2012-07-04 42 views
1

我有在HTML中上下滚动,并验证码:上下滚动慢慢与jQuery

<div id="Response" class="comment"></div> 
<br> 
<br> 
<br> 
<br> 
<div class="but4 , text15"><a id="Resp" href="#rp">Answer</a></div> 

这个代码jQuery中:

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> 
<script type="text/javascript"> 
    $("#Resp").click(function() { 
     $("html").animate({ scrollTop: $("#Response").offset().top }, 1200); 
    }); 
</script> 

,但它不工作..怎么能我解决这个问题?当我点击答案时,我想要滚动到最后并转到响应格。

回答

3

我建议使用ScrollTo插件,而不是滚动您自己的解决方案:http://flesler.blogspot.com/2007/10/jqueryscrollto.html

演示是在这里:http://demos.flesler.com/jquery/scrollTo/

您可以很容易地指定时间:http://demos.flesler.com/jquery/scrollTo/

编辑

我们在聊天中提出的解决方案是这样的(不包括script标签):

$(function() { 
    $("#Resp").click(function() { 
     $("body").scrollTo($("#Response"), 1200); 
    }); 
}); 
+0

我不是jQuery的专业。你能给我一个代码吗? –

+0

当然,我已经添加了一个例子。但是您必须将该scrollTo插件添加到您链接到的项目中。 –

+0

它不工作.. –

0

这取决于浏览器的实现,但你可以得到它在WebKit的(Safari浏览器,铬)和Firefox浏览器与

$('html, body').animate({ scrollTop: $("#Response").offset().top }, 1200); 
+0

这不是工作亲爱的Beat。 –

+0

它适用于Firefox和Chrome浏览器(我为scrollTop使用固定值)。你包括jquery 1.5.1,最新版本是1.7.2 - 如果可以的话,你应该更新它。 –