2017-03-10 41 views
1

您好,我想知道如何在不同的圖像上動態加載模態。這是我目前的網頁。如何動態加載模態

enter image description here

假設當我點擊PI這個圖片的生命是彈出的模態。

enter image description here

我想與其他圖像發生同樣的事情。假設如果我點擊風箏衝浪者圖像,它會打開一個模式,左邊是風箏衝浪者圖像,右邊是文字。

這是我當前的代碼

$(document).ready(function() { 
 
     var $modal = $("#myModal"); 
 
     $("#lifeofpi").click(function() { 
 
     $modal.show(); 
 
     }); 
 
     $modal.find('.close').click(function() { 
 
     $modal.hide(); 
 
     }); 
 
    });
/* 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.7); /* Black w/ opacity */ 
 
} 
 

 
/* Modal Content */ 
 
.modal-content { 
 
background-color: #fefefe; 
 
margin: auto; 
 
padding: 20px; 
 
border: 1px solid #888; 
 
width: 700px; 
 
height: 500px; 
 
background-color: #101010; 
 

 
} 
 

 

 
/* The Close Button */ 
 
.close { 
 
color: #aaaaaa; 
 
float: right; 
 
font-size: 28px; 
 
font-weight: bold; 
 
} 
 

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

 
    
 

 
.modallifeofpi { 
 
cursor: pointer; 
 
height: 160px; // height 
 
//width: 30%; // width 
 
border: 5px solid white; 
 
display: inline; 
 
margin-top:0px; 
 
position: absolute; 
 
} 
 

 

 
.modalheader { 
 
color:white; 
 
margin:0; 
 
margin-left: 470px; 
 

 
} 
 

 
.modalheadertext { 
 
color:white; 
 
margin-left: 350px; 
 
margin-top:40px; 
 
} 
 
    
 
.review-img { 
 
    cursor: pointer; 
 
    height: 160px; // height 
 
    //width: 30%; // width 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<section class='images'> 
 
<img class="review-img" id="lifeofpi" src="https://images-na.ssl-images-amazon.com/images/I/51atapp7YTL._AC_US320_QL65_.jpg" alt="lifeofpi"></img> 
 
<img class="review-img" id="kiterunner" src="https://images-na.ssl-images-amazon.com/images/I/51MtGFNeYjL._AC_US320_QL65_.jpg" alt="kiterunner"></img> </img> 
 
<img class="review-img" id="starwars" src="https://images-na.ssl-images-amazon.com/images/I/51oqkfvEwZL._AC_US320_QL65_.jpg" alt="starwars"></img> 
 
<img class="review-img" id="twilight" src="https://images-na.ssl-images-amazon.com/images/I/41K99+cInvL._AC_US320_QL65_.jpg" alt="twilight"></img> 
 
</section> 
 

 
<!-- The Modal --> 
 
<div id="myModal" class="modal"> 
 
    \t <!-- Modal content --> 
 
    \t <div class="modal-content"> 
 
    \t \t <span class="close">&times;</span> 
 

 
    <img class="modallifeofpi" src="https://images-na.ssl-images-amazon.com/images/I/51atapp7YTL._AC_US320_QL65_.jpg" alt="lifeofpi"></img> 
 
    <h1 class="modalheader">Life of pi</h1> 
 
    <h2 class="modalheadertext">Published:</h2> 
 
    \t </div> 
 
</div>

+0

你忘了加上'jQuery'摘錄 – Rajesh

+0

對不起,我不知道爲什麼片斷心不是工作它是如何出現在我的網頁。並且我在片段中添加了jquery – cars

+0

您是否有一個外部源,您從中獲取模態信息,或者您只是將該信息與圖像一起放在頁面上? –

回答

2

您只需綁定的所有圖像,並通過圖像信息模態。

$(document).ready(function() { 
 
     var $modal = $("#myModal"); 
 
     $(".images img").click(function() { 
 
    $modal.find('img.modallifeofpi').attr('src',$(this).attr('src')); 
 
    $modal.find('.modalheader').text($(this).attr('alt')); 
 
    $modal.show(); 
 
}); 
 
$modal.find('.close').click(function() { 
 
    $modal.hide(); 
 
}); 
 
    });
/* 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.7); /* Black w/ opacity */ 
 
} 
 

 
/* Modal Content */ 
 
