锚链接,就是当你点击链接时,页面滚动至锚的位置,百度百科的目录就是这个思路,点击某个章节,页面滚动至该章节。
实现方式如下:
1、设置页面滚动到的元素id,并在前面加#
<a href="#anchor">点击这个锚链接,页面将滚动至标记位置</a>
这样点击上面链接后,页面即可滚动至对应id的元素的位置 <span id="anchor">**********</span>
2、js也可实现此效果
window.location.hash='#anchor'
效果如下,请另存为html试验:
<!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=utf-8" />
<title>锚链接</title>
</head>
<body>
<a href="#anchor">点击这个锚链接,页面将滚动至标记位置</a>
<span οnclick="window.location.hash='#anchor'">点这里用js实现同样功能</span>
<div style="width:100px; height:1000px;"></div>
<span id="anchor">哈哈哈!!!!!!!!!!!!!</span>
<div style="width:100px; height:1000px;"></div>
</body>
</html>