单击图像时图像模式未打开。JavaScript不工作
我制作了我想在点击时打开的画廊模式图片,但没有实现。基本上,我希望在单击每个图像时将其图像id传递给JavaScript。脚本必须以弹出窗口或模式以及关闭按钮的形式打开相应的图像 下面是CSS单击图像时图像模式未打开。JavaScript不工作,javascript,html,css,image,modal-dialog,Javascript,Html,Css,Image,Modal Dialog,我制作了我想在点击时打开的画廊模式图片,但没有实现。基本上,我希望在单击每个图像时将其图像id传递给JavaScript。脚本必须以弹出窗口或模式以及关闭按钮的形式打开相应的图像 下面是CSS <style> /* The grid: Four equal columns that floats next to each other */ .column { float: left; width: 25%; padding: 10px; } /* Style the i
<style>
/* The grid: Four equal columns that floats next to each other */
.column {
float: left;
width: 25%;
padding: 10px;
}
/* Style the images inside the grid */
.column img {
opacity: 0.8;
cursor: pointer;
}
.column img:hover {
opacity: 1;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* The expanding image container */
.imgcontainer {
position: relative;
display: none;
}
/* Closable button inside the expanded image */
.closebtn {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.closebtn:hover,
.closebtn:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
/* Add Animation */
.modal-content {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
@-webkit-keyframes zoom {
from {-webkit-transform:scale(0)}
to {-webkit-transform:scale(1)}
}
@keyframes zoom {
from {transform:scale(0)}
to {transform:scale(1)}
}
</style>
/*网格:四个相等的列,彼此相邻浮动*/
.栏目{
浮动:左;
宽度:25%;
填充:10px;
}
/*在网格内设置图像样式*/
.img列{
不透明度:0.8;
光标:指针;
}
.列img:悬停{
不透明度:1;
}
/*清除列后的浮动*/
.罗:之后{
内容:“;
显示:表格;
明确:两者皆有;
}
/*扩展图像容器*/
.imgcontainer{
位置:相对位置;
显示:无;
}
/*展开图像内的可关闭按钮*/
.关闭{
位置:绝对位置;
顶部:15px;
右:35px;
颜色:#f1f1;
字体大小:40px;
字体大小:粗体;
过渡:0.3s;
}
.closebtn:悬停,
.closebtn:焦点{
颜色:#bbb;
文字装饰:无;
光标:指针;
}
.莫代尔{
显示:无;/*默认情况下隐藏*/
位置:固定;/*保持原位*/
z指数:1;/*位于顶部*/
填充顶部:100px;/*框的位置*/
左:0;
排名:0;
宽度:100%;/*全宽*/
高度:100%;/*全高*/
溢出:自动;/*根据需要启用滚动*/
背景色:rgb(0,0,0);/*回退色*/
背景色:rgba(0,0,0,0.9);/*黑色w/不透明度*/
}
.模态内容{
保证金:自动;
显示:块;
宽度:80%;
最大宽度:700px;
}
/*添加动画*/
.模态内容{
-webkit动画名称:缩放;
-webkit动画持续时间:0.6s;
动画名称:缩放;
动画持续时间:0.6s;
}
@-webkit关键帧缩放{
来自{-webkit变换:缩放(0)}
到{-webkit转换:缩放(1)}
}
@关键帧缩放{
来自{transform:scale(0)}
到{变换:比例(1)}
}
下面是HTML
<div id="myModal" class="modal">
<span class="closebtn">×</span>
<img class="modal-content" id="expandedImg">
</div>
<!-- The four columns -->
<div class="row">
<div class="column">
<img src="img/content/services/serv-71.jpg" alt="Nature" style="width:100%" onclick="myFunction(this);">
</div>
<div class="column">
<img src="img/content/services/serv-72.jpg" alt="Snow" style="width:100%" onclick="myFunction(this);">
</div>
<div class="column">
<img src="img/content/services/serv-73.jpg" alt="Mountains" style="width:100%" onclick="myFunction(this);">
</div>
<div class="column">
<img src="img/content/services/serv-74.jpg" alt="Lights" style="width:100%" onclick="myFunction(this);">
</div>
</div>
</div>
</div>
&时代;
下面是JavaScript
<script>
function myFunction(imgs) {
// Get the modal
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("imgs");
var modalImg = document.getElementById("img");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("closebtn")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}}
</script>
功能myFunction(imgs){
//获取模态
var modal=document.getElementById(“myModal”);
//获取图像并将其插入模式-使用其“alt”文本作为标题
var img=document.getElementById(“imgs”);
var modalImg=document.getElementById(“img”);
img.onclick=函数(){
modal.style.display=“块”;
modalImg.src=this.src;
}
//获取关闭模态的元素
var span=document.getElementsByClassName(“closebtn”)[0];
//当用户单击(x)时,关闭模式对话框
span.onclick=function(){
modal.style.display=“无”;
}}
这修复了您的代码,我添加了两个单独的函数,一个用于打开和设置模态的src属性,另一个用于关闭模态
函数myFunction(事件){
var modal=document.getElementById(“myModal”);
modal.style.display=“块”;
document.getElementById(“expandedImg”).setAttribute(“src”,event.target.getAttribute(“src”);
}
功能关闭模式(事件){
const modal=document.getElementById(“myModal”);
modal.style.display=“无”;
}
/*网格:四个相邻浮动的相等列*/
.栏目{
浮动:左;
宽度:25%;
填充:10px;
}
/*在网格内设置图像样式*/
.img列{
不透明度:0.8;
光标:指针;
}
.列img:悬停{
不透明度:1;
}
/*清除列后的浮动*/
.罗:之后{
内容:“;
显示:表格;
明确:两者皆有;
}
/*扩展图像容器*/
.imgcontainer{
位置:相对位置;
显示:无;
}
/*展开图像内的可关闭按钮*/
.关闭{
位置:绝对位置;
顶部:15px;
右:35px;
颜色:#f1f1;
字体大小:40px;
字体大小:粗体;
过渡:0.3s;
}
.closebtn:悬停,
.closebtn:焦点{
颜色:#bbb;
文字装饰:无;
光标:指针;
}
.莫代尔{
显示:无;
/*默认情况下隐藏*/
位置:固定;
/*原地不动*/
z指数:1;
/*坐在上面*/
填充顶部:100px;
/*盒子的位置*/
左:0;
排名:0;
宽度:100%;
/*全宽*/
身高:100%;
/*全高*/
溢出:自动;
/*如果需要,启用滚动*/
背景色:rgb(0,0,0);
/*退色*/
背景色:rgba(0,0,0,0.9);
/*黑色w/不透明度*/
}
.模态内容{
保证金:自动;
显示:块;
宽度:80%;
最大宽度:700px;
}
/*添加动画*/
.模态内容{
-webkit动画名称:缩放;
-webkit动画持续时间:0.6s;
动画名称:缩放;
动画持续时间:0.6s;
}
@-webkit关键帧缩放{
从{
-webkit变换:缩放(0)
}
到{
-webkit转换:缩放(1)
}
}
@关键帧缩放{
从{
变换:缩放(0)
}
到{
变换:缩放(1)
}
}
&时代;
到目前为止,损坏的图像是否显示在模式中?