1

does anybody know a command to bind "scroll to top"/ "jump to top" function to a button?

I found something more fancy, but would like to have a more simple version: https://chrome.google.com/extensions/detail/chiikmhgllekggjhdfjhajkfdkcngplp

Much appreciated.

baik
  • 993
  • 2
  • 14
  • 21

4 Answers4

2

Well, you can just do the following, scroll to X=0, and Y=0, and thats it https://developer.mozilla.org/en/window.scroll

window.scroll(0, 0);

If you want to hook that up to Google Chrome Extensions, all you need to do is create a JavaScript file and inject it to your page:

background.html

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(tab.id, {code: 'window.scroll(0, 0);'});
});

manifest.json

Make sure you have the tabs permission and browser action:

  ...
  ...
 "browser_action": {
   "default_icon": "some_icon.png",
 },
 "permissions": [
   "tabs",
   "http://*/*"
 ],
  ...
  ...

I havn't tested that, but it will give you an idea how to send a command from Chrome to Website through extensions via browser action button. Once you click on that button, it will execute a script to scroll that page to the top.absolute top.

Mohamed Mansour
  • 39,445
  • 10
  • 116
  • 90
1

Home
(Simple, no over-thinking.)

Aaron Thoma
  • 3,820
  • 1
  • 37
  • 34
delfin
  • 11
  • 1
0

To create an element with id "top" at the beginning of body, and assign the href of the "go to top" button to "#top", or add an event listener to it.

If you want it to show on the page persistently, use CSS rule position: fixed and appropriate other attributes.

Ryan Li
  • 9,020
  • 7
  • 33
  • 62
-1

Chrome extensions are html/js, right ? So just set the scrollTop attribute of <body> to 0. Maybe there'is a more elegant way but this work :

<script>
function setScroll()
{
    document.getElementsByTagName("body")[0].scrollTop = 0;
}
</script>

<input type="button" value="Set scrollTop to 50" onclick="setScroll();" />
Thomas Joulin
  • 6,590
  • 9
  • 53
  • 88