Skip to main content
May 3, 2018
Answered

scrollTop doesn't work

  • May 3, 2018
  • 2 replies
  • 2466 views

Best Adobe community,

I've added a button to my webpage that when clicked assigns the value 0 to the element.scrollTop property with no success. I'll post all code below, except the CSS that most importantly specifies the initial value of display:none and styles the button:

HTML:

<button id="top_btn" title="Go to top">

<!-- top_btn -->

<span class="glyphicon glyphicon-chevron-up"></span>

<!-- /top_btn -->

</button>

Javascript:

var body = document.body;
var root = document.documentElement;
var topBtn = document.getElementById("top_btn");

window.onscroll = function() {
"use strict";
scrollFunction();
};

function scrollFunction() {
"use strict";
if (body.scrollTop > 702 || root.scrollTop > 702) {
topBtn.style.display = "block";
}
else {
topBtn.style.display = "none";
}
}

function topFunction() {
"use strict";
body.scrollTop = 0;
root.scrollTop = 0;
}

topBtn.onclick = topFunction();

The button is successfully revealed but nothing happens onclick...

Thank you on beforehand!

Regards,

Andreas

This topic has been closed for replies.

2 replies

WolfShade
Legend
May 3, 2018

Thank you for marking my answer as correct.  I do appreciate it.

According to MDN, only Chrome and Edge support .onclick, with no definite support in FireFox, IE, Safari, or Opera.

V/r,

^ _ ^

May 3, 2018

Is that you between Bill Nye and Neil deGrasse Tyson? ^_^

Might "I 'do' appreciate it" allude to a little bit of irony? He, he, he...

So it wasnt anything wrong with my code, functionally speaking, right?

WolfShade
Legend
May 3, 2018

https://forums.adobe.com/people/Dominus+AB  wrote

So it wasnt anything wrong with my code, functionally speaking, right?

No, it was syntactically correct, but only for Chrome and Edge.  It just may or may not work in other browsers.  The addEventListener is DOM compliant and I believe fully supported in all browsers (IE didn't start until v9; you had to use the proprietary .addEvent() for IE <= 8).

https://forums.adobe.com/people/Dominus+AB  wrote

Is that you between Bill Nye and Neil deGrasse Tyson? ^_^

ME ME ME ME ME ME ME ME ME ME ME!  ME ME ME ME ME ME ME ME?  ME ME ME ME ME ME ME ME ME ME..

^ _ ^

WolfShade
WolfShadeCorrect answer
Legend
May 3, 2018