/    Sign up×
Community /Pin to ProfileBookmark

Problem with IF statements

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.

[COLOR=limegreen]
// 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 ) {[/COLOR]

[COLOR=darkblue]if[/COLOR] ( [COLOR=blue] window.event.y < winH / 3 [/COLOR]) { [COLOR=limegreen]// if the mouse is on the top 1/3 of screen[/COLOR]
[COLOR=firebrick]window.scrollBy(0,-4);[/COLOR] [COLOR=limegreen]// the window will scroll up[/COLOR]
}
[COLOR=darkblue]else[/COLOR] ( [COLOR=blue] window.event.y > [/COLOR]( [COLOR=blue]winH / 3 [/COLOR]) [COLOR=blue]* 2 [/COLOR]) { [COLOR=limegreen]// if the mouse is on the lower 3/3 of screen[/COLOR]
[COLOR=firebrick]window.scrollBy(0,4);[/COLOR] [COLOR=limegreen]// the window will scroll down[/COLOR]
}

[COLOR=limegreen]//end vertical scroll

//Start horizontal scroll[/COLOR]

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

}[COLOR=limegreen]//End function[/COLOR]

I have just tried:

[COLOR=darkblue]if[/COLOR] ( [COLOR=blue]window.event.y < [/COLOR]([COLOR=blue] winH / 3 [/COLOR]) )

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

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 22.2004 — else [color=red]if[/color]( .....
Copy linkTweet thisAlerts:
@KorMay 22.2004 — correct syntax

if(condition){

..statement;

}

else if(condition){

..statement;

}

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

if(condition){

..statment;

}

else{

..statement:

}
Copy linkTweet thisAlerts:
@smercerauthorMay 22.2004 — 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:

[b]***I will keep updating this section***[/B]

[COLOR=limegreen]/* If autoScrollOk = true then it will autoscroll*/[/COLOR]

[COLOR=darkblue]if[/COLOR] ( [COLOR=blue]autoScrollOk == true &&[/COLOR] ( [COLOR=blue]mouseCoorY < scrollUp[/COLOR] ) ) {

[COLOR=darkblue]do[/COLOR] { [COLOR=firebrick]window.scrollBy(0,-4);[/COLOR]

} [COLOR=darkblue]while[/COLOR] ( [COLOR=blue]mouseCoorY < scrollUp[/COLOR] ); [COLOR=limegreen]//Line 52[/COLOR]

}

}

[COLOR=limegreen]/* if the mouse is on the top 1/3 of screen*/
/* the window will scroll up */[/COLOR]


[COLOR=darkblue]if[/COLOR] ( [COLOR=blue]autoScrollOk == true && [/COLOR]( [COLOR=blue]mouseCoorY > scrollDown[/COLOR] ) ) {

[COLOR=darkblue]do[/COLOR] {[COLOR=firebrick] window.scrollBy(0,4);[/COLOR]

} [COLOR=darkblue]while[/COLOR] ( [COLOR=blue]mouseCoorY > scrollDown[/COLOR] ); [COLOR=limegreen]//Line 60[/COLOR]

}

}


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



[COLOR=firebrick]window.scrollBy(0,4);[/COLOR]



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.

[b]***end part I will keep updating***[/B]



Do you have a solution Kor?

Thanks Kor for helping


[COLOR=blue] [/COLOR]
Copy linkTweet thisAlerts:
@KorMay 22.2004 — can you post the whole code?
Copy linkTweet thisAlerts:
@smercerauthorMay 22.2004 — I certainly can. Please find the attached zip file with the included .js and .HTML files.

Thanks again Kor for helping.

