/    Sign up×
Community /Pin to ProfileBookmark

Javascript Scrolling text control

Hi, I am new to the forum, I’m also not very efficient with Javascript, guess thats why I’m here ? , and I wouldn’t ask for help unless I’m really stuck on something. Basically, I need to scroll the text using javascript because the default scroll bar is a non-solution (ie: looks ugly and doesn’t match the layout of the webpage, a must go).

Basic function:

1)onmouseover ‘up arrow’ the text scrolls upwards
2)onmouseover ‘down arrow’ the text scrolls downwards
3)onmouseout ‘up or down arrow’ the text stops scrolling

The text <div> is contained within another <div> which acts as the container and which the scrolling of the text takes place in.

I’m stuck on the 3) part since I don’t know how to implement that in my code

The scrolling works fine overall, but still these minor issues, its inefficient basically. Any help is greatly appreciated, thank you in advance.

HTML FILE:

[code]

<html>

<head>

<link rel=”stylesheet” type=”text/css” href=”style.css”>

<script language=”JavaScript” type=”text/javascript” src=”javascript_new.js”>
</script>

</head>

<body bgcolor=”#333333″>

<div id=”up_arrow” onmouseover=”scroll_up();”> ARROW UP </div>

<div id= “textarea”>
<div id=”thetext” >

<h2>Header 1</h2>
<p>
Here is text
</p>
<br />
<br />

<p>
More text<br />
</p>
<br />

<p>
Much more text…
</p>
</div></div>

<div id=”down_arrow” onmouseover=”scroll_down();” > ARROW DOWN </div>

</body>
</html>
[/code]

CSS FILE:

[code]
#textarea { position: relative; width: 300; overflow: hidden; left: 250; height: 200; background-color:#333333; }
#thetext { position: absolute; width: 280; left: 5; top: 100; }
#up_arrow { width: 20px; height: 20px; }
#down_arrow { width: 20px; height: 20px; }
[/code]

JAVASCRIPT FILE:

[code]
var position = 100;

function scroll_up()
{

if(!document.getElementById) return;

text_object = document.getElementById(“thetext”);//get the text object

position-=1;//decrement and scroll up

if(position < 0-text_object.offsetHeight+150)
{
window.clearTimeout(“scroll_up();”, 300);

text_object.style.top = position;
}

else
{

text_object.style.top = position;

window.setTimeout(“scroll_up();”, 30);
}

}

function scroll_down()
{

if(!document.getElementById) return;

text_object = document.getElementById(“thetext”);

position+=1;//increment and scroll down

if(position > 0+((text_object.offsetHeight) – (text_object.offsetHeight – 150)))
{
window.clearTimeout(“scroll_down();”, 300);
text_object.style.top = position;
}

else
{

text_object.style.top = position;

window.setTimeout(“scroll_down();”, 30);
}

}

[/code]

Anyways, any thoughts or input is again greatly appreciated. Thanks again!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisJan 18.2008 — If you were to change your logic to use setInterval() instead of setTimeout(), then onmouseout you would use clearInterval() to stop the effect.
<i>
</i>&lt;script type="text/javascript"&gt;
var timer;
var position = 100;

function scroll_up() {
position -= 1;
text_object = document.getElementById("thetext");
if (position &lt; 0 - text_object.offsetHeight + 150)
{clearInterval(timer); // stop moving
text_object.style.top = position;}
else
{text_object.style.top = position;}
}

function scroll_down() {
position += 1;
text_object = document.getElementById("thetext");
if (position &gt; 0 + (text_object.offsetHeight - (text_object.offsetHeight - 150)))
{clearInterval(timer); // stop moving
text_object.style.top = position;}
else
{text_object.style.top = position;}
}

function doUp() {
timer = setInterval("scroll_up()", 30);
}

function doDn() {
timer = setInterval("scroll_down()", 30);
}
&lt;/script&gt;
...
&lt;div id="up_arrow" onmouseover="doUp()" onmouseout="clearInterval(timer)"&gt; ARROW UP &lt;/div&gt;
...
&lt;div id="down_arrow" onmouseover="doDn()" onmouseout="clearInterval(timer)"&gt; ARROW DOWN &lt;/div&gt;
Copy linkTweet thisAlerts:
@carnageuserauthorJan 18.2008 — gil davis, thanks for the reply you solved my problem

I still have much to learn ?
Copy linkTweet thisAlerts:
@giorgioneFeb 20.2008 — Despite I'm very very new to scripts and java I've succeed to use this scripts in a web page I'm building. (http://www.g10design.com/KZM/A-main-Es.html)

Only problem is that it does not work in Netscape.

Have I made some big mistakes?

gr. G.
×

Success!

Help @carnageuser spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...