<a href="#n">
<img src="../images/over2.jpg" />
<span class="over_bg"></span>
<span class="over_txt">마우스 오버하면<br />나타나는 텍스트</span>
</a>
a{
position: relative;
font-size: 0;
line-height: 0;
text-decoration: none;
display: block;
}
a .over_bg{
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: 1s;
background: rgba(54,119,251,0.9);
}
a .over_txt{
position: absolute;
left: 0;
top: 0;
width: 100%;
text-align: center;
opacity :0;
transition: 1s;
color: #fff;
font-size: 15px;
line-height: 1.5;
}
a:hover .over_bg{opacity: 1;}
a:hover .over_txt{
top: 40%;
opacity: 1;
}
-
1번 마우스 오버하면 나타날 효과를 설계한다.
이미지만 보이다가 이미지에 마우스를 올리면 색이 덮어지고 글자가 나타난다.
이미지를 덮을 색상 over_bg와 나타날 텍스트 ober_txt를 코딩한다.
-
2번 마우스를 오버한 상태로 css를 코딩한다.
a{
position: relative;
font-size: 0;
line-height: 0;
text-decoration: none;
display: block;
}
a .over_bg{
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(54,119,251,0.9);
}
a .over_txt{
position: absolute;
left: 0;
top: 40%;
width: 100%;
text-align: center;
color: #fff;
font-size: 15px;
line-height: 1.5;
}
a를 기준으로 해서(position:relative) 이미지의 위에 over_bg, over_txt가 올라가기 때문에 모두 position:absolute;로 배치해야함.
a의 높이는 img의 높이로 결정되기 때문에 img태그에 행간이 적용되어 img하단에 여백이 발생한다. 그렇기 때문에 line-height:0;font-size:0;을 img를 감싸고 있는 a태그에 줘야 여백이 사라짐.
반대로 이때문에 over_txt에는 글자크기와 행간을 줘야함.
-
3번 마우스를 오버하면 나타나는 요소에 transtion:1s;opacity:0;을 추가한다.
transtion:1s;는 효과를 1초동안 실행다는 값으로 변경가능 함.
opacity:0;은 자연스럽게 나타나는 효과를 주기 위해서 투명하게 처리한 값. 절대 display:none;을 주면 안됨.
-
4번 마우스를 오버했을 경우 변경되는 값을 코딩한다.
a:hover .over_bg,
a:hover .over_txt{opacity: 1;}
주의사항 a:hover .over_bg 일때 변경사항을 코딩하면 반드시 a .over_bg에 transition을 미리 선언했어야함.
a .over_bg{
opacity: 0;
transtion: 1s;
}
a .over_txt{
opacity: 0;
transtion: 1s;
}
a:hover .over_bg,
a:hover .over_txt{opacity: 1;}
-
5번 모바일은 터치로 동작하므로 마우스 오버 없음!!!
만약 이 효과를 pc버전에서만 적용되게 하려면
@media screen and (min-width:641px){
a:hover .over_bg,
a:hover .over_txt{opacity:1;}
}
이렇게 선언하면 마우스 오버효과는 641px이상에서만 적용된다.