"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to keep an element fixed at the bottom, even when the keyboard is open

How to keep an element fixed at the bottom, even when the keyboard is open

Published on 2024-08-25
Browse:378

Hey devs! I discovered this while working on my note-taking app.
Here's a post using my app.
Sharing the progress on X (Twitter)

Have you ever been surprised that CSS isn't working the way you expected?
That happened to me (again) when I set an element to be fixed at the bottom and then opened the keyboard on my iPhone.

What I've seen is that element is not visible at all.
Because it's fixed. To the bottom. Behind the keyboard.

How to keep an element fixed at the bottom, even when the keyboard is open

Seems like to fix the fixed we need some JS.

VisualViewport

There's a browser API with good support that can be used for these purposes: VisualViewport.
It returns the width and height of the actual visible viewport. MDN link to docs.
However, do your own investigation to see if it's supported for the versions you're targeting.

How to keep an element fixed at the bottom, even when the keyboard is open

Math

Basically, we need to handle the position of the element with respect to the visual viewport, as well as the scroll position and the element's height. Let's do the math.

Also, since the math is much simpler this way, it makes sense to use the top parameter instead of the bottom.

top = viewport height   scroll - element height

Implementation

I'll use React. For any other framework, you can just copy the content of the useEffect hook.

import { useEffect, useState } from 'react';
import classNames from 'classnames';
import { useDebounce } from 'use-debounce';

const elementHeight = 55; // elem. height in pixels
// It's also a good idea to calculate it dynamically via ref

export const FixedBlock = () => {
  // top postion -> the most important math result goes here
  const [top, setTop] = useState(0);

  useEffect(() => {
    function resizeHandler() {
      // viewport height
      const viewportHeight = window.visualViewport?.height ?? 0;
      // math
      setTop(viewportHeight   window.scrollY - elementHeight)
    }

    // run first time to initialize 
    resizeHandler();

    // subscribe to events which affect scroll, or viewport position
    window.visualViewport?.addEventListener('resize', resizeHandler);
    window.visualViewport?.addEventListener('scroll', resizeHandler);
    window?.addEventListener('touchmove', resizeHandler);

    // unsubscribe
    return () => {
      window.visualViewport?.removeEventListener('resize', resizeHandler);
      window.visualViewport?.removeEventListener('scroll', resizeHandler);
      window?.removeEventListener('touchmove', resizeHandler);
    };
  }, [debouncedScroll]);

  return (
    
      
I am fixed
> ); };

Final results

I also needed to add some animations and hide my block on scroll, but you don't have to do that, and it will always be visible.

How to keep an element fixed at the bottom, even when the keyboard is open

Release Statement This article is reproduced at: https://dev.to/rita/how-to-keep-an-element-fixed-at-the-bottom-even-when-the-keyboard-is-open-n0m?1 Any infringement , please contact [email protected] to delete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3