簡體   English   中英

自動打開頁面加載后無法關閉fancybox

[英]Can't close fancybox after automatic open on page load

我的fancybox出現問題,該問題會在頁面加載時自動打開。 單擊關閉按鈕后,fancybox再次打開。

你能幫我一下嗎?

<script>
 jQuery(function($){
  $(".popup-wrapper").fancybox({
  'padding': '0',
  'max-width': '50%',
  'max-height': '50%',
  'autoSize': false,
  'transitionIn': 'fade',
  'transitionOut': 'fade',
  });

 $(".popup-wrapper").trigger('click');
});
</script>

CSS:

.popup-wrapper {
 display: none;
 width: 50%;
 height: auto;
   img {
    width: 80%;
   height: auto;
 }
}

PHP和HTML

<?php $args = array('post_type' => 'popup', 'showposts' => 5,); 
$the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$meta = get_post_meta( $post->ID, 'popup_details', true ); ?>

<?php if (is_array($meta) && $meta['displayPopup'] === '1') { ?>
<div class="popup-wrapper col-12 col-xl-12">
    <?php the_post_thumbnail(); ?>
</div><!-- .popup-wrapper -->
<?php }; 
endwhile;

wp_reset_postdata(); ?>

更新僅當我單擊關閉按鈕時,問題才會出現。 當我按下ESC按鈕或單擊黑色背景時,該框將毫無問題地關閉。

我通過在代碼中添加preventDefault解決了這一問題,現在它可以工作了。 希望這對以后的人有所幫助。

jQuery(function($){
 $(".popup-wrapper").fancybox({
  'padding': '0',
  'autoSize': true,
  'transitionIn': 'fade',
  'transitionOut': 'fade',
 });

 $(".popup-wrapper").trigger('click');

 $(document).find('.fancybox-close-small').on('click', function(e) {
   e.preventDefault();
      self.close(e);
 });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM