3

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>

2 Answers 2

3

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

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

16 Comments

I can't get it working. I edited my question with my exact code.
@Moppy You have a typo "+new_margin"+)"the + sign must be outside the quotes like this "+new_margin +")"
Like this? (+"new_margin"+)
Ah, I added the "px" and it worked. Still bugs but I'll figure it out. Thanks
Thanks mate, I appreciate all the help :)
|
1

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

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.