Click to See Complete Forum and Search --> : Problem with IF statements


smercer
05-21-2004, 07:56 PM
Hi all

I am having a problem with an if statement. It is for an auto scroll depending on where the mouse pointer is on the screen. The problem is that it is not doing anything and not giving any error message either.

the variables winH stands for Window Height, winW stands for Window width.




// the following is for vertical scroll
var autoScrollOk = true; // when the push button "autoscroll on" is pressed autoScrollOk
// will be changed to true ( I have not put this button in yet)
//If autoScrollOk = true then it will autoscroll
//if ( autoScrollOk = true ) {
if ( window.event.y < winH / 3 ) { // if the mouse is on the top 1/3 of screen
window.scrollBy(0,-4); // the window will scroll up
}
else ( window.event.y > ( winH / 3 ) * 2 ) { // if the mouse is on the lower 3/3 of screen
window.scrollBy(0,4); // the window will scroll down
}

//end vertical scroll


//Start horizontal scroll


if ( window.event.x < winW / 3 ) { // if the mouse is on the left 1/3 of screen
window.scrollBy(-4,0); // the window will scroll left
}
else ( window.event.x > ( winW / 3 ) * 2 ) { // if the mouse is on the Right 3/3 of screen
window.scrollBy(4,0); // the window will scroll Right
}
//End horizontal scroll
//End If autoScrollOk = true then it will autoscroll
}//End function



I have just tried:

if ( window.event.y < ( winH / 3 ) )

but that does not work either.

Please keep technical terms to a minimum because I am a beginner at JavaScript.


I would be grateful for any help offered. Thanks

Kor
05-22-2004, 02:40 AM
else if( .....

Kor
05-22-2004, 02:52 AM
correct syntax

if(condition){
..statement;
}
else if(condition){
..statement;
}

in "tertium non datur" cases (no third possibility)

if(condition){
..statment;
}
else{
..statement:
}

smercer
05-22-2004, 03:19 AM
If I have "else if" because I am using different varables (scrollUp and scrollDown) that the if statement is checking, would it conflict?

I did some playing around with the if statement and now looks like this:

***I will keep updating this section***
/* If autoScrollOk = true then it will autoscroll*/
if ( autoScrollOk == true && ( mouseCoorY < scrollUp ) ) {
do { window.scrollBy(0,-4);
} while ( mouseCoorY < scrollUp ); //Line 52
}
}

/* if the mouse is on the top 1/3 of screen*/
/* the window will scroll up */

if ( autoScrollOk == true && ( mouseCoorY > scrollDown ) ) {
do { window.scrollBy(0,4);
} while ( mouseCoorY > scrollDown ); //Line 60
}
}


and now, after using an alert line, I find that the IF statement is not to blame but this:



window.scrollBy(0,4);



Works ok by itself but not in the if statement. It is now giving errors like these:

Stack overflow error at line: 60 (for when I move the mouse down)

Stack overflow error at line: 52(for when I move the mouse up)

Plus there are more if statements that are almost identical to said if statements that are for horizontal scroll which also have the simalar error.


I have put in a comment telling you which line is 52 and 60.
***end part I will keep updating***



Do you have a solution Kor?

Thanks Kor for helping

Kor
05-22-2004, 05:18 AM
can you post the whole code?

smercer
05-22-2004, 09:18 AM
I certainly can. Please find the attached zip file with the included .js and .HTML files.

Thanks again Kor for helping.

smercer
05-23-2004, 03:02 AM
does anyone have any ideas?

Kor
05-24-2004, 02:02 AM
1. scrollBy() is a method, thus is amongst rezerved words. You can not use it to name your function...

2. 1I think the mouse position is not capture in a proper way

As far as I know you must use window.event.clienX and window.event.clientY for mouse position (at least for IE).

3. using while instead of if statement will prolongue the code time running

If time, I'll try to build a new script for you.

Kor
05-24-2004, 05:08 AM
Ok. Try this (at least it might be a better start for your needs):


