页面添加置顶按钮

给页面添加置顶按钮,当页面滚动到一定高度时,出现置顶按钮:

  1. 添加css样式:
    /*-----------置顶按钮-----------*/
    .to_top_btn{
    	width:76px;
    	height:54px;
    	background:url(../images/to-top.png);
    	background-position:0px 0px;
    	position:fixed;
    	bottom:95px;
    	color:white;
    	z-index:2;
    	cursor:pointer;
    	display:none;
    	_position:absolute;
    	_top:expression(eval(document.documentElement.scrollTop || document.body.scrollTop) +eval(document.documentElement.clientHeight || document.body.clientHeight)*0.7 +'px');
    	}
    .to_top_btn:hover{
    	background:url(../images/to-top.png);
    	background-position:0px 54px;
    	}
    
    /*-----------置顶按钮-----------*/
    

  2. 添加公共脚本,在window.onload中添加方法:
    //初始化置顶按钮位置
    $('.to_top_btn').css('right',($(window).width()-$('.container').width())/2-$('.to_top_btn').width()+"px");
    
    //置顶按钮
    $(window).scroll(function(){if($(document).scrollTop()>160){
    	$('.to_top_btn').fadeIn();
    	}else{
    		$('.to_top_btn').fadeOut();
    		}});
    $('.to_top_btn').click(function(){
    	var timer=setInterval(function(){
    	if($(document).scrollTop()==0){
    		clearInterval(timer);
    		}else{
    	$(document).scrollTop($(document).scrollTop()-30);
    	}
    	},5);
    });
    

  3. 在需要的页面中添加标签:
    <div class="to_top_btn"></div>
    

说明:top图标在附件中,可以自定义图标,修改CSS样式即可!目前是宽:76px,高:108px。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值