jQuery实现分章节锚点“回到顶部”动画特效代码


Posted in Javascript onOctober 23, 2015

本文实例讲述了jQuery实现分章节锚点“回到顶部”动画特效。分享给大家供大家参考,具体如下:

这里演示基于jquery实现的分章节动画实现“回到顶部”的效果,可通过 网页顶部的数字序号直接进入网页的章节,当处于第二章节的时候,网页右侧会显示竖排的控制按钮,点击按钮会回到相应章节,其实也就是定义好的锚点,当然也可回到顶部,网页上见到的回顶部大多不是这个样子,所以本款效果还挺新颖。

运行效果截图如下:

jQuery实现分章节锚点“回到顶部”动画特效代码

在线演示地址如下:

具体代码如下:

<!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>
<title>jQuery锚点带动画跳转特效</title>
<script src="jquery-1.6.2.min.js" type="text/javascript"></script>
<style>
.gray {
 -webkit-filter: grayscale(100%);
 -moz-filter: grayscale(100%);
 -ms-filter: grayscale(100%);
 -o-filter: grayscale(100%);
 filter: grayscale(100%);
 filter: gray;
}
/* reset */
body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, p , form, fieldset, legend, input, button, textarea, th, td {
 margin:0;
 padding:0;
}
table {
 border-collapse:collapse;
 border-spacing:0;
}
fieldset, img {
 border:0;
}
ul,li {
 list-style:none;
}
body {
 background: #f1f1f1;
 color: #666;
 font:12px/1.7 Helvetica,Arial,Tahoma,sans-serif,"\5B8B\4F53";
}
.row:after {
 clear:both;
 content:'\0020';
 display:block;
 height:0;
}
.row {
 zoom:1;
}
/*链接颜色*/
a,button{outline:none; /*移除虚线框 IE8,FF有用*/ hide-focus: expression(this.hideFocus=true); /*IE6、IE7*/}
a{
 color:#999;
 text-decoration:none;
}
a:hover{
 color:#be0000;
 text-decoration: underline;
}
.fst{
 font-family: "\5B8B\4F53";
}
h1,h2,h3,h4,h5,h6{font-family:\5FAE\8F6F\96C5\9ED1; font-size: 16px;}
/* end reset */
.row{
 width:1000px;
 margin:0 auto;
}
/*模板头部,所有专题页面公用*/
.index_nav{width:575px; height: 70px; margin: 0 auto; padding-left: 5px; background: #fff;}
.index_nav li{width: 115px; height: 70px; float: left;}
.index_nav li a{display: block; float: left; width: 70px;height: 70px; background:url(images/index_nav.gif) 0 0 no-repeat; text-align: center; color: #e02800;}
.index_nav li a i{display: block; padding-top: 12px; height: 22px; line-height: 22px;}
.index_nav li a strong{font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 24px; display: block; height: 30px; line-height: 20px;}
.index_nav li a:hover{background-position: 0 -70px; color: #fff; text-decoration: none;}
.h15{height: 15px; overflow: hidden;}
.mainpage{width: 1000px; height:1000px;}
h2{font-size: 24px; text-align: center; color: #333; font-weight: bold;}
.mainpage p{width: 650px; margin: 0 auto; color: #fff; font-size: 16px; padding: 50px 0 50px;}
.mainpage1{background: #fe6400;}
.mainpage2{background: #be0000;}
.mainpage3{background: #ccc;}
.mainpage4{background: #0093dd;}
.mainpage5{background: #a8cf33;}
*html{background-image:url(about:blank);background-attachment:fixed;}/*低版本浏览器防止抖动的,必须有*/
#tbox{width:30px; height:200px; float:right; position:fixed; display: none;
_position:absolute;
_bottom:auto;
_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));
_margin-bottom:50px;
}/*这个只能改宽高其他的不要修改,需要滚动的内容都要放在这个div里面*/
.taoba{display:block; background:#c2c2c2; width:30px; height:30px; margin-bottom:4px;text-align: center; color: #fff; line-height: 30px; overflow: hidden;cursor:pointer}
.taoba:hover{background:#ff4200; text-decoration: none; color: #fff;}
#gotop{display:block; width:30px; height:30px; text-indent:-9999px; overflow: hidden; color: #fff; background:url(images/top_icon.gif) no-repeat; position:absolute; display:none; cursor:pointer} /*这个样式随便改,必须有position:absolute; */
#gotop:hover{background-position: 0 -30px;}
</style>
<script type="text/javascript">
 jQuery(document).ready(function($) {
 $(".index_nav li a").click(function(event) { 
  var index=this.title
  var id='#'+'index_'+index
  $("html,body").animate({scrollTop: $(id).offset().top}, 1000);
 });
 $(".taoba").click(function(event) { 
  var index=this.title
  var id='#'+'index_'+index
  $("html,body").animate({scrollTop: $(id).offset().top}, 1000);
 });
 function a(x,y){
 l = $('#main').offset().left;
 w = $('#main').width();
 $('#tbox').css('left',(l + w + x) + 'px');
 $('#tbox').css('bottom',y + 'px');
}//获取#tbox的div距浏览器底部和页面内容区域右侧的距离函数#main为页面的可视宽度
$(function() { 
 $(window).scroll(function(){
 t = $(document).scrollTop();
 if(t>500){
  $('#tbox').show();
 }else{
  $('#tbox').hide();
 }
 if(t > 50){
  $('#gotop').fadeIn('slow');
 }else{
  $('#gotop').fadeOut('slow');
 }  
}) 
 a(10,100);//#tbox的div距浏览器底部和页面内容区域右侧的距离
 $('#gotop').click(function(){ 
  $('body,html').animate({
   scrollTop: 0
  },
  800);//点击回到顶部按钮,缓懂回到顶部,数字越小越快
  return false; 
 })
});
 });
</script>
</head>
<body>
<div class="indexnav_wrap">
 <ul class="index_nav">
 <li><a href="javascript:void(0)" title="1"><i>活动</i><strong>1</strong></a></li>
 <li><a href="javascript:void(0)" title="2"><i>活动</i><strong>2</strong></a></li>
 <li><a href="javascript:void(0)" title="3"><i>活动</i><strong>3</strong></a></li>
 <li><a href="javascript:void(0)" title="4"><i>活动</i><strong>4</strong></a></li>
 <li><a href="javascript:void(0)" title="5"><i>活动</i><strong>5</strong></a></li>
 </ul>
</div>
<div class="h50"></div>
<div class="row" id="main">
 <h2 id="index_1">这是第一个活动页</h2>
 <div class="mainpage mainpage1"></div>
</div>
<div class="h15"></div>
<div class="row">
 <h2 id="index_2">这是第二个活动页</h2>
 <div class="mainpage mainpage2"></div>
</div>
<div class="h15"></div>
<div class="row">
 <h2 id="index_3">这是第三个活动页</h2>
<div class="mainpage mainpage3"></div>
</div>
<div class="h15"></div>
<div class="row">
 <h2 id="index_4">这是第四个活动页</h2>
 <div class="mainpage mainpage4"></div>
</div>
<div class="h15"></div>
<div class="row">
 <h2 id="index_5">这是第五个活动页</h2>
 <div class="mainpage mainpage5"></div>
</div>
<div id="tbox"><!-- 这个必须有 id="tbox"-->
 <a class="taoba" href="javascript:void(0)" title="1">1</a>
 <a class="taoba" href="javascript:void(0)" title="2">2</a>
 <a class="taoba" href="javascript:void(0)" title="3">3</a>
 <a class="taoba" href="javascript:void(0)" title="4">4</a>
 <a class="taoba" href="javascript:void(0)" title="5">5</a>
 <a id="gotop" href="javascript:void(0)" title="回到顶部">回到顶部</a> <!-- 这个也是id="gotop" -->
</div>
</div>
</body>
</html>

希望本文所述对大家jQuery程序设计有所帮助。

Javascript 相关文章推荐
jquery实现表格奇数偶数行不同样式(有图为证及实现代码)
Jan 23 Javascript
js css后面所带参数含义介绍
Aug 18 Javascript
JS+CSS实现可拖拽的漂亮圆角特效弹出层完整实例
Feb 13 Javascript
简化版手机端照片预览组件
Apr 13 Javascript
JavaScript使用DeviceOne开发实战(四)仿优酷视频应用
Dec 02 Javascript
js H5 canvas投篮小游戏
Aug 18 Javascript
关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)
Dec 14 Javascript
javascript 闭包详解及简单实例应用
Dec 31 Javascript
微信小程序支付及退款流程详解
Nov 30 Javascript
使用Angular CLI从蓝本生成代码详解
Mar 24 Javascript
利用vue重构有赞商城的思路以及总结整理
Feb 21 Javascript
vue父组件触发事件改变子组件的值的方法实例详解
May 07 Javascript
JavaScript代码轻松实现网页内容禁止复制(代码简单)
Oct 23 #Javascript
深入浅析javascript立即执行函数
Oct 23 #Javascript
js判断手机号运营商的方法
Oct 23 #Javascript
jQuery实现的网页右下角tab样式在线客服效果代码
Oct 23 #Javascript
js计算文本框输入的字符数
Oct 23 #Javascript
jQuery实现的网页左侧在线客服效果代码
Oct 23 #Javascript
JS实现超简洁网页title标题跑动闪烁提示效果代码
Oct 23 #Javascript
You might like
冰滴咖啡制作步骤
2021/03/03 冲泡冲煮
天津市收音机工业发展史
2021/03/04 无线电
PHPShop存在多个安全漏洞
2006/10/09 PHP
PHP setcookie() cannot modify header information 的解决方法
2009/01/09 PHP
CodeIgniter框架提示Disallowed Key Characters的解决办法
2014/04/21 PHP
php页面函数设置超时限制的方法
2014/12/01 PHP
PHP微信红包API接口
2015/12/05 PHP
PHP动态生成指定大小随机图片的方法
2016/03/25 PHP
PHP中十六进制颜色与RGB颜色值互转的方法
2019/03/18 PHP
PHP全局使用Laravel辅助函数dd
2019/12/26 PHP
JQuery Dialog的内存泄露问题解决方法
2010/06/18 Javascript
jQuery中[attribute^=value]选择器用法实例
2014/12/31 Javascript
javascript实现Table间隔色以及选择高亮(和动态切换数据)的方法
2015/05/14 Javascript
基于jQuery实现在线选座之高铁版
2015/08/24 Javascript
js 判断附件后缀的简单实现方法
2016/10/11 Javascript
AngularJS打开页面隐藏显示表达式用法示例
2016/12/25 Javascript
深入理解ES6学习笔记之块级作用域绑定
2017/08/19 Javascript
使用jQuery实现两个div中按钮互换位置的实例代码
2017/09/21 jQuery
微信小程序图片轮播组件gallery slider使用方法详解
2018/01/31 Javascript
Node.js成为Web应用开发最佳选择的原因
2018/02/05 Javascript
vue-router权限控制(简单方式)
2018/10/29 Javascript
jQuery实现的简单歌词滚动功能示例
2019/01/07 jQuery
JS浮点数运算结果不精确的Bug解决
2019/08/01 Javascript
vue实现的多页面项目如何优化打包的步骤详解
2020/07/19 Javascript
python网页请求urllib2模块简单封装代码
2014/02/07 Python
Python2.7环境Flask框架安装简明教程【已测试】
2018/07/13 Python
使用 django orm 写 exists 条件过滤实例
2020/05/20 Python
俄罗斯连接商品和买家的在线平台:goods.ru
2020/11/30 全球购物
党支部书记先进事迹
2014/01/17 职场文书
舞蹈比赛获奖感言
2014/02/04 职场文书
机械系毕业生求职信
2014/05/28 职场文书
找工作求职信
2014/07/07 职场文书
2015年小学开学寄语
2015/02/27 职场文书
2015社区精神文明建设工作总结
2015/04/21 职场文书
Vue3.0 手写放大镜效果
2021/07/25 Vue.js
vue router 动态路由清除方式
2022/05/25 Vue.js