10

SETUP
I have a grid of boxes (modal triggers). Each box opens a modal. Inside each modal is a very simple thumbnail gallery of one main image and thumbnails. Clicking a thumbnail replaces the main image with larger version of itself.

PROBLEM (4th step in this sequence...)
(1) Open modal A, main image shows - great!
(2) Click the 2nd thumb in modal A and the main image changes to larger version of that thumb - great!
(3) Close modal A - great!
(4) Open modal B, main image is NOT the 1st thumb for modal B, but it's the larger image of the 2nd thumbnail for modal B - NOT so great! Modal B is remembering that I selected the 2nd thumb from modal A so it is showing the Modal B's 2nd thumb instead of its 1st.

QUESTION
How (and where) do I add code so that the modal "forgets" what was chosen in the last modal? Or resets each time I open a modal? This problem is severe if I choose modal A's 3rd image, but open modal B and it doesn't have a 3rd thumbnail - because the main image will be blank. Do I apply code to the modal or gallery jquery? This is driving me bonkers!


MODAL PLUGIN
custombox.js

THUMBNAIL PLUGIN
simplegallery.js

JQUERY TO FIRE MODAL

$(document).ready(function () {
    $(".modal img").not(":visible").each(function () {
        $(this).data("src", this.src);
        this.src = "";
    });
    $(".modal").each(function () {
        $(this).data("sourcesAreSet", false);
    });
    $('.info').on('click', function (e) {
        var correspondingModal = $($(this).data('href'));
        if (correspondingModal.data("sourcesAreSet") == false) {
            correspondingModal.find("img").each(function () {
                this.src = $(this).data("src");
            });
            correspondingModal.data("sourcesAreSet", true);
        }
        Custombox.open({
            target: $(this).data('href'),
            effect: 'push',
            speed: 500,
            overlayColor: '#2C3E50',
            overlayClose: false
        });
        e.preventDefault();
    });
});

JQUERY TO FIRE GALLERY

$(document).ready(function(){
    $('#gallery').simplegallery({
        galltime : 1000, // transition delay
        gallcontent: '.content',
        gallthumbnail: '.thumbnail',
        gallthumb: '.thumb',
    });
});

EXAMPLE MODAL

<div id="modal51" class="modal">
    <div id="portfolioItemClose" class="close"><span></span>
    </div>
    <div class="portfolioTitle wow fadeInLeft" data-wow-delay=".5s" data-wow-duration=".3s">ikuw solutions
    </div>
    <div class="portfolioImageBodyContainer">
        <div class="portfolioImage wow rotateIn" data-wow-delay=".3s" data-wow-duration=".3s">
            <div id="gallery" class="">
                <div class="content">
                    <img src="../../../../../assets/images/portfolio/brochures-flyers/20141117_ikuw_flyer_course-outline_tech.jpg" class="image_1">
                    <img src="../../../../../assets/images/portfolio/brochures-flyers/20141117_ikuw_flyer_course-outline_tech_2.jpg" class="image_2" style="display:none;">
                </div>
            </div>
        </div>
        <div class="portfolioBody wow fadeInDown" data-wow-delay=".5s" data-wow-duration=".3s">
            <div class="portfolioClientDescriptionUsage">
                <div class="portfolioBodyClient wow fadeIn" data-wow-delay=".8s">ikuw solutions</div>
                <div class="portfolioBodyDescription wow fadeIn" data-wow-delay=".9s">technical training course outline</div>
                <div class="portfolioBodyUsage wow fadeIn" data-wow-delay="1s">students</div>
            </div>
            <div class="portfolioBodyText wow fadeIn" data-wow-delay="1.1s">[text]</div>
            <div class="portfolioBodyPDF wow fadeIn" data-wow-delay="1.1s"><a href="../../../../../assets/images/portfolio/brochures-flyers/20141117_ikuw_flyer_course-outline_tech.pdf" target="_blank">View full-scale PDF&nbsp;&nbsp;<span class="fa fa-angle-right"></span></a></div>
            <div class="portfolioBodyLine wow zoomIn" data-wow-delay="1.2s" data-wow-duration=".3s"></div>
            <div class="portfolioBodyVersions wow fadeIn" data-wow-delay="1.3s">pages</div>
            <div class="thumbnail">
                <div class="thumb wow bounceIn" data-wow-delay="1.5s"><a href="#" rel="1"><img src="../../../../../assets/images/portfolio/brochures-flyers/thumb_20141117_ikuw_flyer_course-outline_tech.jpg" id="thumb_1" class="fit"></a></div>
                <div class="thumb wow bounceIn" data-wow-delay="1.6s"><a href="#" rel="2"><img src="../../../../../assets/images/portfolio/brochures-flyers/thumb_20141117_ikuw_flyer_course-outline_tech_2.jpg" id="thumb_2" class="fit"></a></div>
            </div>
        </div>
    </div>
</div>


EDIT
FIDDLE! FIDDLE! FIDDLE! --> http://jsfiddle.net/zuhloobie/nLhcejsz/

(1) click modal A, see modal main image A1 and thumbs A1 and A2 at right
(2) click on thumb A2, see main image change to A2
(3) close modal A, open modal B
(4) see that main image is B2, not B1 (it remembered the 2nd thumb was clicked in modal A so it is showing 2nd thumb of modal B)

Hope that helps! :)

chris
  • 577
  • 1
  • 9
  • 23
  • Bueller....Bueller... – chris Jul 23 '15 at 23:15
  • Isn't it the same question that you already asked before here: http://stackoverflow.com/questions/31305037/zoom-effect-only-zooms-original-image-not-new-image-from-thumbnail ? – Dekel Jul 24 '15 at 10:46
  • No - different. That had to do with zoom. This is a step higher, about emptying a modal or clearing cache or resetting the code before opening it. – chris Jul 24 '15 at 15:41
  • I don't know custombox, but can it has multiple instances? If no, the problem could be that you have multiple elements with class modal. – schellingerht Jul 28 '15 at 13:40
  • Yes, it can have multiple instances. Posted a fiddle: http://jsfiddle.net/zuhloobie/nLhcejsz/ – chris Jul 28 '15 at 18:53

