首页 /  web前端 /  html/css / html页面内锚点定位

0 15

html页面内锚点定位

URL地址不会变,点击锚点会自动改变滚动条

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>锚点定位</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<style type="text/css">
.content{height:200px;line-height:200px;text-align: center;background: #ccc;margin:20px;}
.top{margin:10px;}
.top a{display: inline-block;padding:3px 5px;margin:10px;}
</style>
</head>
<body>
<div class="top">
   <a href="javascript:;" data-anchorpoint-to="name1">name1</a>
   <a href="javascript:;" data-anchorpoint-to="name2">name2</a>
   <a href="javascript:;" data-anchorpoint-to="name3">name3</a>
   <a href="javascript:;" data-anchorpoint-to="name4">name4</a>
   <a href="javascript:;" data-anchorpoint-to="name5">name5</a>
</div>
<div class="content" data-anchorpoint-top="name1">name1</div>
<div class="content" data-anchorpoint-top="name2">name2</div>
<div class="content" data-anchorpoint-top="name3">name3</div>
<div class="content" data-anchorpoint-top="name4">name4</div>
<div class="content" data-anchorpoint-top="name5">name5</div>
<div class="top">
   <a href="javascript:;" data-anchorpoint-to="name1">name1</a>
   <a href="javascript:;" data-anchorpoint-to="name2">name2</a>
   <a href="javascript:;" data-anchorpoint-to="name3">name3</a>
   <a href="javascript:;" data-anchorpoint-to="name4">name4</a>
   <a href="javascript:;" data-anchorpoint-to="name5">name5</a>
</div>
<script type="text/javascript">
jQuery(function(){
	jQuery(".top a").unbind().click(function(){
		var name = jQuery(this).attr("data-anchorpoint-to");
		anchorpoint(name);
	});
});
function anchorpoint(name){
	var sctop = jQuery("[data-anchorpoint-top='"+name+"']").offset().top;
	jQuery("body,html").animate({scrollTop:sctop},800);
}
</script>
</body>
</html>

[来源] https://yuanmouren.erlyun.com/p.php?id=56

[声明] 本站资源来自用户分享,如损害你的权益请联系客服QQ:120074275给予处理。

添加一条新回复
容若 2019-09-07 19:11
10
送花
20积分一朵