<html>
<head>
<script>
function setUp() {
if( typeof( window.innerWidth ) == 'number' ) {
/* Non-IE */
winW = window.innerWidth;
winH = window.innerHeight;
} else if( document.documentElement &&
( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
/* IE 6+ in 'standards compliant mode' */
winW = document.documentElement.clientWidth;
winH = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
/*IE 4 compatible*/
winW = document.body.clientWidth;
winH = document.body.clientHeight;
}
setL = winW/3; //zone left
setR = winW*2/3;//zone right
setU = winH/3;//zone up
setD = winH*2/3;//zone down
pix=4// scroll speed control (pixels/scroll unit)
}
function scrollP(e){
//capture the mouse position
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY)
{
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY)
{
posx = e.clientX;
posy = e.clientY;
}
x=0;
y=0;
//verify mouse position according to zones
scL=document.body.scrollLeft;
if(posx<setL){
x=-pix;
}
if(posx>setR){
x=pix;
}
if(posy<setU){
y=-pix;
}
if(posy>setD){
y=pix;
}
//scroll
window.scrollBy(x,y);
}
</script>
</head>
<body onload="setUp()" onmousemove="scrollP(event)">
<table width="1200" height="900" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle" bgcolor="#CCCCCC">blabla</td>
</tr>
</table>
</body>
</html>

Kor
05-24-2004, 05:11 AM
and...
erase the line
scL=document.body.scrollLeft;

it is usless (I forgot it there while testing something)

smercer
05-24-2004, 10:31 AM
wow, you put a lot of effort into my project!! Although your coding did not do exactly what I wanted (did not go from left to right) the up and down worked and I liked your idea on the layout of the coding. I changed some of my coding simialar to what you did and now works except for one thing: when you hold the mouse over an area it scrolls once but won't until you move the mouse again in that area. here is what I tried:

if ( y || x != 0 )
{ scrollP();
}

works like above with out this code.

upon opening/refreshing it will give a Runtime error saying: out of memory
line 52 (Line 52 is nowhere near the code I posted above.)

I close them and then move the mouse pointer so that it scrolls and when it gets to the end, this will some how close the browser window (IE6)

Kor
05-24-2004, 11:23 AM
(did not go from left to right)


works ok for me... (Ie6, Moz Firefox, Opera 7.5) on Win XP

??

smercer
05-25-2004, 12:37 AM
I tested it in IE6 with win xp and I have my resolution set at
1280 by 1024. Just tested it at 800 by 600 and it was much worse (the threshold for scrolling up was the bottom 10% of screen). although I don't know why because your code checks the screen size.

works great in Sleipnir (excellent Browser!!)

Don't worry, you did a good job anyway (everyone makes mistakes, even profesionals).

anyway I got it fixed up except for the above problem, but I would not have got anywhere if you didn't help. :cool:

Kor
05-25-2004, 01:08 AM
aha... try than replace

posx = e.clientX;
posy = e.clientY;

with:

posx = e.clientX + document.body.scrollLeft;
posy = e.clientY + document.body.scrollTop;

smercer
05-25-2004, 03:47 AM
Sorry, I now realize the last post of mine was not well worded.

I meant that I merged your coding with mine. Here is the coding:


function scrollP () {

var autoScrollOk = true; // If autoScrollOk = true then it will autoscroll
/* when the push button "autoscroll on" is pressed autoScrollOk
will be changed to true ( I have not put this button in yet) */

var setU = winH / 3, setD = ( winH / 3 ) * 2; //vertical scroll
var setL = winW / 3, setR = ( winW / 3 ) * 2; //horizontal scroll
var posy = window.event.y, posx = window.event.x; //Mouse coordinates
pix=2 // scroll speed control (pixels/scroll unit)
x=0;
y=0;


/* the following is for vertical scroll */

/* If autoScrollOk = true then it will autoscroll*/
if ( autoScrollOk == true && ( posy < setU ) ) {
y=-pix;
}

if ( autoScrollOk == true && ( posy > setD ) ) {
y=pix;
}

//end vertical scroll

//Start horizontal scroll


if ( autoScrollOk == true && ( posx < setL ) ) {
x=-pix;
}

if ( autoScrollOk == true && ( posx > setR ) ) {
x=pix;
}
//End horizontal scroll
//scroll
window.scrollBy(x,y);

if ( y || x != 0 ) {
//alert('time to scroll');
scrollP();
}


}
//End scrollP() function




anyway I got it fixed up except for the above problem

What I meant was this code was causing the errors and giving line numbers where there is no problems (no errors when this code is taken out). what also happens is that it closes the browser when you move the mouse into a scroll spot after getting to the end of scrolling.

if ( y || x != 0 ) {
//alert('time to scroll');
scrollP();
}

I want this code to make the window to keep scrolling, whereas before the window will only scroll when you are moving the mouse (for mousemove).

By the way, I hope I did not upset you by not using your code untouched.

Thanks for helping Kor

smercer
05-26-2004, 01:25 AM
Can someone help me with this?


Thanks in advance