首页 > 语言 > JavaScript > 正文

jQuery实现自动滚动到页面顶端的方法

2024-05-06 16:20:45
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了jQuery实现自动滚动到页面顶端的方法,涉及jQuery针对页面操作的相关技巧,需要的朋友可以参考下

本文实例讲述了jQuery实现自动滚动到页面顶端的方法。分享给大家供大家参考。具体实现方法如下:

 

 
  1. $(".scroll").click(function(event) 
  2. { 
  3. //prevent the default action for the click event 
  4. event.preventDefault(); 
  5. //get the full url - like mysitecom/index.htm#home 
  6. var full_url = this.href; 
  7. //split the url by # and get the anchor target  
  8. //name - home in mysitecom/index.htm#home 
  9. var parts = full_url.split("#"); 
  10. var trgt = parts[1]; 
  11. //get the top offset of the target anchor 
  12. var target_offset = $("#"+trgt).offset(); 
  13. var target_top = target_offset.top; 
  14. //goto that anchor by setting the body scroll top to anchor top 
  15. $('html, body').animate({scrollTop:target_top}, 500); 
  16. });​ 

希望本文所述对大家的jQuery程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选