화면에 보이는 책을 클릭하면 큰 이미지의 라이트박스가 나타나고, esc key를 누르거나 닫기버튼, 그림자를 클릭하면 라이트박스가 닫히는 기능을 구현해보자.
<body>
<div id="wrap">
<h1>라이트박스</h1>
<p>
<a href="#" class="book">
<img src="./images/thumb_1.png" alt="반응형웹퍼블리싱">
</a>
<a href="#" class="book">
<img src="./images/thumb_2.png" alt="UX/UI디자인">
</a>
</p>
<!-- shadow 안에 lightbox를 넣어서 사진을 클릭해도 닫히도록 /
js에서 이벤트 버블링을 막으면 사진을 클릭해도 닫히지 않는다. -->
<div class="shadow">
<div class="lightbox">
<img src="#" alt="#">
<button class="clse">닫기</button>
</div>
</div>
</div>
</body>
<style>
h1{
height: 100px;
line-height: 100px;
text-align: center;
}
.book{
display: inline-block;
width: 140px;
height: 190px;
}
img{
display: block;
width: 100%;
height: 100%;
}
.shadow{
display: none;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.75);
/* opacity: 0.75; */
}
.lightbox{
/* js에서 코딩으로 나타나게 할 것 */
position: fixed;
left: 50%;
top: 50%;
margin: -250px 0 0 -200px;
width: 400px;
height: 500px;
outline: 1px solid red;
}
.clse{
position: absolute;
right: 0;
bottom: -30px;
}
</style>
<script>
$(function(){
const $lightbox = $('.lightbox');
const $book = $('.book');
$book.on('click',function(evt){
evt.preventDefault();
const imgSrc = $(this).attr('href'); //href 변수선언
const imgAlt = $(this).children('img').attr('alt'); //alt 변수선언
$lightbox.append('<img>'); //img태그 추가
$lightbox.children('img').attr({ //img의 속성값에 부여
src : imgSrc,
alt : imgAlt
});
$lightbox.show();
});
});
</script>
<script>
$(function(){
const arrSrc = ['./images/big_1.png','./images/big_2.png'];
const arrAlt = ['반응형웹퍼블리싱','UX/UI디자인'];
const $lightbox = $('.lightbox');
const $book = $('.book');
$book.on('click',function(evt){
evt.preventDefault();
let nowIdx = $book.index(this);
$lightbox.children('img').attr({
src:arrSrc[nowIdx],
alt:arrAlt[nowIdx]
});
$lightbox.show();
});
});
</script>
<div class="shadow"></div>
태그를 추가한다. <script>
$(function(){
const arrSrc = ['./images/big_1.png','./images/big_2.png'];
const arrAlt = ['반응형웹퍼블리싱','UX/UI디자인'];
const $lightbox = $('.lightbox');
const $book = $('.book');
const $shadow = $('.shadow');
$book.on('click',function(evt){
evt.preventDefault();
let nowIdx = $book.index(this);
$lightbox.children('img').attr({
src:arrSrc[nowIdx],
alt:arrAlt[nowIdx]
});
$shadow.show();
$lightbox.show();
});
});
</script>
<script>
$(function(){
const arrSrc = ['./images/big_1.png','./images/big_2.png'];
const arrAlt = ['반응형웹퍼블리싱','UX/UI디자인'];
const $lightbox = $('.lightbox');
const $book = $('.book');
const $shadow = $('.shadow');
const $btnClse = $('.clse');
$book.on('click',function(evt){
evt.preventDefault();
let nowIdx = $book.index(this);
$lightbox.children('img').attr({
src:arrSrc[nowIdx],
alt:arrAlt[nowIdx]
});
$shadow.show();
$lightbox.show();
});
//닫기버튼
$btnClse.on('click',function(){
$lightbox.hide();
$shadow.hide();
});
//그림자를 선택해도 닫히도록
$shadow.on('click',function(){
$lightbox.hide();
$shadow.hide();
});
//이벤트 전파 막기 (사진 클릭하면 닫히지 않게)
$lightbox.on('click',function(evt){
evt.stopPropagation();
});
//esc 키를 이용한 닫기 설정 - keyup이벤트 : 키보드를 누르고 뗄떼 발생
$(document).on('keyup',function(evt){
console.log('현재 눌린 키의 번호는'+evt.which); //눌린키의 번호를 알수있음
if(evt.which=='27'){
$lightbox.hide();
$shadow.hide();
}else{
}
});
});
</script>