简体   繁体   中英

Show image in a popup when I click button

I have a button and i need to show an image in a popup when i click this button.

Why this code doesn't work? I try with a window.open in onclick event but it doesn't work

<input type="button" value="Mostra Immagine" onclick=window.open(src="../img/mypicture.jpg")>

Where is my error?

You can use a fancybox to do it.

 a { color: #FFFFFF; text-decoration: none; } .primary-btn { background-image: -moz-linear-gradient(0deg, #235ee7 0%, #4ae7fa 100%); background-image: -webkit-linear-gradient(0deg, #235ee7 0%, #4ae7fa 100%); background-image: -ms-linear-gradient(0deg, #235ee7 0%, #4ae7fa 100%); } .primary-btn { line-height: 36px; padding-left: 30px; padding-right: 30px; border-radius: 25px; border: none; color: #fff; display: inline-block; font-weight: 500; position: relative; -webkit-transition: all 0.3s ease 0s; -moz-transition: all 0.3s ease 0s; -o-transition: all 0.3s ease 0s; transition: all 0.3s ease 0s; cursor: pointer; text-transform: uppercase; position: relative; } .primary-btn { color: #fff; border: 1px solid #fff; -webkit-transition: all 0.3s ease 0s; -moz-transition: all 0.3s ease 0s; -o-transition: all 0.3s ease 0s; transition: all 0.3s ease 0s; } .primary-btn:hover { background: transparent; color: #235ee7; border-color: #235ee7; }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.0/jquery.fancybox.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.0/jquery.fancybox.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.0/jquery.fancybox.min.css"> <a data-fancybox="gallery" class="primary-btn" href="//source.unsplash.com/user/erondu/300x300">Demo</a>

您在 onclick 中缺少"并为新页面添加_blank 。使用'引号将参数与 in 函数一起传递

<input type="button" value="Mostra Immagine" onclick="window.open('../img/mypicture.jpg','_blank')">

You are using the attribute and function wrong:

onclick="window.open('../img/mypicture.jpg');"

Always quote an attribute value like you did for type and value .
And the window.open function needs a path, you don't need to write "src=".

MDN - window.open()

try this:

HTML:

<div class="popup" onclick="myFunction()">Click me!
  <span class="popuptext" id="myPopup">Popup text...</span>
</div>

CSS:

/* Popup container */
.popup {
    position: relative;
    display: inline-block;
    cursor: pointer;
}

/* The actual popup (appears on top) */
.popup .popuptext {
    visibility: hidden;
    width: 160px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 8px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    left: 50%;
    margin-left: -80px;
}

/* Popup arrow */
.popup .popuptext::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

/* Toggle this class when clicking on the popup container (hide and show the popup) */
.popup .show {
    visibility: visible;
    -webkit-animation: fadeIn 1s;
    animation: fadeIn 1s
}

/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
    from {opacity: 0;} 
    to {opacity: 1;}
}

@keyframes fadeIn {
    from {opacity: 0;}
    to {opacity:1 ;}
}

JavaScript:

<script>
// When the user clicks on <div>, open the popup
function myFunction() {
    var popup = document.getElementById("myPopup");
    popup.classList.toggle("show");
}
</script>

You can read more here https://www.w3schools.com/howto/howto_js_popup.asp Hope Helpful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM