【问题标题】:Javascript ordered image popup ... can't separate individual popupsJavascript 排序的图像弹出窗口...无法分隔单个弹出窗口
【发布时间】:2016-05-21 00:06:22
【问题描述】:

好的,这是我对 javascript 或一般编码的第一次尝试,所以我希望这不会太混乱。我的代码几乎是我想要的,但我遇到了最后一个问题 -

我有一个图像滚动表,我希望查看者能够单击每张图片以单独放大它。我正在尝试通过 showImage 函数执行此操作,如下所示:

JS:

function showImage(largeimg01) {
document.getElementById("largeimg01").visibility = 'visible';
showLargeImagePanel();
unselectAll();
}

function showImage(largeimg02) {
document.getElementById("largeimg02").visibility = 'visible';
showLargeImagePanel();
unselectAll();
}

function showImage(largeimg03) {
document.getElementById("largeimg03").visibility = 'visible';
showLargeImagePanel();
unselectAll();
}

function showLargeImagePanel() {
document.getElementById('largeImgPanel').style.visibility = 'visible';
}
function unselectAll() {
if(document.selection) document.selection.empty();
if(window.getSelection) window.getSelection().removeAllRanges();
}
function hideMe(obj) {
obj.style.visibility = 'hidden';
}

HTML: 利亚·戈切尔

<!-- javascript here -->

<link href="../wood-site-pagespopup.css" rel="stylesheet" type="text/css">

</head>
<body>

<a href="../index.html">
<p id="back"> LEAH GOTCHEL </p></a>

<!-- hidden popup images -->

<div id="largeImgPanel"  onclick="hideMe(this);">
<img class="popup" id="largeimg01" src="../images/GUS_4735retweb.jpg"       />
</div>

<div id="largeImgPanel" onclick="hideMe(this);">
<img class="popup" id="largeimg02" src="../images/GUS_5034retweb.jpg" />
</div>


<div id="largeImgPanel" onclick="hideMe(this);">
<img class="popup" id="largeimg03" src="../images/GUS_4735retweb.jpg"  />
</div>

<div id="largeImgPanel" onclick="hideMe(this);">
<img class="popup" id="largeimg04" src="../images/GUS_4735retweb.jpg" />
</div>



<table>
<tr>
<td><img src="../images/GUS_4735retweb.jpg" id="img01"    style="cursor:pointer" 
onclick="showImage(largeimg01);"></td>

<td> 
<img src="../images/GUS_5034retweb.jpg" id="img02" style="cursor:pointer" 
onclick="showImage(largeimg02);">       
</td>
<td><img src="../images/GUS_5060retweb.jpg" id="img03" style="cursor:pointer" 
onclick="showImage(largeimg03);">
</td>
<td><img src='../images/GUS_5077retweb.jpg'" id="img04" style="cursor:pointer" 
onclick="showImage(largeimg04);">
</td>

</tr>

</table>
<p id="caption">custom window bench: oak, poplar </p>




</body>
</html>

CSS:

html, body {
height:100%;
width:100%;
margin:0;}

html {
display:table;
}

body{
display:table-cell;
vertical-align:middle;
}

#back{
font-family: Corbert;
font-style: italic; 
font-size: 2em; 
color: ##006699;
position: relative; 
padding: 30px; 
margin-left: 20px; 
}


img {
padding: 20px;
width: 500px; 
}
a {
color: #006699;
margin: 0 auto;
text-decoration: none; 
}

a:hover {
color: #8b98a7;
}



p {
position: fixed;
padding: 30px;
font-family: Verdana, Arial, sans-serif;
font-size: 12px;
clear: both; 
margin-left: 20px; 
}


table {
top: 50%
left: 50% 
position: absolute; 
border:none;
}


.popup {
position: relative;
height: 90%;
width: auto;   
overflow: scroll;

}



.largeImgPanel {
text-align: center;
visibility: hidden;
position: fixed;
z-index: 100;
top: 0; left: 0; bottom: 0; 
width:100%;
height:100%; 
background-color: rgba(100,100,100, 0.5);
}

#largeImg  {
height: auto;
max-width: 100%;
}

这段代码和我在这里找到的很接近:How to enlarge a clicked image with plain JavaScript on a mobile device

弹出窗口正在运行 - 我的问题是,当我调用任何一个弹出窗口时,所有弹出图像都会出现。第一个弹出窗口 (img01) 阻止了其余的弹出窗口,因此它们无法被看到。我认为这是因为它们都包含在具有相同“largeImgPanel”ID 的 div 中 - 但如果它们不包含在“largeImgPanel”div 中,那么当我运行该函数时它们根本不会出现。如何更改脚本以使该功能仅适用于与单击的图像相对应的特定弹出图像?

这是我的第一篇文章,如果是荒谬的,我很抱歉!任何帮助深表感谢。如果我可以尝试澄清,请告诉我。

【问题讨论】:

  • showLargeImagePanel 函数中有什么?知道了。那是你的问题
  • 嗨,你看到页面了吗? rachelgallen.com/leahgotchel.html?
  • 我确实做了另一页减去已弃用的表格,但我的 Dreamweaver 崩溃了!我出去吃午饭,电脑重启了,所以我稍后会发布重新编辑
  • 实际上你的其他编辑在rachelgallen.com/leahgotchel1.html
  • 我一定会的!不,直到现在我才知道“查看源代码”,但我喜欢它!将研究您提到的其他事情...至于差距,我不确定为什么会出现在您的示例中...在我这边,图像始终位于左侧。也许我发布的css有差异?不过,这是一个很酷的设计;)

标签: javascript popup modal-dialog


【解决方案1】:

所有 4 个图像都使用相同的 id、largeImgPanel 命名,因此所有 4 个图像都会显示调用该函数的时间。将 id 更改为一个类(在您的 css 中放置一个点而不是 #)并为您的 div 提供唯一的 id,将类保留在那里

这应该可以解决您的问题,但请记住调用相关的 id!

编辑:

好的,这是您的工作(已编辑!)代码

function showImage(imgname){
var mypic=document.getElementById(imgname);
mypic.style.visibility = 'visible';
mypic.style.zIndex = '20';
unselectAll();
}
function unselectAll() {
if(document.selection) document.selection.empty();
if(window.getSelection) window.getSelection().removeAllRanges();
}
function hideMe(obj) {
obj.style.visibility = 'hidden';
obj.style.zIndex = '-50';
}
html, body {
height:100%;
width:100%;
margin:0;}
html {
display:table;
}
body{
display:table-cell;
vertical-align:middle;
}
#back{
font-family: Corbert;
font-style: italic; 
font-size: 2em; 
color: ##006699;
position: relative; 
padding: 30px; 
margin-left: 20px; 
}
td img {
padding: 20px;
width: 500px; 
}
a {
color: #006699;
margin: 0 auto;
text-decoration: none; 
}
a:hover {
color: #8b98a7;
}
p {
position: fixed;
padding: 30px;
font-family: Verdana, Arial, sans-serif;
font-size: 12px;
clear: both; 
margin-left: 20px; 
}
table {
top: 50%
left: 50% 
position: absolute; 
border:none;
}
.popup {
position: fixed;
text-align: center;
z-index: 0;  
overflow-y: scroll;
top: 5px; left: 5px;
background-color: rgba(100,100,100, 0.5);
background-size:100% 100%;
}
#popup1, #popup2, #popup3, #popup4 {
visibility: hidden;
width:800px!important;
padding:25px;
height:auto; 
margin:auto;
}
<!doctype html>
<html>
<body>
<a href="../index.html">
<p id="back"> LEAH GOTCHEL </p></a>

<!-- hidden popup images -->
<img class="popup" id="popup1" src="http://www.rachelgallen.com/images/daisies.jpg" alt="daisies" onclick="hideMe(this);"    />
<img class="popup" id="popup2" src="http://www.rachelgallen.com/images/snowdrops.jpg" alt="snowdrops" onclick="hideMe(this);"/>
<img class="popup" id="popup3" src="http://www.rachelgallen.com/images/mountains.jpg" alt="mountains" onclick="hideMe(this);"/>
<img class="popup" id="popup4" src="http://www.rachelgallen.com/images/yellowflowers.jpg" alt="yellowflowers" onclick="hideMe(this);"/>

<table>
<tr>
<td><img src="http://www.rachelgallen.com/images/daisies.jpg" alt="daisies" id="img01" style="cursor:pointer" 
onclick="showImage('popup1');"></td>
<td> 
<img src="http://www.rachelgallen.com/images/snowdrops.jpg" alt="snowdrops" id="img02" style="cursor:pointer" 
onclick="showImage('popup2');">       
</td>
<td><img src="http://www.rachelgallen.com/images/mountains.jpg" alt="mountains" id="img03" style="cursor:pointer" 
onclick="showImage('popup3');"></td>
<td><img src="http://www.rachelgallen.com/images/yellowflowers.jpg" alt="yellowflowers" id="img04" style="cursor:pointer" 
onclick="showImage('popup4');">
</td>
</tr>
</table>

<p id="caption">custom window bench: oak, poplar </p>

</body>
</html>

您会注意到您的 javascript 被巧妙地压缩为三个函数,并且您的 id 现在是唯一的。我摆脱了额外的 div,为什么你仍然需要它们,当你可以点击图像来隐藏它的时候?过于复杂的事情。显然,这些图像已被我网站上的虚拟图像替换,但它确实有效!

【讨论】:

  • 试试这段代码jsfiddle.net/RachGal/5nee1k3q我没有把自己的图片放进去测试,但是复制粘贴
  • 您好,谢谢!似乎很接近,但没有雪茄。我正在使用dreamweaver,它在设计视图中运行良好,但是在浏览器上时只有largeImgPanel 显示没有图像。好像我必须在 CSS 中调整一些东西,但还不确定是什么。感谢您的超级有用的回复!
  • 不客气!如果您发布完整的 css/HTML,尤其是 css,我将能够提供更多帮助。因为我试图重新创建但由于资源有限而无法重新创建:(
  • 您可以编辑现有问题。无需发布新的。
  • 哇,你真是个天才!!!非常感谢你的帮助!看起来绝对完美。谢谢你告诉我我离开的地方……是的,很抱歉延迟回复,看起来我们正在按照完全相反的时间表/池塘边工作。期待让我的网站启动并运行...我欠你一个!
猜你喜欢
  • 2010-12-01
  • 2021-03-07
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 2013-06-06
  • 2011-09-30
  • 1970-01-01
相关资源
最近更新 更多