简体   繁体   English

更改弹出窗口的位置?

[英]Change location of pop-up?

I'm relatively new to this and trying to create a pop-up.我对此比较陌生,并试图创建一个弹出窗口。 I'm not sure how to get the pop-up to be in the middle of the page and still populate the box correctly, since it ends up being inside the table.我不确定如何让弹出窗口位于页面中间并仍然正确填充框,因为它最终位于表格内。 I tried setting a position in the CSS but that didn't work.我尝试在 CSS 中设置 position 但这没有用。 Maybe I did it wrong?也许我做错了?

PHP PHP

 foreach ($updateInfo['updates'] as $update) {
  echo "<table><tr><td>";
  if (isset($update['details']['newsDetails']['fldDatePosted'])) {
             echo '<a class="news_popper">'.$update['details']['newsDetails']['fldDatePosted'].'</a><div class="news_pop"><p>'.$update['details']['newsDetails']['fldBody'].'</p></div>';
  }
  echo "</td></tr></table>";
}

CSS CSS

    .news_pop {
        display:none;
        position: absolute;
        z-index: 99999;
        padding: 10px;
        background: #ffffff;
        border: 1px solid #A2ADBC;
    }

JS JS

   $(function() {
     $('a.news_popper').click(function() {
            $(this).next(".news_pop").toggle();
     });
   });

I would suggest coding a popup in a more object oriented approach, that way you can call it whenever you need it throughout the page, and you can have multiple instances if needed.我建议以更面向 object 的方法编写一个弹出窗口,这样您就可以在整个页面中随时调用它,并且如果需要,您可以拥有多个实例。 What I would do is create a constructor for the popup and a div that manages the pop ups.我要做的是为弹出窗口创建一个构造函数和一个管理弹出窗口的 div。

First the pop up manager:首先弹出管理器:

const popUpManager = (function popUpManager() {


const $popUpManager = document.createElement('div');
  $popUpManager.className = "pop-up_manager";
  
  document.body.append($popUpManager);
  
  return {
    createPopUp: () => {
      const popup = new Popup();
      const $popup = popup.getPopup();
      $popUpManager.append($popup);
      return popup;
    }
  }
})();

We create a div called pop-up_manager that will house your popup and allow you to place it where ever you need throughout the page.我们创建了一个名为 pop-up_manager 的 div,它将容纳您的弹出窗口,并允许您将其放置在整个页面中您需要的任何位置。

Then we need to create the blueprint for what a popup is:然后我们需要为弹出窗口创建蓝图:

class Popup{
  constructor() {
    this.$popup = document.createElement('div');
    this.$popup.className = 'pop-up';
    this.$popup.innerHTML = '<div class="exit-button">X</div><div class="body"></div>';
    
    this.$exitButton = this.$popup.querySelector('.exit-button');
    this.$body = this.$popup.querySelector('.body');
    
    this.setUpListeners();
  }
  
  getPopup() {
    return this.$popup;
  }
  
  setContent($content) {
    if (typeof $content === 'string') {
      this.$body.innerHTML = $content;
    } else {
      this.$body.appendChild($content);
    }
  }

Every time we call new Popup();每次我们调用 new Popup(); we will generate a div that floats over the page that we can set to house anything we want by passing content to the setContent() method.我们将生成一个浮动在页面上的 div,我们可以通过将内容传递给 setContent() 方法来设置它以容纳我们想要的任何内容。 This will create what is in the pop up.这将创建弹出窗口中的内容。

Finally, the styling can be configured to whatever you want in the css:最后,可以在 css 中将样式配置为您想要的任何样式:

.pop-up {
  position: fixed;
  height: 300px;
  width: 300px;
  background-color: grey;
  
/*  Positioning  */
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.body {
  height: 100%;
  width: 100%;
}

.exit-button {
  cursor: pointer;
}

To call the popup from anywhere in you JS all you have to do is:要从 JS 中的任何位置调用弹出窗口,您所要做的就是:

$('a.news_popper').click(function() {
    const popup = popUpManager.createPopUp();

    popup.setContent('<h1>INSERT CONTENT TO SHOW USER HERE</h1>');
});

Here is a codepen: CodePen Link这是一个代码笔: CodePen 链接

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

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