Anay Bose
Anay Bose

Reputation: 890

jquery pop up script needs some tweakings

I am trying to use an js pop-up script: Magnific Popup. The basic settings work well, except that the site header (#header) also shows up and become mixed in the actual pop-up, which is unwanted.

I have written a tiny js function that helps me disappear the header, more specifically the #header id upon clicking on the pop-up link, but the drawback is that the header still remains not visible as I exit out the pop-up.

<script type="text/javascript">
    function hide(div) {
        document.getElementById(div).style.display = 'none';
    }
</script>

<p><a class='youtube' href="http://www.youtube.com/embed/VOJyrQa_WR4?rel=0&amp;wmode=transparent"  onClick="hide('header')">Flash / Video (Iframe/Direct Link To YouTube)</a></p>

Please note onClick="hide('header') does the trick

Here is my settings

<script type="text/javascript">
    $(document).ready(function() {
        //assign the Colorbox event to elements
        $(".youtube").magnificPopup({
            type: 'iframe',
            src: 'http://www.youtube.com/embed/VOJyrQa_WR4?rel=0&amp;wmode=transparent'
        });
    });
</script>

Now, I try to load the pop-up after a few seconds delay, but I cannot figure out how to hide the header #header div and restore it as the pop-up exists. The following script works, but it needs further tweaking. I want to know why is the site header getting mixed up, I am not calling it? I am clueless. If you have encountered similar problems before, please contribute. Thank you.

<script type="text/javascript">
    $(window).load(function() {
        setTimeout(function() {
            $.magnificPopup.open({
                items: {
                    src: 'http://vimeo.com/123123',
                    type: 'iframe'
                },
                mainClass: 'youtube'
            });
        }, 10000); 
    });
</script>

Here is my site header:

<header class="header" id="header" role="banner">
    <div class="header-inner">
        <div id="menu-btn">
            <a href="#" title="Menu" id="menu-btn-toggle" class="menu-icon-link">
                <svg
                    xmlns="http://www.w3.org/2000/svg"
                    xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="512px" height="512px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve" id="menu-icon" class="injected-svg svg inject-svg" data-fallback="/sites/all/themes/zeus_base/images/menu-icon.png">
                    <style type="text/css">
                        #menu-icon{fill:#FFFFFF;}
                    </style>
                    <path id="menu-icon" d="M462,163.5H50v-65h412V163.5z M462,223.5H50v65h412V223.5z M462,348.5H50v65h412V348.5z"></path>
                </svg>
            </a>
        </div>
        <div class="logo">
            <a href="/" title="Home" rel="home" class="header__logo" id="logo">
                <img src="http://www.bicycling.com/sites/bicycling.com/themes/bicycling/logo.png" alt="Home" class="header__logo-image">
            </a>
        </div>
        <div class="main-menu">
            <a href="#" class="search-submit">
                <svg
                    xmlns="http://www.w3.org/2000/svg"
                    xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="512px" height="512px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve" id="search-icon" class="injected-svg svg inject-svg" data-fallback="/sites/all/themes/zeus_base/images/search.png">
                    <path id="magnifier-2-icon" d="M460.355,421.59L353.844,315.078c20.041-27.553,31.885-61.437,31.885-98.037   C385.729,124.934,310.793,50,218.686,50C126.58,50,51.645,124.934,51.645,217.041c0,92.106,74.936,167.041,167.041,167.041   c34.912,0,67.352-10.773,94.184-29.158L419.945,462L460.355,421.59z M100.631,217.041c0-65.096,52.959-118.056,118.055-118.056   c65.098,0,118.057,52.959,118.057,118.056c0,65.096-52.959,118.056-118.057,118.056C153.59,335.097,100.631,282.137,100.631,217.041   z"></path>
                </svg>
            </a>
            <?php
                echo $this->widget("searchbox", "content", array("params" => array("type" => "article")));
            ?>
            <ul id="main-menu" class="links clearfix">
                <li class="menu-16221 first">
                    <a href="/bikes-gear" id="bikesgear-main">Bikes &amp; Gear</a>
                </li>
                <li class="menu-16226">
                    <a href="/rides" id="ride-main">Rides</a>
                </li>
                <li class="menu-16231">
                    <a href="/training" id="trainingnutrition-main">Training</a>
                </li>
                <li class="menu-16241">
                    <a href="http://shop.bicycling.com/" id="culture-main" target="_blank">Shop</a>
                </li>
                <li class="menu-16246 last">
                    <a href="https://offers.rodale.com/offer/600175?keycode=I5LW0D0F&amp;cm_mmc=bicycling.com-_-Nav%20Subscribe-_-BKE%20SwA%20Nav%20Bar%20Nov15-_-OfferID_600175_keycode_I5LW0D0F" id="mainsubscribe" class="subscribe-link" target="_blank">Subscribe</a>
                </li>
            </ul>
        </div>
    </div>
</header>

Upvotes: 0

Views: 87

Answers (1)

Bosworth99
Bosworth99

Reputation: 4234

You are hiding the header onClick(), which it sounds like is working, but on close of the popup, you have no logic to show it, again. So, it remains hidden.

To put it another way, you are controlling events "near" the plugin activation, but you've lost control when plugin logic takes over (you are relying on the plugin to close itself, when the user clicks "close").

This is a very common problem, so plugin developers usually make some hooks or events available that you may leverage.

Lets check out the API:

$('.image-link').magnificPopup({
  // you may add other options here, e.g.:
  preloader: true,

  callbacks: {
    open: function() {
      // Will fire when this exact popup is opened
      // this - is Magnific Popup object
    },
    close: function() {
      // Will fire when popup is closed
    }
  }
});

So here, the developer has included a number of events for you to consume, you just need to provide a couple callback methods to do your hide and show :

<script type="text/javascript">

    function hide(div) {
        document.getElementById(div).style.display = 'none';
    }
    // lets add a show method, like your hide()
    function show(div) {
        document.getElementById(div).style.display = 'block';
    }

</script>

Then you can call those methods on the callback property of your settings, a bit like so :

callbacks : {

    open : function(){
        hide('header');
    },
    close : function(){
        show('header');
    }
}

This is a pretty simple thing to do, and there are zillions of ways of doing it. But, here, you've bought into a plugin and its functionality, so you might as well use what it gives you.

See if you can get that working, and let me know if you run into any more issues.

Good luck ;)

Note : I wonder why you are hiding the header? this is css, after all, and you might be better served simply by rendering the popup over the header.

Upvotes: 1

Related Questions