Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
Get sprite sheet background position?

I have a table where each cell background is populated by sprites from a sprite sheet when the user clicks the cell which applies a class.

#design_container td {
  border: solid 1px #333;
  border-width: 0 1px 1px 0;
  width: 15px;
  height: 15px;
  background-image: url("../img/material_sprite.png");
  background-repeat: no-repeat;
}
.material_58 {
  background-position: -192px -48px;
}

Now I am using html5 canvas to make a download image. How can I get the background-position of the cells? I tried(and failed)

let x = document.querySelector('.material_58').style.backgroundPosition;
console.log(x); // returned nothing/blank

And also to no avail:

let tmp = window.getComputedStyle(x,null).backgroundPosition.trim().split(/\s+/);

I assume they may be getting the background position of the actual element, not the position of the sprite sheet. I'm trying to get

background-position: -192px -48px;

If something like that doesn't work maybe something like get the actual class description and parse it? Thank you for your help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue with your approach is that you're referencing a nonexistent style attribute on the element. What you want is the computed style:

const element = document.querySelector('.material_58');
const bgpos = getComputedStyle(element).backgroundPosition;

console.log(bgpos);
.material_58 {
  background-position: -192px -48px;
}
<div class="material_58"></div>

It would work the way you have it if the background position were inline:

console.log(
  document.querySelector('.inline-style')
    .style
    .backgroundPosition
);
<div class="inline-style" style="background-position: -192px -48px"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda