0

Ok so I have one div and inside it a canvas. from my html file:

<div id="container">
    <canvas id="game" width="600px" height="2000px"></canvas>
</div>

from css file:

#game{
    /*background-color: #99CC00;*/
    background:-moz-linear-gradient(top, #00b7ea 0%, #008793 14%, #004b9b 43%, #1f00ea 74%, #008793 89%, #009ec3 100%);
    bottom: 0px;
    position: absolute;
}
#container{
    position: relative;
    width: 600px;
    height: 500px;
}

What i want to do is to control the 'bottom' value of #game that is:

($("#game").css("bottom")),

To put it in an if statement and if true, to change it automatically.

But when I try there is a problem that I cannot understand. I have a variable called s. Where s will be the amount that should be changed, it's not a fixed number.

when I tried ($("#game").css("bottom", -s)); nothing happened, so to check it I did the following:

alert( ($("#game").css("bottom", -s)) ); the reply was [object Object].

while if I just do alert( ($("#game").css("bottom")) ); the reply is correctly 0px.

What am I missing? Help would be much appreciated!

2
  • Have you tried parseInt() on s to ensure that it's numeric? Commented Aug 27, 2014 at 13:15
  • try adding px - $("#game").css("bottom", -s + 'px') Commented Aug 27, 2014 at 13:21

2 Answers 2

1

The .css(name, value) will set the css property of the name equal to the value. It will not alter the value like doing x += 1 would change a variable.

Therefore you should be doing something like:

var oldValue = parseInt($("#game").css("bottom"));
$("#game").css("bottom", (oldValue - s) + "px")

The parseInt is necessary, because as you said it returns 0px, which is a string.

Sign up to request clarification or add additional context in comments.

1 Comment

It still needs a unit, unless the value is actually 0.
0

You can change the value and set as:

$( "div" ).on( "click", function() {
    $( this ).css({
    height: function( index, value ) {
        return value * 1.2;
    }
    });
});

The 1.2 is same as r in your code, when you click on div , the 'div' height is increased automatically . It may help you out.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.