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

279
Visualizações
How to Split *MULTIPLE* image into tiles? (Javascript)

I want to split multiple images into tiles(it must be div or similar). (With Javascript or jQuery.)

and Images should be one below the other.

Its my div;

<div class="reading-content">                                               
    <div class="page-break no-gaps">
        <img id="image-0" src="image.jpg" class="some-class">
    </div>
    <div class="page-break no-gaps">
        <img id="image-1" src="image1.jpg" class="some-class">
    </div>
</div>

example jsfiddle; http://jsfiddle.net/0Lxr2tad/

var t = Date.now();
var img = new Image();
var length = 30;
var ylength = 20;
img.onload = function() {
    var width = this.width,
        height = this.height,
        _length = -length,
        i, j;

    // create a <div/> with all basic characteristics, to be cloned over and over in the loops below.
    var $basicDiv = jQuery('<div/>', {
        class: 'splitImg',
        css: {
            'width': Math.floor(width/length),
            'height': Math.floor(height/ylength),
            'background-image': 'url(' + img.src + ')'
        }
    });
    // Finding a node in the DOM is slow. Do it once and assign the returned jQuery collection.
    // Also, #wrapper's width can be set here.
    var $wrapper = $('#wrapper').width(width + length * 2); 
    
    for (i = 0; i > _length; i--) {
        for (j = 0; j > _length; j--) {
            $basicDiv.clone().css({'background-position': `${width/length * j}px ${height/ylength * i}px`}).appendTo($wrapper);
        }
    }
    console.log(Date.now() - t);
}
img.src = 'http://www.jqueryscript.net/images/Simplest-Responsive-jQuery-Image-Lightbox-Plugin-simple-lightbox.jpg'; // how to do multiple this??

Anyone can help?

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

0

I don't use jquery much so I can suggest javascript option like this:

const targetElement = document.querySelector(...) // I choose the div that we are going to append newdivs 

I don't know if it exists but I assume there is an array of content you use for src urls or any other data.

let arrayOfInfo = [{},{}] // this is a hypothetical array of objects that we will use  

arrayOfInfo.forEach(obj => {
let newDiv = document.createElement("div");
    newDiv.classList.add("page-break","no-gaps")
    newDiv.innerHTML=`<img id="image-${obj.index}" src=${obj.url} class="some-class">`
   
targetElement.append(newDiv);

})

so with that approach you automate everything. If you need to reuse, you can make it into a function and run it wherever you need. It is also handy if you need to change some content later on, so you only change the array values and rest goes auto again.

I hope this was what you need.

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