LightGallery is counting wrong number of images.. here is jsfiddle code... In this jsfiddle i am append dynamical element with a div that is in html file.. Please check manually ... Why lightgallery is count wrong images ?

Dani AI

Generated

A quick diagnosis and practical fixes that add to the replies from and :

Off-by-one loop mistakes are an easy first check (as pointed out), but LightGallery’s reported “extra” slide often comes from the gallery being initialized with a different set of DOM nodes than expected — e.g., the plugin was initialized twice, a hidden/placeholder anchor is present, or new items were appended after init and the gallery was not refreshed. LightGallery builds its internal list from the DOM at init (or from dynamicEl when using dynamic mode). (See the dynamic-mode docs.)
(https://www.lightgalleryjs.com/demos/dynamic-mode/)

Concrete checks to run before changing business logic:

  • Log the array length and the number of anchors actually in the gallery container (use console.log(imagesArray.length) and document.querySelectorAll('#yourGallery a').length) to confirm a mismatch.
  • After init, inspect the plugin’s internal list (plugin.galleryItems.length) and look for duplicate anchors or multiple lg-uid entries on the page (which indicate multiple initializations). These checks point to whether the problem is your loop or the plugin state. (See methods / update examples.)
    (https://www.lightgalleryjs.com/docs/methods/)

Fixes that have worked reliably:

Final notes: ensure the LightGallery version in use (1.x vs 2.x) — APIs evolved and 1.x is no longer actively maintained, so behavior and recommended methods differ by version. When debugging, prefer a minimal reproducible snippet showing only the gallery markup and init code so the source of duplication is obvious.
(https://sachinchoolur.github.io/lightgallery.js/docs/plugin-api.html)

Recommended Answers

All 8 Replies

for(var i =0;i<=imagesarray.length-1;i++){

Changed line 12?

why i should do that ?

If you have 5 items and then loop 0 to 5. That's one too many.

Your choice of -1 or change i<= to i< whichever you can wrap your head around. The right way would be to drop the = sign but the key item is for you to think of the array count is one number and arrays start at zero so think about that.

That's the same code as above. Sorry but you need to think about your for loop harder.

An array of 5 items is indexed from 0 to 4. You have it from 0 to 5 so that's not 5 but 6. Keep thinking, you'll get it soon.

So is this a new issue? Your topic is "i have 5 imges and lightgallery shows 6 images." If you have a new issue, state it in a new thread so we can tackle that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.