基于单选按钮选择的模态弹出窗口

时间:2017-10-24 14:31:11

标签: javascript jquery html css

我有这个非常接近但不完全。我正在寻找的是基于单选按钮选择循环图像,这很好。我还想在点击图像时弹出一个模态,所以如果我选择无线电选择1,图像1显示并可点击一个模态,类似于每个其他无线电选项。再次,这工作正常。什么不是基于单选按钮图像弹出模式。

这是使用的CSS:

def myMethode(MyCommand criteria){
    def count = myService.countByCriteria(criteria)
    def result = [statut: "ok", total: count]
    render result as JSON
}

单选按钮(简化)如下:

#piGal ul {
  float: left;
  list-style-type: none;
  padding: 20px 2em 3em 2em;

}
#piGal img {
    max-width: 100%;
    height: auto;
    padding: .5em .5em 2em .5em;
}
#piGal .outer {
  float: left;
  padding-bottom: 5px;
}



#piGalImg img {
    max-width: 100%;
    height: auto;
  -webkit-box-shadow: 0px 0px 1px #000;
  -moz-box-shadow: 0px 0px 1px #000;
    padding-bottom: 5px;
}


#piGalImg .outer {
  float: left;
  padding-bottom: 5px;
}



 /* Style the Image Used to Trigger the Modal */
#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 (Image Text) - Same Width as the Image */
#caption {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
    text-align: center;
    color: #ccc;
    padding: 10px 0;
    height: 150px;
}

/* Add Animation - Zoom in the Modal */
.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%;
    }
}

javascript如下:

<div class="contentText">
<section>
<h5>Choose color</h5>
<ul id="radio-attribute" class="radio-attribute">           
<li class="radio-attribute">
<label for="1" class="radio-attribute">
<input type="radio" id="1" name="id[27]" value="170" onClick="change_image(this.id)">
<img src="images/silver_swatch.png" alt="GBM0911S Silver" title=" GBM0911S Silver " width="42" height="40" />
</label>
</li>

<li class="radio-attribute">
<label for="2" class="radio-attribute">
<input type="radio" id="2" name="id[27]" value="175" onClick="change_image(this.id)">
<img src="images/blue_swatch.png" alt="GBM0911B Blue" title=" GBM0911B Blue " width="45" height="40" />
</label>
</li>
</ul>       
</section>
</div>

用于图像显示的html:

    <script type="text/javascript">
    <!--

var images = ["GLAMOURFRONT.jpg","GBM0911S.png"];

function preload(arrayOfImages) {
        $(arrayOfImages).each(function () {
            $('<img />').attr('src',this).appendTo('body').css('display','none');
        });
    }

    preload(images);

    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("piGal");
    var captionText = document.getElementById("caption");

        function change_image(radioID) {
            document.getElementById("piGal").innerHTML = "<img id=\"piGal\" src=\"images/"+ images[radioID] +"\" />";
        }

    // Get the modal

        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";
        }

    document.querySelectorAll('label.radio-attribute img').forEach(function (radioClick) {
      radioClick.addEventListener("click", function () {
        modal.style.display = "block";
        modalImg.src = this.src;
        captionText.innerHTML = this.alt;
      });
    });
    //-->
    </script>

和模式的html:

    <div id="piGal" style="float: left;">

      <a href="/images/GLAMOURFRONT.jpg"><img src="images/GLAMOURFRONT.jpg" alt="image" title=" image " width="225" height="184"  id="myImg"  /></a>   

</div>

目前,只要点击无线电图像,模态就会打开,没有图像(存储在图像阵列中)。在这很长一段时间,我非常接近解决方案......

感谢。

1 个答案:

答案 0 :(得分:0)

单击单选按钮时弹出模式,因为您在事件监听器中使用modal.style.display = "block";显式调用该行为:

document.querySelectorAll('label.radio-attribute img').forEach(function (radioClick) {
      radioClick.addEventListener("click", function () {
        modal.style.display = "block"; <-- Remove this line here.
        modalImg.src = this.src;
        captionText.innerHTML = this.alt;
      });
    });

我看到的另一个问题是,当您切换图像时,当您拨打change_image时,您会摧毁在图像上启动模态的事件监听器。替换img代码意味着新的img不会拥有相同的img.onclick处理程序。当您替换<a>时,您还没有替换包裹img标记的InnerHTML标记。

为了解决这个问题,你可以将你的听众放在piGal元素上:

  document.getElementById("piGal").onclick = function(){
    // You'll have to update this to target the image as a child of piGal.
  }

同样在您的HTML onClick中应该是onclick