-2

I am trying to incrementally reposition an element by setting the -webkit-transform property to +=200px

But it doesn't work. I'm missing something, but don't know what.

Clicking the div is supposed to move it further down by 200px everytime.

tim
  • 3,823
  • 5
  • 34
  • 39

3 Answers3

1

CSS doesn't work like that, and jQuery doesn't pull that data out so you can do it like that.

You'd need to read the value, add 200 and then reset it.

var getTranslatedY = function (element) {
    var transform = element.css("transform") || element.css("-webkit-transform") || element.css("-moz-transform");

    if (transform == "none") {
        return 0;
    }

    return parseInt((transform.match(/^matrix\(.*?(-?[\d.]+)\)$/) || [])[1]) || 0;
};

$('#m').on(
    'click',
    function () {
    var element = $(this);
         element
         .css('-webkit-transform',
              "translateY(" + (getTranslatedY(element) + 200) + "px)");
    });

jsFiddle.

alex
  • 479,566
  • 201
  • 878
  • 984
  • well, it works with things like marginLeft, so why wouldn't it also work with translate? – tim Apr 10 '13 at 00:30
  • @tim Because `margin-left`'s value is, for example, `42px`. `transform`'s value is `translate(42px)`. It would need to be a lot smarter in order to parse that. You could probably set it up as a hook in jQuery. – alex Apr 10 '13 at 00:33
1
var step = 200;
$('body').on('click', "#m", function(){
    $(this).css('-webkit-transform','translateY('+ step +'px)');
    step = step+200;
});

And instead -webkit-transform:translateY(100px); in CSS you can set margin-top: 100px;

http://jsfiddle.net/yVSDC/28/

Miljan Puzović
  • 5,840
  • 1
  • 24
  • 30
0

You have to get the Current webkit-transform value and increment it. Alternative Solution is ustin the top CSS Property

You can try this (it's free): http://ricostacruz.com/jquery.transit/

Sample Code:

$('.box').transition({ x: '200px' });
$('.box').transition({ y: '200px' });
$('.box').transition({ x: '200px', y: '200px' });