5 Answers5

4

The Problem is you only set new src values but the image-elements themselves (which are hidden/displayed after a click on the thumbnails) are still in the same state as in the modal before.

I got it working by triggering a click on the first thumb when the modal opens:

open: function() {
    correspondingModal.find("#thumb_1").parent("a").trigger("click");
}

Updated Fiddle

sydeslyde
  • 135
  • 7
  • I like this, but is it possible to just reset the entire modal contents on modal open callback? Like empty it and reload it on click? – chris Jul 29 '15 at 15:26
  • Then use the `open` function to `empty()` the modal and fill it again with your markup using `html()`.I don't understand why you would want to reload it. Yes the suggested solution is kind of a hack, but it works. – sydeslyde Jul 30 '15 at 12:56
  • As I mentioned in Jeroen's post, I can only get it to empty - I can't get it to reload. I thought emptying it and reloading it was the "proper" way to do it. It was also suggested by the plugin's author, I just couldn't get it to reload? – chris Jul 30 '15 at 14:50
  • UPDATE: This doesn't work. It fails when you try to open Modal C. If you open Modal B and click on thumb B2...then close Modal B and open Modal C - the main image is blank. Your answer only works if Modals have equal amount of thumbnails - which won't be the case. – chris Jul 30 '15 at 22:57
3

Could be a hackjob. But have a try. Add this piece of code in the click of .info function:

$(".content img").hide(0, function () {
    $(".image_1").show();
});

This effectively "resets" the images:

Fiddle: http://jsfiddle.net/praveenscience/nLhcejsz/2/

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Is it possible to just reset the entire modal contents on modal open callback? Like empty it and reload it on click? – chris Jul 29 '15 at 16:07
  • Your answer is essentially the same answer I already mentioned that I tried in my response to Jeroen, 3 days before you posted this. – chris Jul 30 '15 at 14:48
  • @chris But it was something about making it empty, is it? – Praveen Kumar Purushothaman Jul 30 '15 at 21:30
  • My preference would be to empty the modal contents and reload it. If that doesn't work, then I would have to resort to hiding/showing the images as I already mentioned that I did and have working in Jeroen's post. – chris Jul 30 '15 at 22:52
3

I changed the order of your inital .ready() functions and attempted to save the contents of all modals and reload them when Custombox is closed.

EDIT: The non-loading images has been resolved by loading the content on both the open() and close() functions of Custombox.

Grab initial modal content:

$(".modal").each(function () {
    window['reset' + $(this).attr('id')] = $(this).find('.content').html();
});

Load modal at Custombox open():

open: function(){
            $(".modal").each(function () {
                $(this).find('.content').html(window['reset' + $(this).attr('id')]);
            });
        }

Reload modal at Custombox close():

close: function(){
            $(".modal").each(function () {
                $(this).find('.content').html(window['reset' + $(this).attr('id')]);
            });
        }

UPDATED FIDDLE: http://jsfiddle.net/nLhcejsz/6/

This should now behave as you are requesting, the modal forgets which thumbnail was clicked and resets accordingly.

Glen Despaux Jr
  • 1,064
  • 7
  • 19
1

I've had the same problem with bootstrap modals. I found for myself a simple and working fix:

$('.info').on('click', function (e) {
    $(".modal-body").empty(); 
    // Don't know which element should be empty in your example
    // After the empty(), I load the content I need
}

For me a simple .empty() before I load the content did the job.

Jeroen Bellemans
  • 2,049
  • 2
  • 25
  • 42
  • I would love to empty all the contents of the modal prior to loading it (using the custombox.js open callback)...and I can do that using `.empty()`, but it only empties...it doesn't load the content – chris Jul 24 '15 at 15:28
  • The closest I've gotten was to add this to the close callback: `$('.image_1').hide();` and then `$('.image_1').show();` to the open callback. Feels hacky? I don't really know why it works, but I think it does. If there is a better way, I'm all ears... – chris Jul 24 '15 at 15:39
  • But I'd rather empty the contents of the modal and reload it on modal open than tweaking individual pieces within the modal as it would cover other potential issues. – chris Jul 24 '15 at 15:43
  • 2
    Any chance you could make a jsfiddle? – Jeroen Bellemans Jul 25 '15 at 14:51
  • FIDDLE! http://jsfiddle.net/zuhloobie/nLhcejsz/. Tried to make it somewhat understandable. Follow sequence outlined in question, at top. – chris Jul 28 '15 at 18:49
  • I would love to just reset each modal on open per its open callback (or close callback). This would solve a multitude of issues with wow.js animations and image galleries. – chris Jul 28 '15 at 18:54
  • Did you have a chance to look at that Fiddle? – chris Jul 30 '15 at 14:52
  • Sorry, not yet. I'll try to look this sunday. I'm quite busy & sports atm. I'll certainly take a look! :) – Jeroen Bellemans Jul 30 '15 at 15:58
  • I'm sure we can fix this, by first emptying the modal and then reloading the data. You could eventualy store all data in data-attributes, so we can ask them up later using `$(this)` – Jeroen Bellemans Jul 30 '15 at 15:59
0

I have a simple:

let preventNoMultiple = true;
$('.modal').modal();
$('.Modal').on('shown.bs.modal', function (event) {
  if(preventNoMultiple){// ...do something}
});
$('.Modal').on('hidden.bs.modal', function (event) {preventNoMultiple = false;}