How to open a magnific popup on page load?

2019-02-06 04:02发布

I'm using Magnific Popup and I would like to have a video come up as soon as the page loads in a popup.

I got the plugin to work fine, but I have no idea on how to get it to pop up as soon as the page loads, without clicking on the thumbnail.

I looked around for a solution, but I did not manage to get it to work.

2条回答
放我归山
2楼-- · 2019-02-06 04:49

If you're using jQuery you could just listen for the window load event and then call the open method for your Magnific Popup like so:

(function($) {
    $(window).load(function () {
        // retrieved this line of code from http://dimsemenov.com/plugins/magnific-popup/documentation.html#api
        $.magnificPopup.open({
            items: {
                src: 'someimage.jpg'
            },
            type: 'image'

          // You may add options here, they're exactly the same as for $.fn.magnificPopup call
          // Note that some settings that rely on click event (like disableOn or midClick) will not work here
        }, 0);
    });
})(jQuery);
查看更多
时光不老,我们不散
3楼-- · 2019-02-06 04:52

I was able to get a timed modal working using jquery's setTimeout function, Just wrap .magificpopup in the settimeout function to set a delay. Change the value of 5000 (5 seconds) to whatever value you want.

See below:

$(document).ready(function () {
setTimeout(function() {
 if ($('#myModal').length) {
   $.magnificPopup.open({
    items: {
        src: '#myModal' 
    },
    type: 'inline'
      });
   }
 }, 5000);
});
查看更多
登录 后发表回答