Javascript 模态图像仅缩放一次

Javascript 模态图像仅缩放一次,javascript,css,image,Javascript,Css,Image,。出于某种原因,使用此代码,图像上的“缩放”只发生一次(第一次单击图像时)。单击“上一步”按钮,再试一次,它不会缩放。如何更改此代码以使每次单击图像时都发生缩放 代码: #myImg{ 边界半径:5px; 光标:指针; 过渡:0.3s; } #myImg:hover{opacity:0.7;} /*模态(背景)*/ .莫代尔{ 显示:无;/*默认情况下隐藏*/ 位置:固定;/*保持原位*/ z指数:1;/*位于顶部*/ 填充顶部:100px;/*框的位置*/ 左:0; 排名:0; 宽度:100

。出于某种原因,使用此代码,图像上的“缩放”只发生一次(第一次单击图像时)。单击“上一步”按钮,再试一次,它不会缩放。如何更改此代码以使每次单击图像时都发生缩放

代码:


#myImg{
边界半径:5px;
光标:指针;
过渡:0.3s;
}
#myImg:hover{opacity:0.7;}
/*模态(背景)*/
.莫代尔{
显示:无;/*默认情况下隐藏*/
位置:固定;/*保持原位*/
z指数:1;/*位于顶部*/
填充顶部:100px;/*框的位置*/
左:0;
排名:0;
宽度:100%;/*全宽*/
高度:100%;/*全高*/
溢出:自动;/*根据需要启用滚动*/
背景色:rgb(0,0,0);/*回退色*/
背景色:rgba(0,0,0,0.9);/*黑色w/不透明度*/
}
/*模态内容(图像)*/
.模态内容{
保证金:自动;
显示:块;
宽度:80%;
最大宽度:700px;
}
/*模态图像字幕*/
#标题{
保证金:自动;
显示:块;
宽度:80%;
最大宽度:700px;
文本对齐:居中;
颜色:#ccc;
填充:10px0;
高度:150像素;
}
/*添加动画*/
.模式内容#标题{
-webkit动画名称:缩放;
-webkit动画持续时间:0.6s;
动画名称:缩放;
动画持续时间:0.6s;
}
@-webkit关键帧缩放{
来自{-webkit变换:缩放(0)}
到{-webkit转换:缩放(1)}
}
@关键帧缩放{
来自{transform:scale(0)}
到{变换:比例(1)}
}
/*关闭按钮*/
.结束{
位置:绝对位置;
顶部:15px;
右:35px;
颜色:#f1f1;
字体大小:40px;
字体大小:粗体;
过渡:0.3s;
}
.关闭:悬停,
.结束:聚焦{
颜色:#bbb;
文字装饰:无;
光标:指针;
}
/*较小屏幕上的100%图像宽度*/
@仅介质屏幕和(最大宽度:700px){
.模态内容{
宽度:100%;
}
}
图像模态
在本例中,我们使用CSS创建默认隐藏的模式(对话框)

我们使用JavaScript触发模态,并在单击模态时显示模态内部的当前图像。还要注意,我们使用图像的“alt”属性中的值作为模式中的图像标题文本

× 关闭模态的元素 var span=document.getElementsByClassName(“关闭”)[0]; //当用户单击(x)时,关闭模式对话框 span.onclick=function(){ modal.style.display=“无”; }
您使用哪个浏览器查看此页面

在Chrome版本54.0.2840.71(64位)上,它工作得非常好,允许您重复单击图像并获得相同的结果

一个问题是,提供w3schools示例是为了演示javascript是如何工作的。投入一个项目是不太合适的


如果可能的话,一个解决方案是使用开源的lightbox javascript包。它们更有可能与多种浏览器和显示尺寸兼容

我使用的是Firefox49.0.2。我刚刚在微软的Edge上测试过,它运行得非常完美-这一定是Firefox的问题。。。
<!DOCTYPE html>
<html>
<head>
<style>
#myImg {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

/* The Modal (background) */
.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 (image) */
.modal-content {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
}

/* Caption of Modal Image */
#caption {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
    text-align: center;
    color: #ccc;
    padding: 10px 0;
    height: 150px;
}

/* Add Animation */
.modal-content, #caption {    
    -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)}
}

/* The Close Button */
.close {
    position: absolute;
    top: 15px;
    right: 35px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
}

.close:hover,
.close:focus {
    color: #bbb;
    text-decoration: none;
    cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
    .modal-content {
        width: 100%;
    }
}
</style>
</head>
<body>

<h2>Image Modal</h2>
<p>In this example, we use CSS to create a modal (dialog box) that is hidden by default.</p>
<p>We use JavaScript to trigger the modal and to display the current image inside the modal when it is clicked on. Also note that we use the value from the image's "alt" attribute as an image caption text inside the modal.</p>

<img id="myImg" src="img_fjords.jpg" alt="Trolltunga, Norway" width="300" height="200">

<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">×</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>

<script>
// 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('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}

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

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

</body>
</html>