【问题标题】:First click on lightbox works, but not from other galleries首先点击灯箱作品,而不是来自其他画廊
【发布时间】:2015-07-24 19:46:50
【问题描述】:

我已经在 J​​avaScript 中构建了一个灯箱功能,现在我正在努力使实现在同一页面上有多个画廊时正常工作,这是我在开发时没有考虑到的功能。

为了达到预期的效果,我广泛使用数据集和 data-*-attributes(data-index 来索引,data-total 来存储 .gallerydata-current 中有多少张图片来存储哪个@应该显示在#lightbox 中的987654330@。带有缩略图<img>s 的.gallery 被包裹在<figure><a> 中,带有href 属性到更大的#lightbox img

这些属性在理论上有效,并通过随附的代码返回正确的值。页面加载后,当单击其中一个画廊时,画廊按预期工作,与其他画廊隔离,循环浏览画廊图像。

关闭灯箱并单击其他画廊之一时出现问题。 在 IE 11 中,我在开发人员工具的最后 document.querySelector 行中收到 Unable to get value of... 错误。但是,当将返回的值输出到 console.log() 时,我不明白为什么它不添加用于显示第一次点击的.gallery):

这是 JS 代码的一个大大缩短的版本。变量和选择器也被翻译成英文,以便这里的人们可以更好地理解它。仔细查看 galleryindexgalleryindexarraythisgallerythisgalleryacurrent 变量,这是解决问题的功能所在。

var lightbox = document.createElement('div'),
    gallerya = document.querySelectorAll(".gallery a"),
    current, img, i, galleryindexarr = [];
for (i = 0; i < gallerya.length; i++) {
    gallerya[i].addEventListener("click", function(e) {
        e.preventDefault();
        var galleryindex = this.parentNode.parentNode.dataset.index,
            thisgallery = document.querySelector(".gallery[data-index='" + galleryindex + "']"),
            thisgallerya = document.querySelectorAll(".gallery[data-index='" + galleryindex + "'] a");
        if(!document.getElementById("lightbox")) {
            lightbox.id = "lightbox";
            document.body.insertBefore(lightbox, document.body.firstChild);
        }
        if(galleryindexarr.indexOf(galleryindex) == -1) {
            galleryindexarr.push(galleryindex);
            for (i = 0; i < thisgallerya.length; i++) {
                img = document.createElement("img");
                img.src = thisgallerya[i].href;
                img.dataset.index = galleryindex;
                lightbox.appendChild(img);
            }
        }
        current = [].indexOf.call(thisgallery.childNodes, this.parentNode);
        document.querySelector("#lightbox img[data-index='" + galleryindex + "']:nth-of-type(" + current + ")").classList.add("current");
    });
}

这是test demo site with further visual clarification。如果您知道任何可以提供帮助的信息,请帮助我解决这个相当小的错误。

【问题讨论】:

  • 正如您在图片中看到的,in the full code 可能在您自己的控制台中on the demo site,它们已经被记录了。至于 jQuery,我知道它更容易编写,但我放弃了它,以获得更快、更纯粹的代码。
  • 对不起,我在手机上 - 我看到你确实记录了它。我什至读过挪威语:)
  • 是的,是挪威语,你怎么知道的? :) 如果你有任何知识,你能帮我吗?
  • 我是丹麦人,我很乐意提供帮助,但我无法在此处访问控制台或 IE11
  • 我明白了,非常感谢。您什么时候可以使用;几个小时后,也许?这不是那么紧急的;我会等,只要你以后能帮助我。

标签: javascript css-selectors gallery image-gallery photo-gallery


【解决方案1】:

经过将近两个小时的测试,我碰巧发现了问题。在 cmets 中提到的 @mplungjan 选择器中存在问题,因此在您的 full code 中工作,尝试用正确的选择器替换下一行(在两个控制台日志行之后)中的选择器。

替换:

 document.querySelector("#lysboks img[data-galleriindeks='" + galleriindeks + "']
 :nth-of-type(" + gjeldende + ")").classList.add("current");

作者:

 document.querySelectorAll("#lysboks img[data-galleriindeks='" + galleriindeks + "']").item(gjeldende-1).classList.add("current");

现在您应该可以看到图片了,但是下一张和上一张图片还不能使用。

要修复导航(下一个和上一个)按钮,您必须在 gå() 函数中替换下面的行以获得正确的选择器。

替换:

document.querySelector("#lysboks img[data-galleriindeks='" + document.querySelector
(".galleri[data-gjeldende]").dataset.indeks + "']:nth-of-type(" + destinasjon + ")").
classList.add("current");

作者:

document.querySelectorAll("#lysboks img[data-galleriindeks='" + document.querySelector(".galleri[data-gjeldende]").dataset.indeks + "']").item(destinasjon-1).classList.add("current");

现在它应该可以正常工作了(在 Google Chrome 中测试过)。我希望这有助于解决您的问题。祝你好运,周末愉快。

【讨论】:

  • 非常感谢。我明天会测试它,看看它是否适合我。但是,如果确实如此,我很想知道为什么会这样,而不是我的代码。 .querySelector:nth-of-type 都应该工作,因为它是有效代码,不是吗?祝你周末愉快!
  • 是的,实际上问题是 querySelector 只返回第一个 img 所以当您尝试通过 :nth-of-type 选择其中一个来添加 current 类时,它无法选择它,而不是返回与选择器选项匹配的所有img 元素的querySelectorAll,所以当我使用item() 函数选择index 时,它是有效的。
  • 感谢您的解释,但是,我不明白 - 那么您知道为什么第一次点击的画廊中的图像有效吗?我想在 JavaScript 方面变得更好,所以我需要确切地知道它为什么不起作用。我只想选择一个元素,因此我认为.querySelector 就足够了。
  • 我找到了原因!众所周知,.querySelector 内部的选择器是(并且应该是)一个 CSS 选择器。在我的 CSS 中,我尝试为每个 [data-galleriindeks] 中的每个 :nth-of-type(2) 应用一个轮廓 - 这也只适用于第一次点击!它不针对第二次单击和第三次单击的图库,并且轮廓仅应用于第一次单击的图库中的第二个 img,就像在 JavaScript 中一样。我所知道的关于 CSS 选择器(和:nth-of-type)的一切,我必须再看一遍。我现在将研究为什么连 CSS 都能像现在这样工作。
  • 令人惊讶的是,使用:nth-of-type 时似乎没有考虑[attributes]。所以这个选择器#lysboks img[data-galleriindeks="2"]:nth-of-type(1) 不适用于带有[data-galleriindeks="2"] 的第二个img,而是适用于具有该属性的第二个img。而且由于首先单击的图库是附加的第一张图像,所以它可以工作,因为编号在那里,但我还不如写#lysboks img:nth-of-type(1),它不会改变任何东西,因为在计数时不考虑属性.太可惜了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
  • 2016-03-01
  • 1970-01-01
相关资源
最近更新 更多