0

我想在索引上显示我的参考作品。我限制了单词。我的目标是:想要了解有关帖子的详细信息的用户,将单击帖子缩略图,其余内容将以弹出窗口打开。

我使用 w3css modal 来制作它。我的javascript代码是:

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal 
btn.onclick = function() {
modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
    modal.style.display = "none";
}
}

但只有第一篇文章似乎是这样的。其他人没有任何反应。

谢谢。

我想制作所有的缩略图

我对此做了一些修改。我使用了 fancybox 2 并用这个更改了我的代码

<a href="#inline1" rel="gallery" class="fancybox"><?php the_post_thumbnail('thumbnail', array('class' => 'aligncenter')); ?></a>

<div id="inline1" style="width:400px;display: none;">
    <?php the_post_thumbnail('thumbnail', array('class' => 'aligncenter')); ?><?php the_content(); ?>

</div>

现在所有缩略图都用fancybox打开了,但这次其他缩略图的内容与第一篇文章相同。

4

2 回答 2

0

我不认为在每个容器中创建一个模态元素是正确的方法。

相反,我建议您只制作一个模态元素,并更改

单击任何图像时的模态。

因此,这是我刚刚制作的演示

JSbin

步骤将是

  1. 找出所有容器元素
  2. 将这些元素与click事件绑定
  3. 当用户单击一个元素时,提取该元素的文本和图像 src
  4. 注入模态体
  5. 删除hide
于 2016-08-09T00:14:00.187 回答
0

混合模式很好。您必须对事件处理程序以及关闭哪个事件处理程序有点挑剔,但这还不错。

var sites = document.querySelectorAll('.site');
var closes = document.querySelectorAll('.close');
var ix;

for (ix = 0; ix < sites.length; ix++) {
  sites.item(ix).addEventListener('click', showModal);
}

for (ix = 0; ix < closes.length; ix++) {
  closes.item(ix).addEventListener('click', closeModal);
}

function showModal(e) {
  e.stopPropagation();
  this.querySelector('.modal').style.display = "block";
}

function closeModal(e) {
  e.stopPropagation();
  try {
    getParent(this, 'modal').style.display = "none";
  } catch (e) {
    console.warn('Failed to find my daddy.');
  }
}

function getParent(el, cls) {
  while (el.parentElement) {
    el = el.parentElement;
    if (el.classList.contains(cls)) return el;
  }
  return null;
}
.site {
  display: inline-block;
}
.thumbnail {
  width: 100px;
  height: 100px;
  border: 1px solid black;
  margin: 10px;
}
.modal {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgba(0, 0, 0, 0.4);
}
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 50%;
}
.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  line-height: 28px;
  font-weight: bold;
  cursor: pointer;
}
.close:hover,
.close:focus {
  color: #000;
}
<link href="//cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" rel="stylesheet" />
<div class="container">
  <div class="site">
    <div class="thumbnail"></div>
    <div class="modal">
      <div class="modal-content">
        <span class="close">×</span> First Item
      </div>
    </div>
  </div>
  <div class="site">
    <div class="thumbnail"></div>
    <div class="modal">
      <div class="modal-content">
        <span class="close">×</span> Second Item
      </div>
    </div>
  </div>
</div>

于 2016-08-09T17:24:03.533 回答