jquery - Fancybox 2.1加载AJAX模板时顶部留有较大间隙,但当我以其他方式使用Fancybox时不会出现此问题

标签 jquery html fancybox fancybox-2

在我的电子商务网站上,我使用 Fancybox 在有人单击“添加到购物车”按钮时加载购物车。这可以正常工作。页面后台不跳转到顶部,模态购物车窗口始终显示在中央,或者页面高度不够时显示在顶部。此购物车模式上只有 Top: 20px,您无法进一步向上滚动。

这是我用于 Fancybox 模式购物车的代码(运行完美):

(function ($, F) {

    // Opening animation - fly from the top
    F.transitions.dropIn = function() {

        var endPos = F._getPosition(true);

        endPos.top = (parseInt(endPos.top, 10) - 50) + 'px';
        endPos.opacity = 0;

        F.wrap.css(endPos).show().animate({
            top: '+=50px',
            opacity: 1
        }, {
            duration: F.current.openSpeed,
            complete: F._afterZoomIn
        });
    };

    // Closing animation - fly to the top
    F.transitions.dropOut = function() {
        F.wrap.removeClass('fancybox-opened').animate({
            top: '-=50px',
            opacity: 0
        }, {
            duration: F.current.closeSpeed,
            complete: F._afterZoomOut
        });
    };

}(jQuery, jQuery.fancybox));

$(".cart-button").fancybox({
maxWidth    : 700,
maxHeight   : 600,
fitToView   : false,
width       : '100%',
height      : '70%',
autoSize    : false,
autoDimensions: false,
autoCenter : true,
closeClick  : false,
openMethod:'dropIn',
openSpeed:200,
closeMethod:'dropOut',
closeSpeed:200,
helpers : {
  overlay : {
      locked : true // try changing to true and scrolling around the page
  }
}
});

现在我需要以另一种方式使用 Fancybox,其中包括在单击按钮时通过 AJAX 加载页面模板。与模态购物车不同,此页面模板足够长,始终会超过可见页面高度。问题是它不断将 Top: 632px (变化)分配给 Fancybox 模态,然后在顶部留下一个大的空白点。也就是说,它确实在屏幕顶部显示 Fancybox 模式,顶部有 20 像素的空间(如购物车),但是,用户可以继续向上滚动以查看顶部额外的 600+ 像素的空白空间(在此空间中可以看到深色覆盖层)。这个问题似乎只发生在桌面上,因为它在移动设备上表现得很好。

这是我用于通过 Fancybox 加载模板的代码:

$(document).ready(function() {
  $(".trees-modal-button").click(function(e) {
    e.preventDefault();
    $.fancybox({
        maxWidth    : 700,
        maxHeight   : 600,
        fitToView   : false,
        width       : '100%',
        height      : '70%',
        autoSize    : false,
        autoDimensions: false,
        autoCenter : true,
        closeClick  : false,
        openMethod:'dropIn',
        openSpeed:200,
        closeMethod:'dropOut',
        closeSpeed:200,
        helpers : {
          overlay : {
              locked : true // try changing to true and scrolling around the page
          }
        },
        content: `<div class="loading-directive" style="display: block; position: relative; margin: 
        auto;">
         <div class="loader">
           <svg class="circular">
            <circle class="path" cx="32" cy="32" r="30" fill="none" stroke-width="4">
            </circle>
           </svg>
         </div>
       </div>`
      });

    $.get('/pages/tree-planting-temp-noindex', null, function(data) {
        $.fancybox({
          maxWidth  : 700,
          maxHeight : 600,
          fitToView : false,
          width     : '100%',
          height        : '70%',
          autoSize  : false,
          autoDimensions: false,
          autoCenter : true,
          closeClick    : false,
          openMethod:'dropIn',
          openSpeed:200,
          closeMethod:'dropOut',
          closeSpeed:200,
          content: data,
          helpers : {
            overlay : {
                locked : true // try changing to true and scrolling around the page
            }
          }
        });
      }
    )
  })
}) 

它首先打开带有加载动画的模式弹出窗口,然后在准备好时显示模板。我使用与购物车相同的 Fancybox 变量,所以我不确定问题是什么。

值得一提的是,如果我强制 CSS 为 Top: 20px 来覆盖 Fancybox 模态生成的大值,模态将弹出到页面的最顶部,然后你必须向上滚动才能看到模式的顶部。

非常感谢您的帮助。

最佳答案

考虑使用 codepen 或类似工具会很有用,但如果没有,我的建议是仅触发 $.fancybox() 打开一次,而只更新内容的灯箱。您也可以选择更新尺寸。

如果初始加载旋转灯箱位置正确,并且只有在内容加载时才出现问题,这可能会解决您的问题。

如果您的加载微调器显示不正确,您可以通过完全删除 ajax 部分并添加有关 css 以及灯箱显示后加载内容的更多信息来简化您的问题。

$(document).ready(function() {
  $(".trees-modal-button").click(function(e) {
    e.preventDefault();
    $.fancybox({
      maxWidth    : 700,
      maxHeight   : 600,
      fitToView   : false,
      width       : '100%',
      height      : '70%',
      autoSize    : false,
      autoDimensions: false,
      autoCenter : true,
      closeClick  : false,
      openMethod:'dropIn',
      openSpeed:200,
      closeMethod:'dropOut',
      closeSpeed:200,
      //helpers : {
        //overlay : {
          //locked : true // try changing to true and scrolling around the page
        //}
      //},
      content: `<div class="loading-directive" style="display: block; position: relative; margin: auto;">
       <div class="loader">
         <svg class="circular">
          <circle class="path" cx="32" cy="32" r="30" fill="none" stroke-width="4">
          </circle>
         </svg>
       </div>
     </div>`
    });

    $.get('/pages/tree-planting-temp-noindex', null, function(data) {
      $.fancybox({content:data});

      // optional height resize with
      // $.fancybox.update()
    })
  })
}) 

如果这不能解决问题,请尝试将以下内容添加到初始 fancybox 首选项中:

  padding: 0,
  helpers: {
    overlay: {
    locked: false
  }

关于jquery - Fancybox 2.1加载AJAX模板时顶部留有较大间隙,但当我以其他方式使用Fancybox时不会出现此问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60131782/

相关文章:

javascript - 如何获取jquery中触发事件的对象?

javascript - jquery - 使用 href 按钮删除特定输入

javascript - javaScript 用户定义 $ 函数,如 jquery

Javascript 类型错误 : document. getElementById(...) 为空

html - z-index 问题 - div 不覆盖另一个

jquery - Wordpress 标题作为 Fancybox 标题

jQuery sweetalert2 函数没有

javascript - jQuery 如何为网站的某些部分设置动画?

html - 我在灯箱中有 HTML5 视频。为什么会播放多个视频,我该如何停止播放?

javascript - Fancybox 在第二次点击时响应并且不切换到下一张图片