Jump to content
xisto Community
Sign in to follow this  
t3jem

Javascript Scroll Bar A scroll bar for your webpage using javascript

Recommended Posts

In this tutorial I will show you how to create two buttons in the bottom left of the screen that, when hovered over, will scroll the page.

Now to start with, we must create a our buttons, the first line will create a div element, or block. Using blocks you can position items anywhere on a page. We use the ID property just to let us know what the block is used for, as for the first block, its obvious that it contains the vertical buttons and the second two blocks contains the horizontal buttons.
The style property of the div tag tells the browser how to draw it, in the first block we set it to fixed, which keeps the block in one place on the window, then we follow it with coordinates, 10 pixels from the right and 10 pixels from the bottom.
Same goes with our seconds block except it is 60 pixels from the right.
Now we use anchor tags so we can use the mouseover feature, so when the mouse moves over each image the image can change our speed variable that scrolls our screen. We also use the mouseout property to stop the screen from scrolling when we leave the image.

Below is our first few lines of code.

<div id="verticalbuttons" style="position: fixed; right:10px; bottom:10px;">  <a onmouseover="ycurrentscrollspeed=-staticscrollspeed" onmouseout="ycurrentscrollspeed=0"><img  src="up_arrow.gif" border="0"></a>    <a onmouseover="ycurrentscrollspeed=staticscrollspeed" onmouseout="ycurrentscrollspeed=0"><img  src="down_arrow.gif" border="0"></a>  </div>

Note: you can roposition the buttons by changing cooridinates. Below I will split the block into two instead of one for both buttons. The left button will be all the way on the left site of the screen on the bottom, while the right will be at the right side of the screen on the bottom. If you wanted to put the buttons at the top instead you could change "bottom:10px" to "top:10px" so it will be 10 pixels from the top or however many pixels you want it from the top/bottom or where ever.

<div id="leftbutton" style="position: fixed; right:60px; bottom:10px;">  <a onmouseover="xcurrentscrollspeed=-staticscrollspeed" onmouseout="xcurrentscrollspeed=0"><img  src="left_arrow.gif" border="0"></a></div><div id="rightbutton" style="position: fixed; left: 10px; bottom:10px">  <a onmouseover="xcurrentscrollspeed=staticscrollspeed" onmouseout="xcurrentscrollspeed=0"><img  src="right_arrow.gif" border="0"></a>  </div>

Now we start our script and initialize our variables. The first variable is static and doesn't change, we use this to define how fast we want our page to scroll, you may change the speed if desired.
The other two variables are used to keep track of the current speed of scrolling on the x and y axiis, (x axis is left-right, y axis is up-down). We set these to zero because we don't want to be scrolling when the user loads the page.
<script>    var staticscrollspeed=3 //Enter scroll speed in integer (Advised: 1-3)    var xcurrentscrollspeed=0  var ycurrentscrollspeed=0

our next function will do the actual scrolling. We scroll using the function "scrollBy". The first argument is the speed on the x axis to scroll and the second is the speed on the y axis. We, of course, fill these arguments with our speed variables for each axis.

Then last, but not least, we set an interval to call our scroll function every 20 milliseconds.

function scroll(){	window.scrollBy(xcurrentscrollspeed,ycurrentscrollspeed)  }    setInterval("scroll()",20)    </script>

I hope you enjoyed the tutorial and found it helpful!

if you have any questions/comments, feel free to private message me.
Edited by t3jem (see edit history)

Share this post


Link to post
Share on other sites

nice on you helped me you shall get youre creds soon.Question: How can i make the position of the pics move? For example i want the up button to go on top and down on bottom and same with sideways.

Edited by zak92 (see edit history)

Share this post


Link to post
Share on other sites

Question: How can i make the position of the pics move? For example i want the up button to go on top and down on bottom and same with sideways.

Alright, I just edited the top part of the code. If you want to put the up button at the top of the screen you change the style from "position: fixed; right: 10px; bottom: 10px;" to "position: fixed; right: 10px; top: 10px;" so instead of being 10 pixels from the bottom it will be 10 pixels from the top. Also you can change the pixels to percents/other units and you can also change how much space there is from the edges of the screen, just remember "bottom" is space from the bottom, top is space from the top, left and right are space from the left and right respectively.

Glad you enjoyed my tutorial :lol:

Share this post


Link to post
Share on other sites

ok, if you wanted to split the up and down arrows just do it just like the left/right arrows, here's the code for it.

<div id="topbutton" style="position: fixed; right:10px; top:10px;">  <a onmouseover="ycurrentscrollspeed=-staticscrollspeed" onmouseout="ycurrentscrollspeed=0"><img  src="up_arrow.gif" border="0"></a></div><div id="bottombutton" style="position: fixed; right: 10px; bottom:10px">  <a onmouseover="ycurrentscrollspeed=staticscrollspeed" onmouseout="ycurrentscrollspeed=0"><img  src="down_arrow.gif" border="0"></a>  </div>

just splite the block, and change the position.

Share this post


Link to post
Share on other sites

Hey maybe since its time you should post like more advanced version maybe including a bar to keep track of where you are . Great job though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.