• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

sunshinezjb

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

点击回到顶部

html:

 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>点击回到顶部</title>
<link href="css/demo1.css" rel="stylesheet" />
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/demo1.js"></script>
</head>
<body>
<div id="d1"></div>
</body>
</html>

 

css:

 

* {
padding:0px;
margin:0px;
}

body{/*窗体加个背景图片 设置窗体的高度(图片的高度)*/
height:4773px;
background-image:url(../img/bg.png);

}

/*按钮的宽度 高度 按钮图片 */
#d1{
width:96px;
height:96px;
background-image:url(../img/top.gif);
position:fixed;/*固定定位*/
right:0px;
bottom:0px;
display:none;/*隐藏*/
}

 

js:

 

/// <reference path="jquery-1.10.2.min.js" />

var timer;
$(function () {
var dHeight = $(window).height();//获取窗体的高度
$(document).scroll(function () {//对网页执行的一个scroll事件
var top = $(document).scrollTop();//拖动滚动条的时候 距离浏览器顶部的距离
if (top > dHeight) {
$("#d1").show();
} else {
$("#d1").hide();
}
});

//点击箭头的时候 回到顶部
$("#d1").click(function () {
timer = setInterval(function () {
var backtop = $(document).scrollTop();
var step = backtop / 5;//每次移动多少
$(document).scrollTop(backtop - step);
if (backtop == 0) {
clearInterval(timer);
}
}, 30);
});

});

posted on 2018-03-12 17:37  sunshinezjb  阅读(246)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3