改变滚动条样式,适用于webkit内核的浏览器,平滑返回顶部功能实现

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title>改变滚动条样式,适用于webkit内核的浏览器,平滑返回顶部功能实现</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
body{
	background-color: #2a3542;
	color: #797979;
	overflow: hidden;
}
.scroll-bar{
	position: relative;
	width: 1000px;
	height: 800px;
	border: 1px solid #666666;
	margin-left: auto;
	margin-right: auto;
	margin-top: 5%;
	text-align: center;
	overflow: auto;
}

/* 定义滚动条的宽度 */
::-webkit-scrollbar{
    width: 5px;
}
/* 滚动条的滑轨背景色以及圆角度数 */
::-webkit-scrollbar-track{
    background-color: #666666;
    /* -webkit-border-radius: 6px; */
}
/* 滚动条滚动轨迹纵向背景 */
::-webkit-scrollbar-thumb:vertical{
    height: 10px;
    background-color: #FFAA33;
    -webkit-border-radius: 6px;
}
/* 滚动条滚动轨迹横向背景 */
::-webkit-scrollbar-thumb:horizontal{
    width: 5px;
    background-color: #FFAA33;
    -webkit-border-radius: 6px;
}
/* 鼠标滑过滚动条背景色 */
::-webkit-scrollbar-thumb:hover{
	background-color: #CC6600;
}

.tdTop{
	position: fixed;
    right: 20px;
    bottom: -80px;
    z-index: 999;
}
.tdTop .btn{
	width: 50px;
    height: 50px;
    margin-bottom: 1px;
    cursor: pointer;
    position: relative;
    border-radius: 50%;
}
.tdTop .btn-top{
	background: url("gotop.png");
}
.tdTop .btn-top:hover{
	background-position: 50px 50px;
}
</style>
</head>
<body>
<div class="scroll-bar" id="scroll-bar"></div>

<div id="tdTopBox" class="tdTop">
	<div id="btn_top" class="btn btn-top" style="display: none;"></div>
</div>

<script type="text/javascript">
var tdTopBoxObj = document.getElementById("tdTopBox"),
	scrollObj = document.getElementById("scroll-bar"),
	showHideObj = document.getElementById("btn_top"),
	speed = 25; /* 滚动速度,值越大,滚动速度越慢  */
	
	
/* 向页面添加内容,用于让滚动条出现 */
for(var i=0;i<100;i++){
	var p_node = document.createElement("p");
	p_node.innerHTML = "第   "+i+" 个";
	scrollObj.appendChild(p_node);
}
/* 监听滚动事件,用于判断是否显示隐藏返回顶部按钮 */
scrollObj.onscroll = function(){
	if(scrollObj.scrollTop > 100){
		if(showHideObj.style.display!="block"){
			showHideObj.style.display = "block";
			animateToShow(-80);
		}
	}else{
		if(showHideObj.style.display!="none"){
			animateToHide(25);
		}
	}
}
/* 显示返回顶部按钮动画 */
function animateToShow(x){
	x = x+5;
	setTimeout(function(){
		tdTopBoxObj.style.bottom = x+"px";
		if(x < 25){
			animateToShow(x);
		}
	},5);
}
/* 隐藏返回顶部按钮动画 */
function animateToHide(x){
	x = x-5;
	setTimeout(function(){
		tdTopBoxObj.style.bottom = x+"px";
		if(x > -80){
			animateToHide(x);
		}else{
			showHideObj.style.display = "none";
		}
	},5);
}

/* 绑定事件,让滚动条返回顶部 */
tdTopBoxObj.onclick = function(){
	goTop(scrollObj.scrollTop);
};
function goTop(x){
	if(x > 100){
		x = x-100;
		setTimeout(function(){
			scrollObj.scrollTop = x;
			goTop(x);
		},speed);
	}else{
		scrollObj.scrollTop = 0;
	}
};
</script>
</body>
</html>


下面是返回顶部按钮图片:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值