简体   繁体   English

jQuery中心元素基于屏幕,而不是页面大小

[英]jquery center element based on screen, not page size

I have an element that is revealed using Zurb's Reveal Plugin , and I am having an issue with how it positions the modal window. 我有一个使用Zurb的Reveal插件揭示的元素,并且我对如何放置模式窗口有疑问。

It seems to position the element in the center of the page, above the fold. 似乎将元素定位在页面中心,即折叠上方。 however, if the modal window is activated by a link below the fold, the user is unable to see the window. 但是,如果模式窗口由折叠下面的链接激活,则用户将无法看到该窗口。 Is there a way to center the element based on the current scrolling position instead of the entire page? 有没有一种方法可以根据当前滚动位置而不是整个页面来使元素居中?

Here is, as far as I can tell, the relevant source code from the plugin: 据我所知,这是该插件的相关源代码:

Variables Set (ln 41): 变量集(ln 41):

var modal    = $(this),
topMeasure = parseInt(modal.css('top'), 10),                                                                                                
topOffset  = modal.height() + topMeasure,                                                                                                   
locked     = false,                                                                                                                         
modalBg    = $('.reveal-modal-bg'),                                                                                                         
closeButton;   

Animating the Div (ln 61) 动画Div(ln 61)

 function openAnimation() {                                                                                                                    
    if (!locked) {                                                                                                                              
      lockModal();                                                                                                                              
      if (options.animation === "fadeAndPop") {                                                                                                 
        modal.css({                                                                                                                             
            'top': $(document).scrollTop() - topOffset,                                                                                         
            'opacity': 0,                                                                                                                       
            'visibility': 'visible'                                                                                                             
        });                                                                                                                                     
        modalBg.fadeIn(options.animationSpeed / 2);                                                                                             
        modal.delay(options.animationSpeed / 2).animate({                                                                                       
          "top": $(document).scrollTop() + topMeasure + 'px',                                                                                   
          "opacity": 1                                                                                                                          
        }, options.animationSpeed, function () {                                                                                                
          modal.trigger('reveal:opened');                                                                                                       
        });                                                                                                                                     

      }   
      //etc...

Full Source Code From GitHub 来自GitHub的完整源代码

I had the same problem and I realised that I was calling the reveal function with a tag which had "#" in the href link. 我遇到了同样的问题,我意识到我在带有href链接中带有“#”标记的标签中调用了show函数。

Calling this reloaded the page before the reveal function was called so the screen position of the reveal modal jumped to the co-ordinates of the screen when the reveal button was pushed causing the modal screen to appear half off the screen. 在调用显示功能之前调用此方法重新加载了页面,因此,当按下显示按钮时,显示模态的屏幕位置跳到屏幕的坐标,导致模态屏幕出现在屏幕的一半之外。

Removing this '#' from the link sorted the issue right out... 从链接中删除此“#”可以立即解决问题...

Hope that helps... 希望有帮助...

确保模态在页面主要部分的外部,而不是在行或列的内部,这对它们而言是不正确的相对父代。

I tried rest of the answers and ended up with: 我尝试了其余答案,最后得到:

 .reveal-modal{
    position: fixed; 
 }

Seems to work just fine, still animates and behaves as I would like it to. 似乎可以正常工作,仍然可以按照我希望的方式进行动画处理和表现。

Are your divs for the modals themselves within a row/column div block? 您的div本身是否位于行/列div块中?

Example: 例:

<div class="row">
  <div class="twelve columns">
    <div id="featured"><a href="" data-reveal-id="viewModal">View My Modal</a></div>
    <div id="viewModal" class="reveal-modal large">My Modal</div>
  </div>
</div>

If this is the case the behavior you have described could occur. 如果是这种情况,可能会发生您描述的行为。

From the documentation : 文档中

Remember : Your modal should be at the end of the page, after any of your rows or columns. 切记 :模态应该在页面的末尾,在任何行或列之后。

This code below would work correctly: 下面的代码可以正常工作:

<div class="row">
  <div class="twelve columns">
    <div id="featured"><a href="" data-reveal-id="viewModal">View My Modal</a></div>
  </div>
</div>

<div id="viewModal" class="reveal-modal large">My Modal</div>

I wrote a simple script that fired on page load and looked for all reveal modals and then moved them to an empty target div just before the tag. 我编写了一个简单的脚本,该脚本在页面加载时触发并查找所有显示模式,然后将其移动到标记之前的空目标div中。 This way I could keep all my modals wherever I wanted them to be for the sake of markup separation or to make it easier for others to follow/work-on. 这样,我可以将所有模态保留在我想要的任何位置,以便于标记分离或使其他人更容易跟随/工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM