Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying CSS transform with jQuery using a variable

I can get the below code working when I don't use the variable. When I add the variable it breaks though.

$('.right, .auto_slide').click(function() {
    var slider_margin = parseInt($(this).closest('.slider').css('transform',"translateX"));
    var new_margin = slider_margin - pane_width;
    $(this).closest('.slider').css('transform',"translateX("+new_margin"+)");
});

HTML

<section class="stage" id="basic">
    <div class="slider" style="transform: translateX(0px);">

        <!-- Content -->

    </div>
</section>
like image 659
colmtuite Avatar asked Jan 23 '26 13:01

colmtuite


2 Answers

You need to add the variable like this

$('.thing').click(function() {
    var whatever = 20 - 100;
    $('.slider').css('transform',"translateX("+whatever+"px)");
    //                                       ^^like this^^ 
});

Also you cant subtract string like that, do 20-100

like image 190
Anton Avatar answered Jan 26 '26 02:01

Anton


You need to concatenation

$('.thing').click(function() {
    var whatever = 20 - 100;
    whatever += "px";
               //^ add px
    $('.slider').css('transform',"translateX("+whatever"+)");
                                            //^         ^  
});

and you can not subtract string you need numerbs

like image 41
Tushar Gupta - curioustushar Avatar answered Jan 26 '26 02:01

Tushar Gupta - curioustushar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!