jQuery实现返回顶部功能

整理两个实现功能,一个是右下角的返回顶部,一个是右侧的返回顶部,分别如图

            

第一种实现

一、JSP或HTML(主体结构)

在body中添加

  1. <body id="top"> 
  2. <p id="back-to-top"><a href="#top"><span></span></a></p> 
  3. </body> 
<body id="top">
<p id="back-to-top"><a href="#top"><span></span></a></p>
</body>

二、CSS(样式化)
  1. <style> 
  2.     p#back-to-top
  3.         position: fixed
  4.         bottom: 50px
  5.         right: 50px
  6.     } 
  7.      
  8.     p#back-to-top a { 
  9.         text-align: center
  10.         text-decoration: none
  11.         color: #d1d1d1
  12.         display: block
  13.         width: 50px
  14.         /*使用CSS3中的transition属性给跳转链接中的文字添加渐变效果*/ 
  15.         -moz-transition: color 1s; 
  16.         -webkit-transition: color 1s; 
  17.         -o-transition: color 1s; 
  18.     } 
  19.     p#back-to-top a:hover { 
  20.         color: #979797
  21.     } 
  22.     p#back-to-top a span { 
  23.         background: #d1d1d1 url(/img/back_to_top.png) no-repeat center center
  24.         border-radius: 6px
  25.         display: block
  26.         height: 50px
  27.         width: 50px
  28.         margin-bottom: 5px
  29.           /*使用CSS3中的transition属性给<span>标签背景颜色添加渐变效果*/ 
  30.         -moz-transition: background 1s; 
  31.         -webkit-transition: background 1s; 
  32.         -o-transition: background 1s; 
  33.     } 
  34.      
  35.     #back-to-top a:hover span { 
  36.         background: #979797 url(/img/back_to_top.png) no-repeat center center
  37.     } 
  38. </style> 
<style>
	p#back-to-top {
		position: fixed;
		bottom: 50px;
		right: 50px;
	}
	
	p#back-to-top a {
		text-align: center;
		text-decoration: none;
		color: #d1d1d1;
		display: block;
		width: 50px;
	    /*使用CSS3中的transition属性给跳转链接中的文字添加渐变效果*/
		-moz-transition: color 1s;
		-webkit-transition: color 1s;
		-o-transition: color 1s;
	}
	p#back-to-top a:hover {
		color: #979797;
	}
	p#back-to-top a span {
		background: #d1d1d1 url(/img/back_to_top.png) no-repeat center center;
		border-radius: 6px;
		display: block;
		height: 50px;
		width: 50px;
		margin-bottom: 5px;
	      /*使用CSS3中的transition属性给<span>标签背景颜色添加渐变效果*/
		-moz-transition: background 1s;
		-webkit-transition: background 1s;
		-o-transition: background 1s;
	}
	
	#back-to-top a:hover span {
		background: #979797 url(/img/back_to_top.png) no-repeat center center;
	}
</style>
图片自己网上找资源

三、jQuery(动态效果)

  1. <script> 
  2. $(document).ready(function() { 
  3.     //首先将#back-to-top隐藏 
  4.     $("#back-to-top").hide(); 
  5.  
  6.     //当滚动条的位置处于距顶部100像素以下时,跳转链接出现,否则消失 
  7.     $(function() { 
  8.         $(window).scroll(function() { 
  9.             if ($(window).scrollTop() > 100) { 
  10.                 $("#back-to-top").fadeIn(1500); 
  11.             } 
  12.             else
  13.                 $("#back-to-top").fadeOut(1500); 
  14.             } 
  15.         }); 
  16.         //当点击跳转链接后,回到页面顶部位置 
  17.         $("#back-to-top").click(function() { 
  18.             $('body,html').animate({ 
  19.                 scrollTop: 0 
  20.             }, 
  21.             500); 
  22.             return false
  23.         }); 
  24.     }); 
  25. }); 
  26. </script> 
<script>
$(document).ready(function() {
    //首先将#back-to-top隐藏
    $("#back-to-top").hide();

    //当滚动条的位置处于距顶部100像素以下时,跳转链接出现,否则消失
    $(function() {
        $(window).scroll(function() {
            if ($(window).scrollTop() > 100) {
                $("#back-to-top").fadeIn(1500);
            }
            else {
                $("#back-to-top").fadeOut(1500);
            }
        });
        //当点击跳转链接后,回到页面顶部位置
        $("#back-to-top").click(function() {
            $('body,html').animate({
                scrollTop: 0
            },
            500);
            return false;
        });
    });
});
</script>

 


第二种实现
  1. <style> 
  2.     .backToTop { 
  3.     display: none
  4.     width: 18px
  5.     line-height: 1.2
  6.     padding: 5px 0
  7.     background-color: #000
  8.     color: #fff
  9.     font-size: 12px
  10.     text-align: center
  11.     position: fixed
  12.     _position: absolute
  13.     right: 10px
  14.     bottom: 100px
  15.     _bottom: "auto"
  16.     cursor: pointer
  17.     opacity: .6
  18.     filter: Alpha(opacity = 60); 
  19. </style> 
<style>
	.backToTop {
	display: none;
	width: 18px;
	line-height: 1.2;
	padding: 5px 0;
	background-color: #000;
	color: #fff;
	font-size: 12px;
	text-align: center;
	position: fixed;
	_position: absolute;
	right: 10px;
	bottom: 100px;
	_bottom: "auto";
	cursor: pointer;
	opacity: .6;
	filter: Alpha(opacity = 60);
}
</style>
 
  1. <script> 
  2. (function() { 
  3.     var $backToTopTxt = "返回顶部" 
  4.     $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body")).text($backToTopTxt) 
  5.     .attr("title", $backToTopTxt).click(function() { 
  6.         $("html, body").animate({ 
  7.             scrollTop: 0 
  8.         },120); 
  9.     }) 
  10.     $backToTopFun = function() { 
  11.         var st = $(document).scrollTop(), 
  12.         winh = $(window).height(); (st > 0) ? $backToTopEle.show() : $backToTopEle.hide(); 
  13.         //IE6下的定位 
  14.         if (!window.XMLHttpRequest) { 
  15.             $backToTopEle.css("top", st + winh - 166); 
  16.         } 
  17.     }; 
  18.     $(window).bind("scroll", $backToTopFun); 
  19.     $(function() { 
  20.         $backToTopFun(); 
  21.     }); 
  22. })(); 
  23. </script> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值