[upl-file uuid=2f824e18-dfed-42c8-a786-f59d1598aa91 size=3kB]my script.zip[/upl-file]
Copy linkTweet thisAlerts:
@smercerauthorMay 23.2004 — does anyone have any ideas?
Copy linkTweet thisAlerts:
@KorMay 24.2004 — 
  • 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.
    Copy linkTweet thisAlerts:
    @KorMay 24.2004 — Ok. Try this (at least it might be a better start for your needs):

    [code=php]
    <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>
    [/code]
    Copy linkTweet thisAlerts:
    @KorMay 24.2004 — and...

    erase the line

    scL=document.body.scrollLeft;

    it is usless (I forgot it there while testing something)
    Copy linkTweet thisAlerts:
    @smercerauthorMay 24.2004 — 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)
    Copy linkTweet thisAlerts:
    @KorMay 24.2004 — 
    (did not go from left to right)
    [/quote]


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

    ??
    Copy linkTweet thisAlerts:
    @smercerauthorMay 25.2004 — 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. ?
    Copy linkTweet thisAlerts:
    @KorMay 25.2004 — aha... try than replace

    posx = e.clientX;


    posy = e.clientY;

    with:

    posx = e.clientX + document.body.scrollLeft;

    posy = e.clientY + document.body.scrollTop;
    Copy linkTweet thisAlerts:
    @smercerauthorMay 25.2004 — 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:

    [COLOR=darkblue]

    function[/COLOR]
    [COLOR=blue]scrollP[/COLOR] () {

    [COLOR=darkblue]var[/COLOR] [COLOR=blue]autoScrollOk = true;[/COLOR] [COLOR=limegreen]// 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) *
    /[/COLOR]


    [COLOR=darkblue]var[/COLOR] [COLOR=blue]setU = winH / 3, setD = ( winH / 3 ) * 2;[/COLOR] [COLOR=limegreen]//vertical scroll[/COLOR]

    [COLOR=darkblue]var[/COLOR] [COLOR=blue]setL = winW / 3, setR = ( winW / 3 ) *
    2;[/COLOR] [COLOR=limegreen]//horizontal scroll[/COLOR]

    [COLOR=darkblue]var[/COLOR] [COLOR=blue]posy = window.event.y, posx = window.event.x;[/COLOR] [COLOR=limegreen]//Mouse coordinates[/COLOR]

    [COLOR=blue]pix=2[/COLOR] [COLOR=limegreen]// scroll speed control (pixels/scroll unit) [/COLOR]

    [COLOR=blue]x=0;


    y=0; [/COLOR]



    [COLOR=limegreen]/* the following is for vertical scroll */

    /* If autoScrollOk = true then it will autoscroll*/[/COLOR]

    [COLOR=darkblue]if[/COLOR] ( [COLOR=blue]autoScrollOk == true &&[/COLOR] ( [COLOR=blue]posy < setU [/COLOR] ) ) {

    [COLOR=firebrick]y=-pix;[/COLOR]

    }

    [COLOR=darkblue]if[/COLOR] ( [COLOR=blue]autoScrollOk == true && [/COLOR] ([COLOR=blue] posy > setD[/COLOR] ) ) {

    [COLOR=firebrick]y=pix;[/COLOR]

    }

    [COLOR=limegreen]

    //end vertical scroll



    //Start horizontal scroll[/COLOR]



    [COLOR=darkblue]if[/COLOR] ( [COLOR=blue]autoScrollOk == true &&[/COLOR] ( [COLOR=blue]posx < setL[/COLOR] ) ) {

    [COLOR=firebrick]x=-pix;[/COLOR]

    }

    [COLOR=darkblue]if[/COLOR] ( [COLOR=blue]autoScrollOk == true &&[/COLOR] ([COLOR=blue] posx > setR[/COLOR] ) ) {

    [COLOR=firebrick]x=pix; [/COLOR]

    }

    [COLOR=limegreen]//End horizontal scroll

    //scroll [/COLOR]


    [COLOR=blue]window.scrollBy(x,y);[/COLOR]

    [COLOR=darkblue]if[/COLOR] ( [COLOR=blue] y || x != 0 [/COLOR] ) {

    [COLOR=limegreen]//alert('time to scroll');[/COLOR]

    [COLOR=firebrick]scrollP();[/COLOR]

    }


    }

    [COLOR=limegreen]//End scrollP() function [/COLOR]




    anyway I got it fixed up except for the above problem[/QUOTE]

    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.

    [COLOR=darkblue]if[/COLOR] ( [COLOR=blue] y || x != 0 [/COLOR] ) {

    [COLOR=limegreen]//alert('time to scroll');[/COLOR]

    [COLOR=firebrick]scrollP();[/COLOR]

    }

    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
    Copy linkTweet thisAlerts:
    @smercerauthorMay 26.2004 — Can someone help me with this?


    Thanks in advance
    ×

    Success!

    Help @smercer 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.28,
    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,
    )...