js+css实现回到顶部按钮(back to top)
作者:bea
本文实例介绍了js+css实现回到顶部按钮的方法,分享给大家供大家参考,具体内容如下 效果 html <p id="back-to-top"><a href="#top"><span></span></a></p> css p#back-to-top{ position:fixed; bottom:100px; right:80px;}p#back-to-top a{ text-ali
本文实例介绍了js+css实现回到顶部按钮的方法,分享给大家供大家参考,具体内容如下
效果
html
<p id="back-to-top"><a href="#top"><span></span></a></p>
css
p#back-to-top{
position:fixed;
bottom:100px;
right:80px;
}
p#back-to-top a{
text-align:center;
text-decoration:none;
color:#d1d1d1;
display:block;
width:30px;
/*使用CSS3中的transition属性给跳转链接中的文字添加渐变效果*/
-moz-transition:color1s;
-webkit-transition:color1s;
-o-transition:color1s;
}
p#back-to-top a:hover{
color:#979797;
}
p#back-to-top a span{
background:#d1d1d1 url(../images/arrow_up.png) no-repeat center center;
border-radius:6px;
display:block;
height:30px;
width:30px;
margin-bottom:5px;
/*使用CSS3中的transition属性给<span>标签背景颜色添加渐变效果*/
-moz-transition:background1s;
-webkit-transition:background1s;
-o-transition:background1s;
}
#back-to-top a:hover span{
background:#979797 url(../images/arrow_up.png) no-repeat center center;
}
js
<script type="text/javascript">
$(document).ready(function(){
//首先将#back-to-top隐藏
$("#back-to-top").hide();
//当滚动条的位置处于距顶部600像素以下时,跳转链接出现,否则消失
$(function () {
$(window).scroll(function(){
if ($(window).scrollTop()>600){
$("#back-to-top").fadeIn(500);
}else{
$("#back-to-top").fadeOut(500);
}
});
//当点击跳转链接后,回到页面顶部位置
$("#back-to-top").click(function(){
$('body,html').animate({scrollTop:0},500);
return false;
});
});
});
</script>
以上就是本文的全部内容,希望对大家的学习有所帮助。
有用 | 无用
效果
html
<p id="back-to-top"><a href="#top"><span></span></a></p>
css
p#back-to-top{
position:fixed;
bottom:100px;
right:80px;
}
p#back-to-top a{
text-align:center;
text-decoration:none;
color:#d1d1d1;
display:block;
width:30px;
/*使用CSS3中的transition属性给跳转链接中的文字添加渐变效果*/
-moz-transition:color1s;
-webkit-transition:color1s;
-o-transition:color1s;
}
p#back-to-top a:hover{
color:#979797;
}
p#back-to-top a span{
background:#d1d1d1 url(../images/arrow_up.png) no-repeat center center;
border-radius:6px;
display:block;
height:30px;
width:30px;
margin-bottom:5px;
/*使用CSS3中的transition属性给<span>标签背景颜色添加渐变效果*/
-moz-transition:background1s;
-webkit-transition:background1s;
-o-transition:background1s;
}
#back-to-top a:hover span{
background:#979797 url(../images/arrow_up.png) no-repeat center center;
}
js
<script type="text/javascript">
$(document).ready(function(){
//首先将#back-to-top隐藏
$("#back-to-top").hide();
//当滚动条的位置处于距顶部600像素以下时,跳转链接出现,否则消失
$(function () {
$(window).scroll(function(){
if ($(window).scrollTop()>600){
$("#back-to-top").fadeIn(500);
}else{
$("#back-to-top").fadeOut(500);
}
});
//当点击跳转链接后,回到页面顶部位置
$("#back-to-top").click(function(){
$('body,html').animate({scrollTop:0},500);
return false;
});
});
});
</script>
以上就是本文的全部内容,希望对大家的学习有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- Webpack 实现 AngularJS 的延迟加载
- 浅谈JS原型对象和原型链
- jquery单击事件和双击事件冲突解决方案
- jQuery实现只允许输入数字和小数点的方法
- jQuery Mobile开发中日期插件Mobiscroll使用说明
- javascript求日期差的方法
- 基于JavaScript实现表单密码的隐藏和显示出来
- jQuery判断浏览器并动态调整select宽度的方法
- js实现不重复导入的方法
- 基于JavaScript实现智能右键菜单
- JS简单实现String转Date的方法
- 基于Jquery和html5实现炫酷的3D焦点图动画
- JS判断是否长按某一键的方法
- JS实现为排序好的字符串找出重复行的方法
- 使用NodeJs 开发微信公众号(三)微信事件交互实例
- 在页面中输出当前客户端时间javascript实例代码
- 基于jQuery实现仿51job城市选择功能实例代码
- 基于JavaScript实现类似于百度学术高级检索功能
- js密码强度实时检测代码