Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal is "remembering" code from previous modals

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! :)

like image 903
chris Avatar asked Jul 21 '15 23:07

chris


People also ask

How do I stop modal popups automatically?

Inside the Open event handler of the jQuery UI Dialog Modal Popup box, using the JavaScript setTimeout function and the jQuery UI Dialog “close” command, the jQuery UI Dialog Modal Popup box is set to automatically close after delay of 5 seconds (some seconds). This dialog will automatically close in 5 seconds.

What are bootstrap Modals?

Bootstrap Modals offer a lightweight, multi-purpose JavaScript popup that's customizable and responsive. They can be used to display alert popups, videos, and images in a website.

How do you activate a modal?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

What is modal window in JavaScript?

A modal is a dialog box/popup window that is displayed on top of the current page: Open Modal. ×


1 Answers

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

like image 186
sydeslyde Avatar answered Sep 24 '22 12:09

sydeslyde