Javascript Course

The JavaScript script presented in this page can be used to add dynamically a button in webpage for Scroll to Top.
The button is added and displayed only when vertical scroll-bar position is more then half of window browser height. If vertical scroll position is less then half of window's height, the button is removed.
The script is composed by two main pieces: the JavaScript code that adds the button, and the CSS properties that styles it.

• To download the script, click -> Dynamically Button for Scrolling to Page Top, or you can use the codes presented bellow.

- To test it, Scroll-down the page; a button for scrolling to top will appear in the bottom-right side of the page.

How to include this script in your webpage

1. Add this code in a .js file, named scrolltop.js :
/** Scroll to Top, from: https://coursesweb.net/javascript/ **/

var scrTop = new Object();
 // function to get scrollbar vertical position
 scrTop.scrollY = function() {
 return window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
 }

 // gets the height of the window
 scrTop.brows_height = function() {
 if (self.innerHeight) {
 var brows_hgh = self.innerHeight;
 } else if (document.documentElement && document.documentElement.clientHeight) {
 var brows_hgh = document.documentElement.clientHeight;
 } else if (document.body) {
 var brows_hgh = document.body.clientHeight;
 }
 return brows_hgh;
 }

 // function for register event on scroll window
 scrTop.onScrollWin = function() {
 // if exists in page the #sttop element
 if(document.getElementById('sttop')) {
 window.onscroll = function() {
 var scrl_pos = scrTop.scrollY(); // get vertical scrollbar position

 // if vertical scroll position is more then half of brows_height, and no element '#scrtop'
 // add button to scroll to Top of the page as child element in #sttop
 // else, if vertical scroll position is less half of brows_height, and element '#scrtop'
 // remove button to scroll to Top of the page
 if(scrl_pos > (scrTop.brows_height() *.5) && !document.getElementById('scrtop')) {
 document.getElementById('sttop').innerHTML = '<div id="scrtop" onclick="window.scrollTo(0,0)"><span>&uArr;</span>TOP</div>';
 }
 else if(scrl_pos < (scrTop.brows_height() *.5) && document.getElementById('scrtop')) {
 document.getElementById('sttop').removeChild(document.getElementById('scrtop'));
 }
 }
 }
 }
scrTop.onScrollWin();
2. Add this code in a .css file, named scrolltop.css :
/* For Scroll top */
#sttop {
 position:relative;
}
#sttop #scrtop {
 position:fixed;
 top:80%;
 right:1.1%;
 width:1.8em;
 cursor:pointer;
 text-align:center;
 font-family:Calibri, Arial, sans-serif;
 line-height:1.4em;
 font-size:.75em;
 padding:0;
 font-weight:800;
 color:blue;
 border-bottom: 1.85em solid #ebeb00;
 border-top: .78em solid #ebeb00;
 border-left: 1em solid transparent;
 border-right: 1em solid transparent;
 height: 0;
}
#sttop #scrtop:hover {
 border-color: #9eef9f;
 color:#000100;
border-radius:1.1em;
}
#sttop #scrtop span {
 display:block;
 margin:-8px auto -1px auto;
 font-size:1.9em;
 color:#fb0000;
}
3. In the <head> zone of the HTML document add this code (to include the .css file in that page):
<link href="scrolltop.css" rel="stylesheet" type="text/css" />
4. At the end of the HTML document, before the ending </body> tag, add this code:
<div id="sttop"></div>
<script src="scrolltop.js"></script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which type of <input> creates input fields that should contain a numeric value?
type="text" type="number" type="date"
<input type="number" name="points" min="5" max="80" />
Which CSS property allows to change the transparency of an element?
font-style opacity color
#id {
  filter:alpha(opacity=40);    /* for IE */
  opacity:0.4;
}
Click on the function that formats a number to specified number of characters.
toPrecision() exp() toFixed()
var num = 12.34567;
alert( num.toPrecision(3) );       // 12.3
Which function randomizes the order of the elements into an array?
natsort() shuffle() sort()
$lang =[10=>"PHP", 20=>"JavaScript", "site"=>"coursesweb.net");
shuffle($lang);
var_export($lang);     // array (0=>"coursesweb.net", 1=>"PHP", 2=>"JavaScript")
Dynamically Button to Scroll to Page Top

Last accessed pages

  1. Paint Bucket and Eyedropper (2218)
  2. Simple Admin Login PHP Script (11031)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142972)
  4. Ajax click Tracking - Monitor clicks on html elements (2238)
  5. For and Foreach Loops (1866)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (205)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (19)
  3. The Mastery of Love (19)
  4. CSS cursor property - Custom Cursors (18)
  5. Read Excel file data in PHP - PhpExcelReader (15)