关于使用固定导航a标签跳锚点时产生问题的解决方法
2017-06-30 10:37
351 查看
1、导航固定在头部,a标签跳锚点时,偏移问题。
只适用于在当前页面内的锚点跳转html部分代码:
<ul class="nav"> <li class="dropdown action"> <a data-ajax="false" href="index.php" class="dropdown-toggle">网站首页</a> </li> <li class="dropdown"> <a data-ajax="false" onclick="goto(this)" href="index.php#aboutUs" class="dropdown-toggle" name="#aboutUs">关于我们</a> </li> </ul>
js部分代码:
function goto(th){ var divName = $(th).attr("name"); if( divName == '#aboutUs'){ $("html, body").animate({scrollTop: $(divName ).offset().top - 100 + "px"}, 100); }else{ $("html, body").animate({scrollTop: $(divName ).offset().top - 68 + "px"}, 100); } }
2、跨页面–a标签跳锚点时,偏移问题。
$(function(){ $(window).hashchange(function(){ var hash = $(location.hash); if( hash.selector == '#aboutUs'){ $("html, body").animate({scrollTop: $(hash.selector).offset().top - 100 + "px"}, 100); }else{ $("html, body").animate({scrollTop: $(hash.selector).offset().top - 68 + "px"}, 100); } }); $(window).hashchange(); }) function goto(th){ var divName = $(th).attr("name"); hashA(); } function hashA(){ var hash = window.location.hash; if( hash == '#aboutUs'){ $("html, body").animate({scrollTop: $(hash).offset().top - 100 + "px"}, 100); }else{ $("html, body").animate({scrollTop: $(hash).offset().top - 68 + "px"}, 100); } }
注:
之前不写$(window).hashchange();时,每次都是获取到的是上一次点击之后的hash值。应该是执行顺序的问题,不明白怎么在点击事件完成之后,执行函数,所以才用了这种方式。
代码具体应用时,具体问题具体分析。
希望有同学们看到之后可以解答,感谢。
相关文章推荐
- mysql c++ connector 1.0.5 getString方法 使用过程中遇到汉字产生的乱码问题的解决方法
- 关于安装IE8之后VS2008使用MFC出现问题的解决方法
- 关于在extjs中使用column布局,不能显示textfield的标签(fieldLabel)解决方法
- 关于使用VS.Net2003调试器出现的问题及相关解决方法[转]
- 关于ArrayList使用时常遇到的问题以及解决方法
- 关于解决apt-get 产生404问题的解决方法
- 关于在英创em9161板上使用ADO.net连接远程数据库提示 “无法找到 PInvoke dll"dbnetlib.dll"”问题解决方法
- 关于更改ip后dbconsole服务不能启动(em不能使用)问题的根本解决方法
- 关于使用VS.Net2003调试器出现的问题及相关解决方法
- csshover.htc 以及简单使用方法(解决IE6下 :hover 在其他标签下无效问题)
- 关于在使用Exchange2003系统时无法向sina,yahoo,hotmail等邮箱发送邮件问题的解决方法
- 关于使用gVim编写Java源文件时产生乱码的解决方法
- 关于使用CreateUserWinzard控件创建用户时出现连接数据库问题的解决方法
- 关于安装IE8之后VS2008使用MFC出现问题的解决方法
- 关于在多处理器suse11上中断处理中使用current宏的问题及解决方法
- 关于PPT制作和使用的个别问题提出来的解决方法
- 关于CSS中内层使用float导致外层的背景无法显示的问题的解决方法
- 关于使用VS.Net2003调试器出现的问题及相关解决方法
- Fedora14 使用虚拟机桥接上网 静态配置固定IP 和宿主机IP在同一网段的方法 及 若干问题解决(NFS挂载第一步)