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"
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Absolute Position Back to Top Button </title>
<style type="text/css">
#div1{width:120px;height:34px;right:4px;bottom:5px;cursor:pointer;background:url(images/ToTop.png) no-repeat;position:fixed;_position:absolute;display:none;}
<script type="text/javascript">
// Absolute positioning hidden display 
function getScroll(id){
 var obj = document.getElementById(id);
 var timer = null;
 obj.style.display = 'none';
  getScrollTop() > 0 ? obj.style.display = "block" : obj.style.display = "none";
  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;
     var top = obj.offsetTop;
  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(){
<div id="div1"></div>

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: