javascrip的弹出画廊效果问题

我没有找到问题所在,我尝试在单击“查看更多”按钮后弹出更大的图像,但我在检查器按钮中收到此消息错误。addEventListner 不是一个函数,我不明白出了什么问题。

在这个例子中添加 const 是不正确的吗?因为我尝试使用 var 但没有任何效果。


海绵宝宝撒
浏览 93回答 1
1回答

www说

我发现问题在于addEventListener它不正确,并且缺少 img 标签。现在试试这个:<!DOCTYPE html><html><head>&nbsp; <meta charset="UTF-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; <link rel="stylesheet" href="style.css">&nbsp; <script src="main.js"></script>&nbsp; <title>Document</title></head><body>&nbsp; <section id="portfolio">&nbsp; &nbsp;&nbsp;&nbsp; <div class="project">&nbsp; &nbsp; <img class="project-image" src="https://source.unsplash.com/random"&nbsp; &nbsp; &nbsp; alt="">&nbsp; &nbsp; <div class="grid-overlay">&nbsp; &nbsp; &nbsp; <button class="viewbutton"> View more</button>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="project">&nbsp; &nbsp; <img class="project-image" src="https://source.unsplash.com/random" alt="">&nbsp; &nbsp; <div class="grid-overlay">&nbsp; &nbsp; &nbsp; <button class="viewbutton"> View more</button>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="project">&nbsp; &nbsp; <img class="project-image" src="https://source.unsplash.com/random"&nbsp; &nbsp; &nbsp; alt="">&nbsp; &nbsp; <div class="grid-overlay">&nbsp; &nbsp; &nbsp; <button class="viewbutton"> View more</button>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="project">&nbsp; &nbsp; <img class="project-image" src="https://source.unsplash.com/random" alt="">&nbsp; &nbsp; <div class="grid-overlay">&nbsp; &nbsp; &nbsp; <button class="viewbutton"> View more</button>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="project">&nbsp; &nbsp; <img class="project-image" src="https://source.unsplash.com/random" alt="">&nbsp; &nbsp; <div class="grid-overlay">&nbsp; &nbsp; &nbsp; <button class="viewbutton"> View more</button>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="project">&nbsp; &nbsp; <img class="project-image" src="https://source.unsplash.com/random" alt="">&nbsp; &nbsp; <div class="grid-overlay">&nbsp; &nbsp; &nbsp; <button class="viewbutton"> View more</button>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="project">&nbsp; &nbsp; <img class="project-image" src="https://source.unsplash.com/random" alt="">&nbsp; &nbsp; <div class="grid-overlay">&nbsp; &nbsp; &nbsp; <button class="viewbutton"> View more</button>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="project">&nbsp; &nbsp; <img class="project-image" src="https://source.unsplash.com/random" alt="">&nbsp; &nbsp; <div class="grid-overlay">&nbsp; &nbsp; &nbsp; <button class="viewbutton"> View more</button>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="overlay">&nbsp; &nbsp; <div class="overlay-inner">&nbsp; &nbsp; &nbsp; <div class="close">Close X</div>&nbsp; &nbsp; &nbsp; <img src="" style="width:100%;height:100%"/>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp;&nbsp;</section><script>const buttons = document.querySelectorAll('.project');const overlay = document.querySelector('.overlay');const overlayImage = document.querySelector('.overlay-inner img');function open(e){&nbsp; overlay.classList.add('open');&nbsp; const src = e.currentTarget.querySelector('img').src;&nbsp; overlayImage.src=src;}function close(){&nbsp; overlay.classList.remove('open');}&nbsp; buttons.forEach(button => button.addEventListener('click', open));&nbsp; overlay.addEventListener('click', close);</script></body></html>&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript