views:

37

answers:

5

I got the following code to work with .animate, but it won't with standard css properties, could anyone tell me why?

var xMove = 200;
$("#clickhere").click(function() { 
$("#object").css({left: '+='+xMove});  
});

Got a few similar working approaches now and learnt a bit about syntax along the way, good show people, Ta!

A: 

Try

$(document).ready(function(){
  var xMove = 200;
  var object = $('#object');
    $("#clickhere").click(function() {
        object.animate({
            left:  (object.position().left + xMove) + 'px'
        });  
    });
});

Remember that #object needs to have position absolute or relative when working with the css left property. If its static you can try to animate margin-left instead.

Fred Bergman
cheers, that works, was wondering if it was possible without using animate (explained below).
jack
just change object.animate({ to object.css({
Fred Bergman
yeah worked that out when i saw the other responses. respect!
jack
A: 

Add the CSS attribute position, and set it to relative; it'll work. I've also removed the +=, since it doesn't belong there:

var xMove = 200;
$("#clickhere").click(function() { 
    $("#object").css({position: "relative",left: xMove});  
});

If you want to move your object the number of pixels defined by xMove to the right every time you click it, you can use the following code snippet:

var xMove = 200;
$("#clickhere").click(function() {
    var obj = $("#object");
    var currentPosition = obj.position().left;
    obj.css({position: "relative",left: currentPosition + xMove});  
});
Giu
cheers, looks like guffas and squeegee's codelike the structure of this one though, easy for me to follow
jack
A: 

Try this, should work,

var leftpos = $("#object").css("left"); // get the current left position
var left = parseFloat(leftpos , 10);
left += 200;
var toShift = left + "px";
$("#object").css("left", toShift );
anand
didn't work for me but cheers for the effort
jack
A: 

You can't use the += operator in regular CSS. Get the current position and calculate the new positon from that:

var xMove = 200;
$("#clickhere").click(function() {
  $("#object").css({ left: ($('#object').position().left + xMove) + 'px' });
});
Guffa
niceone, works a treat ;)should have thought of that really, still learning the syntax rules though
jack
A: 

2 things. First of all, left does nothing in CSS unless you set a different position type. So make sure #object has position:relative (or absolute, or fixed).

Second, you can't use += in the value string like that. You would have to fetch the value add your value, then set it to do this.

Checkout this working example: http://jsfiddle.net/d9LnJ/4/

But really, why not just use .animate?

Squeegy
thanks that makes sense does exactly what i'm looking for.i wanted to avoid animate as i'm planning to add 'velocity' to #object by dynamically varying var xMove and looping over the function. using .animate works but wasn't very smooth when applied like that.
jack