Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!