試試這個:
HTML:
<section class="my-work-area">
<div class="my-item">
<img src="http://i.imgur.com/PAj4Ky9.jpg">
<div class="my-caption text-center">
<figure class="effect-hera">
<figcaption>
<p>
<a href="http://www.google.com" target="_blank"><i class="fa fa-moon-o"></i></a>
<a href="http://www.yahoo.com" target="_blank"><i class="fa fa-smile-o"></i></a>
<a href="http://www.apple.com" target="_blank"><i class="fa fa-star-o"></i></a>
<a href="http://www.microsoft.com" target="_blank"><i class="fa fa-sun-o"></i></a>
</p>
</figcaption>
</figure>
</div>
</div>
</section>
CSS:
.my-work-area {
width: 100%;
max-width: 420px;
height: auto;
}
.my-item {
overflow: hidden;
position: relative;
}
.my-item img {
width: 100%;
-webkit-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
.my-item.active img,
.my-item.active img {
-webkit-transform: scale(1.2);
transform: scale(1.2);
}
.my-item.active .my-caption {
background: rgba(0, 0, 0, 0.75);
visibility: visible;
}
.my-item .my-caption {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
-webkit-transition: all 0.25s ease-in-out 0s;
transition: all 0.25s ease-in-out 0s;
visibility: hidden;
}
.my-item .my-caption figure {
position: relative;
float: left;
overflow: hidden;
width: 100%;
height: 100%;
}
.my-item .my-caption figure figcaption {
padding: 2em;
font-size: 3em;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
}
.my-item .my-caption figure figcaption::before,
.my-item .my-caption figure figcaption::after {
pointer-events: none;
}
.my-item .my-caption figure figcaption,
.my-item .my-caption figure figcaption > a {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.my-item .my-caption figure figcaption > a {
z-index: 1000;
text-indent: 200%;
white-space: nowrap;
font-size: 0;
opacity: 0;
}
.my-item .my-caption figure p {
margin: 0;
letter-spacing: 2px;
font-size: 60%;
}
figure.effect-hera {
background: transparent;
}
figure.effect-hera p {
position: absolute;
top: 50%;
left: 50%;
-webkit-transition: opacity 0.35s, transform 0.35s;
transition: opacity 0.35s, transform 0.35s;
-webkit-transform: translate3d(-50%,-50%,0);
transform: translate3d(-50%,-50%,0);
-webkit-transform-origin: 50%;
transform-origin: 50%;
}
figure.effect-hera figcaption::before {
position: absolute;
top: 50%;
left: 50%;
content: '';
opacity: 0;
-webkit-transition: opacity 0.35s, transform 0.35s;
transition: opacity 0.35s, transform 0.35s;
-webkit-transform: translate3d(-50%,-50%,0) rotate3d(0,0,1,-45deg) scale3d(0,0,1);
transform: translate3d(-50%,-50%,0) rotate3d(0,0,1,-45deg) scale3d(0,0,1);
-webkit-transform-origin: 50%;
transform-origin: 50%;
}
figure.effect-hera p {
width: 100px;
text-transform: none;
line-height: 1.8;
}
figure.effect-hera p a {
color: linen;
}
figure.effect-hera p a:hover {
color: gold;
}
figure.effect-hera p a i {
font-size: 1.2em;
}
figure.effect-hera p a i {
opacity: 0;
-webkit-transition: opacity 0.35s, transform 0.35s;
transition: opacity 0.35s, transform 0.35s;
}
figure.effect-hera p a:first-child i {
-webkit-transform: translate3d(-60px,-60px,0);
transform: translate3d(-60px,-60px,0);
}
figure.effect-hera p a:nth-child(2) i {
-webkit-transform: translate3d(60px,-60px,0);
transform: translate3d(60px,-60px,0);
}
figure.effect-hera p a:nth-child(3) i {
-webkit-transform: translate3d(-60px,60px,0);
transform: translate3d(-60px,60px,0);
}
figure.effect-hera p a:nth-child(4) i {
-webkit-transform: translate3d(60px,60px,0);
transform: translate3d(60px,60px,0);
}
.my-item.active figure.effect-hera figcaption::before {
opacity: 1;
-webkit-transform: translate3d(-50%,-50%,0) rotate3d(0,0,1,-45deg) scale3d(1,1,1);
transform: translate3d(-50%,-50%,0) rotate3d(0,0,1,-45deg) scale3d(1,1,1);
}
.my-item.active figure.effect-hera p i:empty {
-webkit-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
opacity: 1;
}
的jQuery:
$('.my-item').on('click', function() {
$('.my-item').removeClass('active');
$(this).addClass('active');
});
$(document).on("click", function(e) {
if (!($(e.target).is('.my-item') || $($(e.target).parents('.my-item')).is('.my-item'))){
$('.my-item').removeClass('active');
}
});
演示:https://jsfiddle.net/gwuuzmah/5/
即使這個工作,它需要用戶使用JavaScript,而通過':focus'僞類可以用純CSS實現解決方案。 – Roberrrt
謝謝你的回覆穆罕默德。到目前爲止,我決定採用這種方法,但仍需要jQuery的一些幫助。實際上,我在我的工作區域中有多個.my-item。所以我需要在點擊或點擊相同的.my-item第二次或在其外面時停用懸停效果。任何修改腳本的想法?非常感謝。 –
@DevinMichaelson你測試了這段代碼嗎?點擊後效果將失效。並且如果接受的答案不起作用,則必須更改標記的答案。 –