JS uses absolute positioning to get back to the top effect full example

  • 2021-06-29 09:45:15
  • OfStack

This article provides an example of JS's absolute positioning back to the top.Share it for your reference, as follows:


<!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=utf-8" />
<title> Absolute Position Back to Top Button </title>
<style type="text/css">
body{margin:0px;padding:0px;height:2500px;background:#6f0024;}
#div1{width:120px;height:34px;right:4px;bottom:5px;cursor:pointer;background:url(images/ToTop.png) no-repeat;position:fixed;_position:absolute;display:none;}
</style>
<script type="text/javascript">
// Absolute positioning hidden display 
function getScroll(id){
 var obj = document.getElementById(id);
 var timer = null;
 positionFixed(obj);
 if(obj){
 obj.style.display = 'none';
 window.onscroll=function(){
  getScrollTop() > 0 ? obj.style.display = "block" : obj.style.display = "none";
 }
 obj.onclick=function(){
  var timer = setInterval(sMove,10);
  function sMove(){
  setScrollTop(getScrollTop() / 1.5);
  if(getScrollTop() < 1)clearInterval(timer);
  }
 }
 }
}
// judge IE6
function positionFixed(obj){
 var iE6 = !-[1,] && !window.XMLHttpRequest;
 if(obj){
     var top = obj.offsetTop;
 if(iE6){
  document.documentElement.style.textOverflow = "ellipsis";
  obj.style.position = "absolute";
  obj.style.setExpression("top", "eval(documentElement.scrollTop + " + top + ') + "px"');
 }
 }
}
// Get Scrollbar Top
function getScrollTop(){
 return document.documentElement.scrollTop || document.body.scrollTop;
}
// Back to the top 
function setScrollTop(value){
 document.documentElement.scrollTop = value;
 document.body.scrollTop = value;
}
window.onload = function(){
 getScroll('div1');
};
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>

More readers interested in JavaScript related content can view this site's topics: JavaScript Switching Special Effects and Techniques Summary, JavaScript Finding Algorithmic Techniques Summary, JavaScript Animation Special Effects and Techniques Summary, JavaScript Errors and Debugging Techniques Summary, JavaScript Data Structure and Algorithmic Techniques Summary,Summary of JavaScript Traversal Algorithms and Techniques and Summary of JavaScript Mathematical Operation Usage

I hope that the description in this paper will be helpful to everyone's JavaScript program design.


Related articles: