0

I'm using a script to move certain background elements on a page. It works by moving the "right" position of an element called #tright. This is similar to a parallax effect. All this works great, except I want the #right element to stop moving when it has a right value greater than 0. I tried to put everything in an if statement. So if the value is greater than 0 do nothing. If it's less than zero keep going. I'm obviously not a programer so I'm not even sure if this is the best way to go about this. Can anyone tell me what I'm doing wrong

$(window).bind('scroll',function(e){
    moveTriangles();
});

function moveTriangles(){
    var scrolled = $(window).scrollTop();
    var rightTriPosition = $( 'canvas#tRight.bTri' ).css( "right" );

if (rightTriPosition < 0){
    $('#tRight').css('right',(-70+(scrolled*.10))+'%');
}
else{
}

}

1 Answer 1

0

css() returns the value with an unit. Now you're comparing something like this ('-100px' < 0). You need to get rid of the unit before comparing the value with a number. For example like this:

var rightTriPosition = parseFloat($( 'canvas#tRight.bTri' ).css( "right" ));

If you don't want to execute anything when the comparison is not passed, you can simply omit the else.

2
  • wow I would have never though of that. works. thanks! The only issue I have is that originally the formula that calculates the "right" value would reverse itself as the user scrolled back up the screen. For example, the object moved to the left as you scroll down, then to the right as you scroll up. That functionality stopped working with this solution. The object is just frozen when it gets to "0" Anyway that I can have the best of both worlds? I played around with the else statement and even adding another if, but had no luck... Dec 22, 2013 at 20:54
  • Please post the whole code involved with this, we'll see what's wrong with it. This answers your original question though...
    – Teemu
    Dec 23, 2013 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.