.modal-content { 
 
background-color: #fefefe; 
 
margin: auto; 
 
padding: 20px; 
 
border: 1px solid #888; 
 
width: 700px; 
 
height: 500px; 
 
background-color: #101010; 
 

 
} 
 

 

 
/* The Close Button */ 
 
.close { 
 
color: #aaaaaa; 
 
float: right; 
 
font-size: 28px; 
 
font-weight: bold; 
 
} 
 

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

 
    
 

 
.modallifeofpi { 
 
width: 300px; 
 
height: 450px; 
 
border: 5px solid white; 
 
display: inline; 
 
margin-top:0px; 
 
position: absolute; 
 
} 
 

 

 
.modalheader { 
 
color:white; 
 
margin:0; 
 
margin-left: 470px; 
 

 
} 
 

 
.modalheadertext { 
 
color:white; 
 
margin-left: 350px; 
 
margin-top:40px; 
 
} 
 
    
 
.review-img { 
 
    cursor: pointer; 
 
    height: 160px; // height 
 
    //width: 30%; // width 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<section class='images'> 
 
<img class="review-img" id="lifeofpi" src="https://images-na.ssl-images-amazon.com/images/I/51atapp7YTL._AC_US320_QL65_.jpg" alt="lifeofpi"></img> 
 
<img class="review-img" id="kiterunner" src="https://images-na.ssl-images-amazon.com/images/I/51MtGFNeYjL._AC_US320_QL65_.jpg" alt="kiterunner"></img> </img> 
 
<img class="review-img" id="starwars" src="https://images-na.ssl-images-amazon.com/images/I/51oqkfvEwZL._AC_US320_QL65_.jpg" alt="starwars"></img> 
 
<img class="review-img" id="twilight" src="https://images-na.ssl-images-amazon.com/images/I/41K99+cInvL._AC_US320_QL65_.jpg" alt="twilight"></img> 
 
</section> 
 

 
<!-- The Modal --> 
 
<div id="myModal" class="modal"> 
 
    \t <!-- Modal content --> 
 
    \t <div class="modal-content"> 
 
    \t \t <span class="close">&times;</span> 
 

 
    <img class="modallifeofpi" src="./images/lifeofpi.jpg"></img> 
 
    <h1 class="modalheader">Life of pi</h1> 
 
    <h2 class="modalheadertext">Published:</h2> 
 
    \t </div> 
 
</div>

+0

該代碼的作品感謝,但如果我想添加不同的信息,如關於每本模式的書本如何做。我的意思是像一個段落。 – cars

+0

然後你可以爲每個圖像部分創建div並在那裏添加段落。會盡快給你發個例子。 –

+0

@cars,這裏是你正在尋找的例子:https://jsfiddle.net/ruhul105/x9bvLwjL/15/ –

0

使用類的選擇和設置圖像來源上的圖像生成的attr。您也可以通過這種方式

$(document).ready(function() { 
    var $modal = $("#myModal"); 
    $(".modal-img").click(function() { 
     $modal.find('img.modallifeofpi').attr('src',$(this).attr('img-src')); 
     $modal.find('.modalheader').text($(this).attr('img-title')); 
     $modal.show(); 
    }); 
    $modal.find('.close').click(function() { 
     $modal.hide(); 
    }); 
}); 

和HTML填寫其他字段:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<section class='images'> 
<img class="review-img modal-img" id="lifeofpi" src="https://images-na.ssl-images-amazon.com/images/I/51atapp7YTL._AC_US320_QL65_.jpg" img-title="Life of pi" img-src="./images/lifeofpi.jpg" alt="lifeofpi"></img> 
<img class="review-img modal-img" id="kiterunner" src="https://images-na.ssl-images-amazon.com/images/I/51MtGFNeYjL._AC_US320_QL65_.jpg" img-title="Kite Runner" img-src="./images/kiterunner.jpg" alt="kiterunner"></img> </img> 
<img class="review-img modal-img" id="starwars" src="https://images-na.ssl-images-amazon.com/images/I/51oqkfvEwZL._AC_US320_QL65_.jpg" img-title="Star Wars" img-src="./images/starwars.jpg" alt="starwars"></img> 
<img class="review-img modal-img" id="twilight" src="https://images-na.ssl-images-amazon.com/images/I/41K99+cInvL._AC_US320_QL65_.jpg" img-title="Twinlight" img-src="./images/twinlight.jpg" alt="twilight"></img> 
</section> 

<!-- The Modal --> 
<div id="myModal" class="modal"> 
    <!-- Modal content --> 
    <div class="modal-content"> 
     <span class="close">&times;</span> 

    <img class="modallifeofpi" src=""></img> 
    <h1 class="modalheader">Life of pi</h1> 
    <h2 class="modalheadertext">Published:</h2> 
    </div> 
</div> 
+0

你的代碼不工作,它甚至不會使第一個圖像模態出現 – cars

+0

@cars我沒有給你一條魚。我只是試圖教你「如何釣到魚」,但你還沒有得到它 –

0

指定的img屬性模態,然後顯示模態。檢查下面的例子。

$(document).ready(function() { 
 
    var $modal = $("#myModal"); 
 
    var src = ""; 
 

 
    $('.images img').click(function() { 
 
    $img = $(this); 
 
    $modal.find('img').attr('src', $img.attr('src')); 
 
    $modal.find('.modalheader').text($img.attr('alt')); 
 
    $modal.show(); 
 
    }); 
 

 

 
    $('#myModal .close').click(function() { 
 
    $modal.hide(); 
 
    }); 
 
});
/* 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.7); 
 
    /* Black w/ opacity */ 
 
} 
 

 

 
/* Modal Content */ 
 

 
.modal-content { 
 
    background-color: #fefefe; 
 
    margin: auto; 
 
    padding: 20px; 
 
    border: 1px solid #888; 
 
    width: 700px; 
 
    height: 500px; 
 
    background-color: #101010; 
 
} 
 

 

 
/* The Close Button */ 
 

 
.close { 
 
    color: #aaaaaa; 
 
    float: right; 
 
    font-size: 28px; 
 
    font-weight: bold; 
 
} 
 

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

 
.review-img { 
 
    cursor: pointer; 
 
} 
 

 
.modallifeofpi { 
 
    width: 300px; 
 
    height: 450px; 
 
    border: 5px solid white; 
 
    display: inline; 
 
    margin-top: 0px; 
 
    position: absolute; 
 
} 
 

 
.modalheader { 
 
    color: white; 
 
    margin: 0; 
 
    margin-left: 470px; 
 
} 
 

 
.modalheadertext { 
 
    color: white; 
 
    margin-left: 350px; 
 
    margin-top: 40px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<section class='images'> 
 
    <img class="review-img" id="lifeofpi" src="https://images-na.ssl-images-amazon.com/images/I/51atapp7YTL._AC_US320_QL65_.jpg" alt="lifeofpi" /> 
 
    <img class="review-img" id="kiterunner" src="https://images-na.ssl-images-amazon.com/images/I/51MtGFNeYjL._AC_US320_QL65_.jpg" alt="kiterunner" /> 
 
    <img class="review-img" id="starwars" src="https://images-na.ssl-images-amazon.com/images/I/51oqkfvEwZL._AC_US320_QL65_.jpg" alt="starwars" /> 
 
    <img class="review-img" id="twilight" src="https://images-na.ssl-images-amazon.com/images/I/41K99+cInvL._AC_US320_QL65_.jpg" alt="twilight" /> 
 
</section> 
 

 
<!-- The Modal --> 
 
<div id="myModal" class="modal"> 
 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
    <span class="close">&times;</span> 
 
    <img class="modallifeofpi" src="./images/lifeofpi.jpg" /> 
 
    <h1 class="modalheader">Life of pi</h1> 
 
    <h2 class="modalheadertext">Published:</h2> 
 
    </div> 
 
</div>