skip to Main Content

I wonder how to concate string value and number variable in a transform property in js/jquery ?

// style.css
.btn {
  scale: 1;
  transform: scale(1);
}


// main.js
  let mousePosY = 2

  $btn.css('transform', 'scale(' + 
  {0.6 + `${mousePosY}` / 4}    //having a typo here, need this line to be a Number(), having issues with backticks maybe? 
  + ')')

2

Answers


  1. Perhaps you meant one of these two template literals/template strings.

    // main.js
    let mousePosY = 2
    
    console.log(`scale(${0.6 + mousePosY / 4})`);
    console.log(`scale(${(0.6 + mousePosY) / 4})`);
    // style.css
    .btn {
      scale: 1;
      transform: scale(1);
    }
    Login or Signup to reply.
  2. Assuming $btn is defined:

    let mousePosY = 2
    let scalePos = 0.6 + mousePosY / 4;
    
    $btn.css('transform', `scale(${scalePos})`);
    .btn {
      scale: 1;
      transform: scale(1);
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search