使用jquery animate创建平滑滚动效果(可以是到顶部、到底部或指定地方)
采用锚点进行页面中的跳转的确很方便,但是要想增加网页的效果,可以使用jquery中的animate,实现滚动的一个动作,慢慢的滚动到你想跳转到的位置,从而看起来会非常高大上。
滚动到顶部:
. 代码如下:
$('.scroll_top').click(function(){$('html,body').animate({scrolltop: '0px'}, 800);});
滚动到指定位置:
. 代码如下:
$('.scroll_a').click(function(){$('html,body').animate({scrolltop:$('.a').offset().top}, 800);});
完整实例参考:
. 代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>js平滑滚动到顶部、底部、指定地方</title>
<script type="text/javascript" src="https://cdn.staticfile.org/jquery/2.1.1-rc2/jquery.min.js"></script>
<style>
.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};
</style>
</head>
<body>
<p class="box"></p>
<p class="box"></p>
<p class="box"></p>
<p class="box"></p>
<p class="box a">产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍</p>
<p class="box"></p>
<p class="box"></p>
<p class="box"></p>
<p class="box"></p>
<p class="box bottom"></p>
<p class="location">
<p class="scroll_top">返回顶部</p>
<p class="scroll_a">产品介绍</p>
<p class="scroll_bottom">滑到底部</p>
</p>
<script type="text/javascript">
jquery(document).ready(function($){
$('.scroll_top').click(function(){$('html,body').animate({scrolltop: '0px'}, 800);});
$('.scroll_a').click(function(){$('html,body').animate({scrolltop:$('.a').offset().top}, 800);});
$('.scroll_bottom').click(function(){$('html,body').animate({scrolltop:$('.bottom').offset().top}, 800);});
});
</script>
</body>
</html>