Welcome Guest ( Log In | Register)



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Javascript Scroll Bar, A scroll bar for your webpage using javascript
t3jem
post Apr 7 2007, 06:48 AM
Post #1


Privileged Member
*********

Group: [HOSTED]
Posts: 521
Joined: 9-February 07
Member No.: 38,519



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.

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.

CODE
<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.
CODE
<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.

CODE
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.

This post has been edited by t3jem: Apr 7 2007, 04:58 PM
Go to the top of the page
 
+Quote Post
zak92
post Apr 7 2007, 03:13 PM
Post #2


Super Member
*********

Group: Members
Posts: 225
Joined: 5-January 07
Member No.: 36,560



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.

This post has been edited by zak92: Apr 7 2007, 03:17 PM
Go to the top of the page
 
+Quote Post
t3jem
post Apr 7 2007, 05:02 PM
Post #3


Privileged Member
*********

Group: [HOSTED]
Posts: 521
Joined: 9-February 07
Member No.: 38,519



QUOTE(zak92 @ Apr 7 2007, 08:13 AM) *
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 biggrin.gif
Go to the top of the page
 
+Quote Post
Unholy Prayer
post Apr 7 2007, 05:19 PM
Post #4


Newbie [Level 2]
**

Group: Members
Posts: 36
Joined: 27-December 05
Member No.: 16,252



That's a pretty good code, man. Nice work. I might use it.
Go to the top of the page
 
+Quote Post
zak92
post Apr 7 2007, 07:11 PM
Post #5


Super Member
*********

Group: Members
Posts: 225
Joined: 5-January 07
Member No.: 36,560



Not exactly. I wanted to have the up arrow on the top and the bottom arrow completely out at the bottom. Help. Thanks
Go to the top of the page
 
+Quote Post
t3jem
post Apr 8 2007, 04:29 AM
Post #6


Privileged Member
*********

Group: [HOSTED]
Posts: 521
Joined: 9-February 07
Member No.: 38,519



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.

CODE
<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.
Go to the top of the page
 
+Quote Post
zak92
post Apr 8 2007, 06:55 AM
Post #7


Super Member
*********

Group: Members
Posts: 225
Joined: 5-January 07
Member No.: 36,560



you know what man i reallly love you. Thanks a lot. I hope you got your creds by now!
Go to the top of the page
 
+Quote Post
anish
post Apr 20 2007, 12:50 PM
Post #8


Newbie
*

Group: Members
Posts: 9
Joined: 3-December 05
Member No.: 15,278



Thanks for sharing
Go to the top of the page
 
+Quote Post
zak92
post Jun 2 2007, 08:47 AM
Post #9


Super Member
*********

Group: Members
Posts: 225
Joined: 5-January 07
Member No.: 36,560



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.