jQuery 返回顶部

比较简单的一个东西,记录下:

主要是监控$(window).scroll()事件。

jquery.top.js

/**
 * jQuery返回顶部
 * @author:xuzengqiang
 * @since :2015-1-30 14:45:59
**/ 
;(function($){
	jQuery.fn.backTop = function(options){
		var defaultOptions = {
				hideIndex:200, //隐藏的位置
				hideTimer:1000, //隐藏返回按钮所用时间
				backIndex:0, //移动的位置,默认返回顶部
				backTimer:500 //返回顶部所需时间
			};
		var settings = jQuery.extend(defaultOptions, options || {}),
			current = $(this);
		
		
		$(window).scroll(function(){
			var timer = settings.hideTimer;
			if($(this).scrollTop()>=settings.hideIndex){  
				current.fadeIn(timer);  
			} else {  
				current.fadeOut(timer);
			}     
    	}); 
		return this.each(function(){
			current.click(function(){
				$("body,html").stop(false,true).animate({scrollTop:settings.backIndex},settings.backTimer);				
			});
		});
	};
})(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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
	*{margin:0;padding:0;font-family:微软雅黑;}
	body{height:10000px;}
	.rollTop{height:40px;width:40px;position:fixed;left:95%;top:90%;display:none;}
</style>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript" src="jquery.top.js"></script>
</head>

<body>
	<button class="rollTop"></button>
</body>
<script type="text/javascript">  
jQuery(document).ready(function(){  
      $(".rollTop").backTop({"backTimer":200}); 
});  
</script>
</html>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值