Code solutions to PHP, JS, CSSCode source PHP, JS, CSS

Spread your code soluces to the world

Love me
Solucior » JAVASCRIPT » Page to top

Page to top

06/08/2011 by Dux   -   4.4/5 (11 ratings)

ToTop() function associated with a link back to the top of page.

Demo.

Click to test toTop

source code



 
<script type="text/javascript">

   function
toTop() {
       if (
window.innerHeight && window.scrollMaxY) {        
           var
yscr = window.pageYOffset;
       } else if (
document.body.scrollTop) {
           var
yscr = document.body.scrollTop;
       } else {
           var
yscr = document.documentElement.scrollTop;
       }
       
       if (
yscr > 0) {
           if (
document.body.scrollTop) document.body.scrollTop = Math.floor(yscr / 1.1) ;
           if (
document.documentElement.scrollTop) document.documentElement.scrollTop = Math.floor(yscr / 1.1) ;
           
setTimeout("toTop()", 10);
       }
   }

</script>

<div style="height:2000px"></div>
<a href="javascript:void(0)" onclick="toTop()">To top</a>

Tested with Safari  Google Chrome  Firefox3  Firefox4  Opera  IE6  
Rate this code     |     Copied : 249x

Comments for Page to top

younes     05/20/2013 23:42
merci beaucoup c'est vraiment sympa...
sami     03/11/2013 17:48
Merciiii ca marche :)
lafumee     02/14/2013 10:50
parfait et simple
Plutonium     01/11/2013 15:22
Excellent !
Merci     01/07/2012 03:21
Super code, ça marche nickel !!! Merci !
Add a comment Nickname
ZOOM


Extract links from a page
Link confirmation
Infinite zoom CSS3 and Javascript
Isolate a color
Dynamic image resizing according to location
Div following the scrolling of the page
Create virtual folders using htaccess
Scroll div with javascript
Adjust the rate of image colors
Dynamically resizing height of textarea