jQuery实现自动滚动到页面顶端的方法
作者:bea
本文实例讲述了jQuery实现自动滚动到页面顶端的方法。分享给大家供大家参考。具体实现方法如下: $(".scroll").click(function(event){ //prevent the default action for the click event event.preventDefault(); //get the full url - like mysitecom/index.htm#home var full_url = this.href;
本文实例讲述了jQuery实现自动滚动到页面顶端的方法。分享给大家供大家参考。具体实现方法如下:
$(".scroll").click(function(event)
{
//prevent the default action for the click event
event.preventDefault();
//get the full url - like mysitecom/index.htm#home
var full_url = this.href;
//split the url by # and get the anchor target
//name - home in mysitecom/index.htm#home
var parts = full_url.split("#");
var trgt = parts[1];
//get the top offset of the target anchor
var target_offset = $("#"+trgt).offset();
var target_top = target_offset.top;
//goto that anchor by setting the body scroll top to anchor top
$('html, body').animate({scrollTop:target_top}, 500);
});
希望本文所述对大家的jQuery程序设计有所帮助。
有用 | 无用
$(".scroll").click(function(event)
{
//prevent the default action for the click event
event.preventDefault();
//get the full url - like mysitecom/index.htm#home
var full_url = this.href;
//split the url by # and get the anchor target
//name - home in mysitecom/index.htm#home
var parts = full_url.split("#");
var trgt = parts[1];
//get the top offset of the target anchor
var target_offset = $("#"+trgt).offset();
var target_top = target_offset.top;
//goto that anchor by setting the body scroll top to anchor top
$('html, body').animate({scrollTop:target_top}, 500);
});
希望本文所述对大家的jQuery程序设计有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JSON字符串和对象之间的转换详解
- Js和JQuery获取鼠标指针坐标的实现代码分享
- Javascript实现飞动广告效果的方法
- javascript自定义右键弹出菜单实现方法
- JS+DIV实现鼠标划过切换层效果的方法
- JavaScript实现仿网易通行证表单验证
- js+cookies实现悬浮购物车的方法
- 手机开发必备技巧:javascript及CSS功能代码分享
- JS和css实现检测移动设备方向的变化并判断横竖屏幕
- jquery 构造函数在表单提交过程中修改数据
- js实现鼠标划过给div加透明度的方法
- javascript实现youku的视频代码自适应宽度
- 微信WeixinJSBridge API使用实例
- jquery.validate使用时遇到的问题
- 微信内置浏览器私有接口WeixinJSBridge介绍
- 微信浏览器内置JavaScript对象WeixinJSBridge使用实例
- JS中的THIS和WINDOW.EVENT.SRCELEMENT详解
- jQuery实现表格行上移下移和置顶的方法
- jquery控制表单输入框显示默认值的方法