Home > Web Front-end > HTML Tutorial > 使用锚点时如何解决顶部浮动DIV的高度占用问题_html/css_WEB-ITnose

使用锚点时如何解决顶部浮动DIV的高度占用问题_html/css_WEB-ITnose

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-21 09:23:19
Original
1262 people have browsed it
下面的这种效果,在点击链接时,切换到锚点上,但因为顶部存在一定高度的浮动DIV,点击后下面的DIV将会被遮挡住相应高度的位置看不到,这样的问题如何解决?困扰好久了,请大侠们出手相助啊,在此先表示感谢。如果能带平滑滚动更好

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><script language=javascript src="http://www.0551jia.cn/js/jquery1.42.min.js"></script><title>测试</title><style type="text/css">* {color:#fff;}.clear { clear:both; height:0; line-height:0px; font-size:0;}#ding{position:fixed;z-index:100; background-color:#036;  top:0;left:0;_position:absolute;_top:expression(documentElement.scrollTop + 0 + "px");_left:expression(documentElement.scrollLeft + 0 + "px");} #ding{width:100%;overflow:hidden; height:56px; text-align:center}.logo { background-color:#eee; height:70px;  border-bottom:#d8d8d8 1px solid; margin-bottom:3px;}.navbox{ width:1100px; margin:0 auto; z-index:998;}.navboxfix{ position:fixed; left:50%; margin-left:-550px; width:1100px;top:56px; _position:relative;}.navbox {height:36px; line-height:36px; background-color:#484441; color:#FFF}.navbox dd a{display:block;width:134px; float:left; text-align:center; color:#fff}.k {height:800px; background-color:#069;}.b {height:800px; background-color:#393}</style></head><body><div id="ding"> 固定的顶 </div><div class="clear" style="height:56px;"></div><div class="logo wk cent">    中间</div><div class="navbox">    <dl style="position:inherit">       	<dd><a href="#kk1">这是1</a></dd>       	<dd><a href="#kk2">这是2</a></dd>       	<dd><a href="#kk3">这是3</a></dd>       	<dd><a href="#kk4">这是4</a></dd>       	<dd><a href="#kk5">这是5</a></dd>    </dl></div><div class="k"><a name="kk1" id="kk1"></a>这是一</div><div class="b"><a name="kk2" id="kk2"></a>这是二</div><div class="k"><a name="kk3" id="kk3"></a>这是三</div><div class="b"><a name="kk4" id="kk4"></a>这是四</div><div class="k"><a name="kk5" id="kk5"></a>这是五</div><script type="text/javascript">var sft=$(".navbox").offset().top-56; //顶部固定DIV function shownav(){	 var s_t= document.documentElement.scrollTop || document.body.scrollTop;	 if(s_t>sft){		 $(".navbox").addClass("navboxfix")		 }		 else{		 $(".navbox").removeClass("navboxfix")	 	 	 }	 }window.onscroll=shownav;</script></body></html>
Copy after login


回复讨论(解决方案)

参考:
offsetting an html anchor to adjust for fixed header
JQuery Position:Fixed 'NAVBAR' by scrolling the page
给锚点加上个下面的样式,纯css实现。

a.anchor{    display: block;    position: relative;    top: -92px;    visibility: hidden;}
Copy after login
Copy after login

另外如果需要加上平滑滚动效果的话,可以点击查看
在线 演示结果
或者在线 演示代码

存贴备用, 实际效果地址

非常感谢您的热情回复!!!

参考:
offsetting an html anchor to adjust for fixed header
JQuery Position:Fixed 'NAVBAR' by scrolling the page
给锚点加上个下面的样式,纯css实现。

a.anchor{    display: block;    position: relative;    top: -92px;    visibility: hidden;}
Copy after login
Copy after login

另外如果需要加上平滑滚动效果的话,可以点击查看
在线 演示结果
或者在线 演示代码
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template