कैसे बनाएं: मोडल इमेज

CSS और JavaScript के द्वारा प्रतिक्रियाशील मोडल इमेज का उपयोग सीखें。

वुहान

×

亲自试一试

मोडल इमेज

मोडल बॉक्स वर्तमान पृष्ठ के शीर्ष पर दिखाई देने वाला डायलॉग/बगल खिड़की है。

यह उदाहरण पिछले उदाहरण 'मोडल बॉक्स' के अधिकांश कोड का उपयोग करता है, लेकिन इस उदाहरण में, हमने चित्र का उपयोग किया है。

पहला कदम - HTML जोड़ें:

<!-- ट्रिगर मोडल बॉक्स -->
<img id="myImg" src="img_snow.jpg" alt="Snow" style="width:100%;max-width:300px">
<!-- मॉडल फ्रेम -->
<div id="myModal" class="modal">
  <!-- बंद करने वाला बटन -->
  <span class="close">×</span>
  <!-- मॉडल सामग्री (इमेज) -->
  <img class="modal-content" id="img01">
  <!-- मॉडल शीर्षक (इमेज टेक्स्ट) -->
  <div id="caption"></div>
</div>

दूसरा कदम - CSS जोड़ें:

/* मॉडल ट्रिगर इमेज के शैली निर्धारण */
#myImg {
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}
#myImg:hover {opacity: 0.7;}
/* मॉडल (बैकग्राउंड) */
.modal {
  display: none; /* डिफ़ॉल्ट रूप से छुपा हुआ है */
  position: fixed; /* स्थान पर रहे */
  z-index: 1; /* शीर्ष पर रहे */
  padding-top: 100px; /* फ्रेम का स्थान */
  left: 0;
  top: 0;
  width: 100%; /* पूरी चौड़ाई */
  height: 100%; /* पूरी ऊंचाई */
  overflow: auto; /* आवश्यकता पड़े तो स्क्रॉल सक्षम करें */
  background-color: rgb(0,0,0); /* बैकग्राउंड का रंग */
  background-color: rgba(0,0,0,0.9); /* काला स्पष्टता सहित */
}
/* मॉडल सामग्री (इमेज) */
.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}
/* मॉडल इमेज का शीर्षक (इमेज टेक्स्ट) - इमेज के उसी चौड़ाई में */
#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}
/* एनिमेशन नेम: ज़ूम; */
.modal-content, #caption {
  animation-name: zoom;
  animation-duration: 0.6s;
}
@keyframes zoom {
  from {transform:scale(0)}
  to {transform:scale(1)}
}
/* बंद बटन */
.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% होगी */
@media only screen and (max-width: 700px){
  .modal-content {
    width: 100%;
  }
}

तीसरा कदम - जावास्क्रिप्ट जोड़ें

// मोडल फ़ेलोस को प्राप्त करें
var modal = document.getElementById("myModal");
// छवि को प्राप्त करें और इसे मोडल फ़ेलोस में इंसर्ट करें - इसके "alt" टेक्स्ट को शीर्षक के रूप में इस्तेमाल करें
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;
}
// बंद करने वाले <span> एलीमेंट को प्राप्त करें
var span = document.getElementsByClassName("close")[0];
// जब उपयोगकर्ता <span> (x) पर क्लिक करता है तो मोडल फ़ेलोस
span.onclick = function() {
  modal.style.display = "none";
}

亲自试一试

相关页面

教程:कैसे मॉडल बनाएं

教程:कैसे मॉडल गैलरी बनाएं