question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow gallery to be assign via data-rel attribute

See original GitHub issue

Hi! This is more of a feature request than an issue. It would be nice if we could uniquely assign a group gallery via data-rel attribute. So for example I can have the following code.

<a href="largeimage" class="lightbox" data-rel="gallery123"><img src="smallimage" /></a> <a href="largeimage" class="lightbox" data-rel="gallery123"><img src="smallimage" /></a>

So that anything with that data-rel that are the same will be grouped in the same gallery.

Thanks!

Issue Analytics

  • State:open
  • Created 10 years ago
  • Reactions:3
  • Comments:7

github_iconTop GitHub Comments

1reaction
iamkeircommented, Aug 8, 2015

I went about it slightly differently (and I hope more simply):

var $lightboxTriggers = $('[data-mfp]'); // cache triggers

  $lightboxTriggers.each(function() { // for each instance
    var $trigger = $(this);
    var triggerLightbox = $trigger.data('magnificPopup'); // to check if magnific already bound
    var enableGallery = false; // default to non-gallery mode
    var groupID = $trigger.data('mfp-group') || false; // group ID, if any

    if (groupID) { // if we have a grouping
      $trigger = $lightboxTriggers.filter('[data-mfp-group="' + groupID + '"]'); // set trigger to filtered group triggers
      enableGallery = true; // enable gallery mode
    }

    if (!triggerLightbox) { // if trigger does not already have magnific popup attached

      $trigger.magnificPopup({ // init magnific
        gallery: {
          enabled: enableGallery // flag for gallery
        }
      });

    }

  });

The markup for a trigger is:

<a href="[link to image etc.]" class="mfp-image" data-mfp data-mfp-group="gallery">Trigger</a>

Note, I am using the class helper to indicate ‘type’.

0reactions
p2mediacommented, Jun 8, 2015

We came up with this workaround:

var lightboxElements = {};

$('[data-rel^="lightbox"]').each(function(idx, elem) {
    var $elem = $(elem),
        href = $elem.attr('href'),
        rel = $elem.data('rel'),
        itemSettings = {
            src: href,
            type: 'iframe'
        }
    ;
    lightboxElements[rel] = lightboxElements[rel] || [];

    // @TODO: get additional properties per `.data()`

    if (href.match(/\.(gif|png|jp(e|g|eg)|bmp|ico|webp|jxr|svg)((#|\?).*)?$/i)) {
        itemSettings.type = 'image';
    }


    lightboxElements[rel].push(itemSettings);
});

$.each(lightboxElements, function(rel, items) {

    var $handles = $('[data-rel="'+ rel +'"]');
    $handles.on('click', function(event) {
        event.preventDefault();

        var idx = $handles.index(this) || 0;

        $.magnificPopup.open({
            items: items,
            image: {
                verticalFit: true
            },
            gallery: {
                enabled: true,
                arrows: true
            }
        });


        return false;
    }, idx);

});
Read more comments on GitHub >

github_iconTop Results From Across the Web

jquery - attribute change does not work - Stack Overflow
I'm working on the prettyphoto image gallery. I need to make a gallery with both "all images" category (containing all image categories) and ......
Read more >
Add "data-" attribute to image links - WordPress Stack Exchange
You can control the output of the generated code via the image_send_to_editor filter like this: function filter_image_send_to_editor($html ...
Read more >
How jQuery Mobile Handles HTML5 data - Peachpit
Let's take a look at a few ways that jQuery Mobile handles data- attributes with dialog windows, popups, and buttons. Creating a Basic...
Read more >
Using data attributes - Learn web development | MDN
To get a data attribute through the dataset object, ... Using the CSS selectors and JavaScript access here this allows you to build...
Read more >
Create a filtered image gallery with jQuery and Fancybox
The category selector will allow us to filter the gallery by any of the existing ... Notice that we have set a (HTML5)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found