jQuery实现锚点平滑定位

一般的锚点,就是点击一个按钮或者其他元素可以实现定位效果,当然可以使用锚点实现,但是这个不够美观,没有平滑的动画过渡效果,下面就通过代码实例介绍一下利用jquery实现平滑的定位效果。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.51texiao.cn/" />
<title></title>
<style type="text/css">
.box{
height:200px;
width:100%;
background:#ccc;
margin:10px 0;
}
.location{
position:fixed;
right:0;
bottom:10px;
width:20px;
background:#FFC;
padding:5px;
cursor:pointer;
color:#003
}
#div1{
width:100px;
height:100px;
background:green;
margin-top:150px;
}
</style>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.scroll_a').click(function(){
$('html,body').animate({scrollTop:$('#div1').offset().top}, 800);
});
});
</script>
</head>
<body style="height:1000px">
<div id="div1"></div>
<div class="location">
<p class="scroll_a">顶部</p>
</div>
</body>
</html>

其中scrollTop属性规定的是页面被向上滚动遮挡的尺寸,而offset().top则是匹配元素距离文档顶部的距离,所以设置scrollTop的属性值为offset().top就可以将指定元素定位于页面文档去的顶部边缘。

上一篇:angular在ie8下的一个bug


下一篇:IE6 浏览器常见兼容问题 共23个