Create floating button that accompanies a table scroll

Asked

Viewed 8,000 times

1

I need to create a floating button that accompanies my scroll as I go along, and I want to put a function back on top.

#voltarAoTopo {
  float:right;
  z-index:0;
}

<button id="voltarAoTopo">
    <i class="material-icons">contacts</i>
</button>   

Tabela

  • 1

    Just put position: fixed

  • but how to put under my body, is appearing the image I put for you ?

  • Speaks Nicola! Blz? Face, gives an end to this question marking in an answer. It doesn’t have to be mine, but the one you thought was best, so it doesn’t have to be open. Abs!

4 answers

2

Just create a fixed button in the bottom right corner, and put a Javascript function to return to the top. The button will only appear after 20 pixels of scroll (this you can adjust as you want):

window.onscroll = function() {scrollFunction()};

function scrollFunction() {
    if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
        document.getElementById("myBtn").style.display = "block";
    } else {
        document.getElementById("myBtn").style.display = "none";
    }
}

function topFunction() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
}
p{
display: block; height: 2500px;}

#myBtn {
  display: none;
  position: fixed;
  bottom: 20px;
  right: 30px;
  z-index: 9999;
  border: none;
  outline: none;
  background-color: red;
  color: white;
  cursor: pointer;
  padding: 15px;
  border-radius: 10px;
}

#myBtn:hover {
  background-color: #555;
}
<p>Role para baixo</p>
<button onclick="topFunction()" id="myBtn" title="Ir ao topo">Topo</button>

Bonus: Animation using pure JS

Code credit at this link.

window.onscroll = function() {scrollFunction()};

window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame    ||
          function( callback ){
            window.setTimeout(callback, 1000 / 60);
          };
})();

function scrollToY(scrollTargetY, speed, easing) {
    // scrollTargetY: the target scrollY property of the window
    // speed: time in pixels per second
    // easing: easing equation to use

    var scrollY = window.scrollY || document.documentElement.scrollTop,
        scrollTargetY = scrollTargetY || 0,
        speed = speed || 2000,
        easing = easing || 'easeOutSine',
        currentTime = 0;

    // min time .1, max time .8 seconds
    var time = Math.max(.1, Math.min(Math.abs(scrollY - scrollTargetY) / speed, .8));

    // easing equations from https://github.com/danro/easing-js/blob/master/easing.js
    var easingEquations = {
            easeOutSine: function (pos) {
                return Math.sin(pos * (Math.PI / 2));
            },
            easeInOutSine: function (pos) {
                return (-0.5 * (Math.cos(Math.PI * pos) - 1));
            },
            easeInOutQuint: function (pos) {
                if ((pos /= 0.5) < 1) {
                    return 0.5 * Math.pow(pos, 5);
                }
                return 0.5 * (Math.pow((pos - 2), 5) + 2);
            }
        };

    // add animation loop
    function tick() {
        currentTime += 1 / 60;

        var p = currentTime / time;
        var t = easingEquations[easing](p);

        if (p < 1) {
            requestAnimFrame(tick);

            window.scrollTo(0, scrollY + ((scrollTargetY - scrollY) * t));
        } else {
            window.scrollTo(0, scrollTargetY);
        }
    }

    // call it once to get started
    tick();
}

function scrollFunction() {
    if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
        document.getElementById("myBtn").style.display = "block";
    } else {
        document.getElementById("myBtn").style.display = "none";
    }
}
p{
display: block; height: 2500px;}

#myBtn {
  display: none;
  position: fixed;
  bottom: 20px;
  right: 30px;
  z-index: 9999;
  border: none;
  outline: none;
  background-color: red;
  color: white;
  cursor: pointer;
  padding: 15px;
  border-radius: 10px;
}

#myBtn:hover {
  background-color: #555;
}
<p>Role para baixo</p>
<button onclick="scrollToY(0, 10000, 'easeInOutSine');" id="myBtn" title="Ir ao topo">Topo</button>

  • thanks for the answer. I tried to do the same thing. I got to that part until I found your solution to the problem. /* const sectionCourses = document.querySelector(".section3");&#xA;const goTop = document.getElementById("gototop");&#xA;&#xA;&#xA;if (window.scrollY >= sectionCourses.offsetTop) {&#xA; goTop.style.display = "block";&#xA;} else {&#xA; goTop.style.display = "none";&#xA;} */ My biggest problem was not having used a Function and using it here: window.onscroll = function(){&#xA; scrollFunction()&#xA; }; Could you explain this part to me?

  • window.onscroll detect when window suffers scroll (scrolling) and triggers a function. You could write so, more simplified: window.onscroll = scrollFunction;. Like scrollFunction is already a function, you don’t need to put it inside another function.

0

#voltarAoTopo {
  float:right;
  z-index:0;
  position:fixed;
  bottom:20px;
  right:15px;
}

And in that detail window, put z-index:9;

You tried so?

0

Hello,

Set the z-index: considering 9 as above all, and 0 below all.

So,

z-index:0

or

  z-index:1
  • 1

    that’s in css ?

  • Yes, in the floating button css.

  • neither with 0 nor with 1 was not a friend.

0

Use position: relative in the parent element of everything. In the table have a div that has overflow enabled. And out of this div put a button with position: Absolute.

This thumb drive can help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.