简体   繁体   中英

How to get a popup box to appear when you click on a div?

I´m trying to make a popup box appear when I click on a div box. I´m haven´t done a lot of js or jquery, so I have a hard time figuring out how to do it. I got a box (class="box") that take up most of the screen and then a couple of boxes inside the first box. So when the second box is clicked (class="profile1") I want an popup box to appear.I later going to insert a picture were it sayes class="picture" , would be nice if that was clickable as well

<div class="box"> 

    <div class="profile1"> 
        <div class="picture"></div>
        <p class="name">NAME</p>
    </div>

</div>

css

.box {
left: 19.5%;
top: 11.5%;
height:85%;
right:2.2%;
background-color: #F2F2F2;
border-radius: 5px;
position: absolute;
border: 1px soid F2F2F2(0, 0, 0, 0.3);
z-index:-1;
overflow: auto;
}

.profile1 {
margin-left:1.7%;
margin-top:6%;
height:40%;
width:18%;
background-color:#0D7BFF;
position:absolute;
z-index:-1;
border-radius:2px;
}

Any suggestions on how to do it? Appreciate all help

You can use jQuery UI like this:

JS:

$(function(){
        $('.profile1').on("click", function(e){
            e.preventDefault()
            $('.picture').dialog({
            width: 600,
            height: 'auto',
            modal:true,
            title: 'Picture',
            overlay: { backgroundColor: "#000", opacity: 0.9 }
            })
        })
       })

HTML:

<div class="box"> 
 <div class="box"> <div class="profile1"> 
  <p class="name">NAME</p>
 </div>
 <div class="picture">Photo Here...</div>

</div>

CSS for ".picture":

.picture{
    display:none;
    width:30%;
    height:30%;
}

And don't forget to include jQuery UI in addition to jQuery and the jQuery UI CSS:

jQuery: <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

jQuery UI: <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>

CSS: https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.min.css (you should download this CSS, not link to CDN)

JSFiddle: https://jsfiddle.net/91k8xa22/

Tell me if I can improve it :)

https://jsfiddle.net/www139/x6q7Ls5g/

 window.addEventListener('load', function() { var profileBox = document.getElementsByClassName('profile1')[0]; var picture = document.getElementsByClassName('picture')[0]; profileBox.addEventListener('click', function() { document.getElementById('popupbox').style.display = 'block'; picture.innerHTML = '<img src="http://freetopwallpaper.com/wp-content/uploads/2013/09/beautiful-background-wallpaper-hd-3.jpg">'; }); }); 
 .box { left: 19.5%; top: 11.5%; height: 85%; right: 2.2%; background-color: #F2F2F2; border-radius: 5px; position: absolute; border: 1px soid F2F2F2(0, 0, 0, 0.3); z-index: -1; overflow: auto; } .profile1 { margin-left: 1.7%; margin-top: 6%; height: 40%; width: 18%; background-color: #0D7BFF; position: absolute; z-index: -1; border-radius: 2px; } #popupbox { position: fixed; width: 50vw; height: 50vh; background-color: #eee; display: none; } 
 <div class="box"> <div class="profile1"> <div class="picture"></div> <p class="name">NAME</p> </div> </div> <!--popup box--> <div id="popupbox">This is the popup box</div> <!--end popup box--> 

here is new one,

html :

<div class="box"> 
  <div class="profile1"> 
    <div class="picture"></div>
    <p class="name">NAME</p>
  </div>
</div>
<div class="popup">popup</div>

css :

.box {
   left: 19.5%;
   top: 11.5%;
   height:85%;
   right:2.2%;
   background-color: #F2F2F2;
   border-radius: 5px;
   position: absolute;
   border: 1px soid F2F2F2(0, 0, 0, 0.3);
   z-index:-1;
   overflow: auto;
  }

.profile1 {
  margin-left:1.7%;
  margin-top:6%;
  height:40%;
  width:18%;
  background-color:#0D7BFF;
  position:absolute;
  z-index:-1;
  border-radius:2px;
}

.popup
 {
 display: none;
 width: 40%;
 height: 300px;
 margin: 0 auto;
 margin-top: 50px;
 background: #eee;
 border-radius: 8px;
 box-shadow: 0 0 10px #999;
 }

 #close
   { 
    float: right;
   }

js :

$(".profile1").click(function(){
  $(".popup").show();
})
$("#close").click(function(){
 $(".popup").hide();
})

http://jsfiddle.net/erasad22/vjpsrtux/

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