JavaScript: Floating Box Following Scroll

By Xah Lee. Date: . Last updated: .
FLOATING BOX

Scroll the page down and you'll see a box that follows your window scrolling, so that it is always visible.

How It Works

The moving block is just a HTML element, like this:

<div id="heart53100">FLOATING BOX</div>

CSS is used to make it larger and red, and position it with respect to the HTML container (from top of the document).

#heart53100 {
position:absolute;
top:300px;
left:20px;
z-index:2;
width:10rem;
font-size:2rem;
color:red;
border:solid thin red
}

The position and top makes it offset 300 pixels down from top of the document. [see CSS Position: static, relative, fixed, absolute]

To move the box, we need to:

  1. Set up a scroll event, so that when user scroll, a function f is called.
  2. The function f computes the distance of the box to window's vertical center, and move the box closer if it is not already there. (moving is done by changing its position in CSS.)
  3. The function f then calls itself again, until the box is in the center.

Each time f moves the box, the distance moved is proportional to how far it is to destination. So, if it is far, it moves a large distance, but if it is very close, it moves a small distance. This gives the illusion that the box slows down like a train stop.

To make this efficient, f will first turn off the scroll event when the box is not at the center. When the box is at the center, then f turns back the scroll event. This make things more efficient, because otherwise each scroll will call f while f is still in the process of moving the box to center.

Here's the JavaScript code:

// -*- coding: utf-8 -*-
// 2010-03-10, 2018-05-22
// Copyright 2018-05-22 by Xah Lee
// http://xahlee.info/js/moving_block.html
// license: permission is granted for using the code, but this license section must remain intact.

// box following scroll
// change position of “div#heart53100”

{

const offsetFromTop = window.innerHeight/2; // number of pixels of the widget should be from top of the window
const updateFrequency= 50; //milisecond. The smaller the value, smooth the animation.
const chaseFactor = .05; // the closing-in factor. Smaller makes it smoother.

const movingBox = document.getElementById("heart53100");
movingBox.style.position="absolute";
movingBox.style.zIndex="2";
movingBox.style.top= offsetFromTop.toString() + "px";
movingBox.style.left="1ex";

let yMoveTo=0;
let yDiff=0;

const moveIt = (() => {
    // distance user has scrolled
    yDiff = (yMoveTo - window.pageYOffset);

    if ( Math.abs(yDiff) > 9) {
        // turn off now, prevent event repeat firing when user kept scrolling
        window.removeEventListener("scroll", moveIt);

        yMoveTo -= yDiff*chaseFactor;
        movingBox.style.top  = (yMoveTo+offsetFromTop).toString() + "px" ;
        setTimeout(moveIt, updateFrequency); // calls itself again
    } else {
        window.addEventListener("scroll", moveIt , false); // turn back on
    }
});

window.addEventListener("scroll", moveIt , false);

}
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|

JavaScript Fancy UI Examples

  1. JS: Image Rollover
  2. JS: Pop-up New Window
  3. JS: Digital Clock
  4. JS: Stopwatch
  5. JS: Fade a Element
  6. JS: Fade a Element Using CSS Transition
  7. JavaScript UI: Shake Element
  8. JS: How to Create Tooltip
  9. JS: Falling Snow Effect
  10. JavaScript: Floating Box Following Scroll
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc