2016-01-21 93 views
0

我搜索了全部內容,發現了一些解決方案,但是我無法在我的應用程序中使用它。我試圖做的是在圖像懸停時顯示黑色圖層,然後顯示文本。理想情況下,我希望文本具有看起來像按鈕的邊框。當鼠標懸停在圖像上時,用文本獲得黑色覆蓋圖

我希望這與我的懸停秤一起工作。出於某種原因,在我的實際頁面上,當將鼠標懸停在圖像上時,它除了縮放之外什麼都沒做。它不會將父div變成灰色。

我在做什麼錯?

$('.home-img-block').find('img').each(function() { 
 
    var imgClass = (this.width/this.height > 1) ? 'wide' : 'tall'; 
 
    console.log(imgClass); 
 
    $(this).addClass(imgClass); 
 
});
#home-img-blocks { 
 
\t width: 100%; 
 
\t height: 600px; 
 
} 
 

 
.home-img-block { 
 
\t width: 33.33%; 
 
\t /*height: 100%;*/ 
 
\t display: inline-block; 
 
\t overflow: hidden; 
 
\t cursor: pointer; 
 
} 
 
.home-img-block:after { 
 
    content: attr(data-content); 
 
    color:#fff; 
 
    position:absolute; 
 
    width:100%; height:100%; 
 
    top:0; left:0; 
 
    background:rgba(0,0,0,0.6); 
 
    opacity:0; 
 
    transition: all 0.5s; 
 
    -webkit-transition: all 0.5s; 
 
} 
 
.home-img-block:hover:after { 
 
    opacity:1; 
 
} 
 
.home-img-block img{ 
 
    -webkit-transition: all 1s ease; /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; /* Firefox */ 
 
    -ms-transition: all 1s ease; /* IE 9 */ 
 
    -o-transition: all 1s ease; /* Opera */ 
 
    transition: all 1s ease; 
 
} 
 
.home-img-block:hover img{ 
 
    -webkit-transform:scale(1.25); /* Safari and Chrome */ 
 
    -moz-transform:scale(1.25); /* Firefox */ 
 
    -ms-transform:scale(1.25); /* IE 9 */ 
 
    -o-transform:scale(1.25); /* Opera */ 
 
    transform:scale(1.25); 
 
\t background: rgba(0,0,0,0.3); 
 
\t width: 33.33%; 
 
\t max-height: 100%; 
 
} 
 
.home-img-block img.wide { 
 
\t max-width: 100%; 
 
    max-height: 100%; 
 
\t height: auto; 
 
} 
 
.home-img-block img.tall { 
 
\t max-width: 100%; 
 
    max-height: 100%; 
 
\t width: auto; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="home-img-blocks"> 
 
\t <div data-content="FIND OUT MORE" class="home-img-block"><img src="http://optimumwebdesigns.com/images/test1.jpg"></div><div class="home-img-block"><img src="http://optimumwebdesigns.com/images/test2.jpg"> 
 
    </div><div class="home-img-block"><img src="http://optimumwebdesigns.com/images/test3.jpg"></div> 
 
</div>

回答

1

希望這是你想要的。看一下這個。爲黑色疊加添加了疊加類。

$('.home-img-block').find('img').each(function() { 
 
    var imgClass = (this.width/this.height > 1) ? 'wide' : 'tall'; 
 
    console.log(imgClass); 
 
    $(this).addClass(imgClass); 
 
});
* { 
 
    box-sizing: border-box; 
 
} 
 

 
#home-img-blocks { 
 
    width: 100%; 
 
    height: 600px; 
 
} 
 

 
.home-img-block { 
 
    width: 33.33%; 
 
    float: left; 
 
    /*height: 100%;*/ 
 
    display: inline-block; 
 
    overflow: hidden; 
 
    cursor: pointer; 
 
    position: relative; 
 
} 
 

 
.home-img-block:hover .overlay { 
 
    background: rgba(0, 0, 0, 0.6); 
 
    width: 100%; 
 
    height: 100%; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
} 
 

 
.home-img-block:after { 
 
    content: attr(data-content); 
 
    color: #fff; 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
    opacity: 0; 
 
    transition: all 0.5s; 
 
    -webkit-transition: all 0.5s; 
 
    border: 1px solid #fff; 
 
    padding: 5px; 
 
    text-align: center; 
 
} 
 

 
.home-img-block:hover:after { 
 
    opacity: 1; 
 
} 
 

 
.home-img-block img { 
 
    -webkit-transition: all 1s ease; 
 
    /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; 
 
    /* Firefox */ 
 
    -ms-transition: all 1s ease; 
 
    /* IE 9 */ 
 
    -o-transition: all 1s ease; 
 
    /* Opera */ 
 
    transition: all 1s ease; 
 
} 
 

 
.home-img-block:hover img { 
 
    -webkit-transform: scale(1.25); 
 
    /* Safari and Chrome */ 
 
    -moz-transform: scale(1.25); 
 
    /* Firefox */ 
 
    -ms-transform: scale(1.25); 
 
    /* IE 9 */ 
 
    -o-transform: scale(1.25); 
 
    /* Opera */ 
 
    transform: scale(1.25); 
 
    background: rgba(0, 0, 0, 0.3); 
 
    width: 33.33%; 
 
    max-height: 100%; 
 
} 
 

 
.home-img-block img.wide { 
 
    max-width: 100%; 
 
    max-height: 100%; 
 
    height: auto; 
 
    width: 100%; 
 
} 
 

 
.home-img-block img.tall { 
 
    max-width: 100%; 
 
    max-height: 100%; 
 
    width: auto; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="home-img-blocks"> 
 
    <div data-content="FIND OUT MORE" class="home-img-block"> 
 
    <img src="http://optimumwebdesigns.com/images/test1.jpg"> 
 
    <div class="overlay"></div> 
 
    </div> 
 
    <div data-content="FIND OUT" class="home-img-block"> 
 
    <img src="http://optimumwebdesigns.com/images/test2.jpg"> 
 
    <div class="overlay"></div> 
 
    </div> 
 
    <div data-content="FIND" class="home-img-block"> 
 
    <img src="http://optimumwebdesigns.com/images/test3.jpg"> 
 
    <div class="overlay"></div> 
 
    </div> 
 
</div>

+0

這是完美的,除了它似乎黑色疊加放大圖像一點點。我將如何防止? – Becky

+0

@Becky你不想在懸停時縮放圖像?或縮小一點點? –

+0

選中此https://jsfiddle.net/Jinukurian7/rb7vdacu/2/ –

0

你缺少你的CSS的幾件事情,這就是爲什麼你沒有得到的期望的影響,你正在尋找。您需要添加相對於.home-img-block類的位置,然後將一些東西放在錯誤的位置,以便在懸停時發生奇怪的晃動。

下面是與它周圍的jsfiddle這樣你就可以搞一個鏈接:

https://jsfiddle.net/kriscoulson/y32ekgdm/

#home-img-blocks { 
 
    width: 100%; 
 
    height: 600px; 
 
} 
 

 
.home-img-block { 
 
    width: 33.33%; 
 
    /*height: 100%;*/ 
 
    display: inline-block; 
 
    overflow: hidden; 
 
    cursor: pointer; 
 
    position: relative; 
 
} 
 
.home-img-block:after { 
 
    content: attr(data-content); 
 
    color:#fff; 
 
    position:absolute; 
 
    width:100%; height:100%; 
 
    top:0; left:0; 
 
    background:rgba(0,0,0,0.6); 
 
    opacity:0; 
 
    transition: all 0.5s; 
 
    -webkit-transition: all 0.5s; 
 
} 
 
.home-img-block:hover:after { 
 
    opacity:1; 
 
} 
 
.home-img-block img{ 
 
    -webkit-transition: all 1s ease; Safari and Chrome 
 
    -moz-transition: all 1s ease; Firefox 
 
    -ms-transition: all 1s ease; IE 9 
 
    -o-transition: all 1s ease; Opera 
 
    transition: all 1s ease; 
 
} 
 
.home-img-block:hover img{ 
 
    transform:scale(1.25); 
 
    background: rgba(0,0,0,0.3); 
 
} 
 
.home-img-block img.wide { 
 
    max-width: 100%; 
 
    max-height: 100%; 
 
    height: auto; 
 
} 
 
.home-img-block img.tall { 
 
    max-width: 100%; 
 
    max-height: 100%; 
 
    width: auto; 
 
}

0

看起來與CSS的問題。主要是以下幾點:

home-img-block:hover img 

.home-img-block img 

因爲,.home-img-block被包裹的HTML。你不必具體。並加上第一個CSS行hover集似乎是錯誤的。我已將代碼分成小提琴。

https://jsfiddle.net/kuape5np/

你能否確認,是你想要的?

0

檢查了這一點,我希望你正在尋找相同

.home-img-block { 
 
\t width: 33.33%; 
 
\t display: inline-block; 
 
\t overflow: hidden; 
 
\t cursor: pointer; 
 
\t  position: relative; 
 
} 
 

 
.home-img-block:hover:after { 
 
    opacity:1; 
 
} 
 
.home-img-block img{ 
 
    -webkit-transition: all 1s ease; /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; /* Firefox */ 
 
    -ms-transition: all 1s ease; /* IE 9 */ 
 
    -o-transition: all 1s ease; /* Opera */ 
 
    transition: all 1s ease; 
 
\t width: 100%; 
 
} 
 
.home-img-block:hover img{ 
 
    -webkit-transform:scale(1.25); /* Safari and Chrome */ 
 
    -moz-transform:scale(1.25); /* Firefox */ 
 
    -ms-transform:scale(1.25); /* IE 9 */ 
 
    -o-transform:scale(1.25); /* Opera */ 
 
    transform:scale(1.25); 
 
\t background: rgba(0,0,0,0.3); 
 
\t max-height: 100%; 
 
} 
 
.home-img-block:after { 
 
    background: rgba(0,0,0,.5); 
 
    content: ""; 
 
    width: 100%; 
 
    height: 100%; 
 
    position: absolute; 
 
    left: 0; 
 
    top: 0; 
 
    opacity: 0; 
 
\t -webkit-transition: all 1s ease; /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; /* Firefox */ 
 
    -ms-transition: all 1s ease; /* IE 9 */ 
 
    -o-transition: all 1s ease; /* Opera */ 
 
    transition: all 1s ease; 
 
} 
 

 
.hover { 
 
    position: absolute; 
 
    z-index: 99; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50% , -50%); 
 
    opacity: 0; 
 
\t -webkit-transition: all 1s ease; /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; /* Firefox */ 
 
    -ms-transition: all 1s ease; /* IE 9 */ 
 
    -o-transition: all 1s ease; /* Opera */ 
 
    transition: all 1s ease; 
 
} 
 
.hover h2 { 
 
\t color:#fff; 
 
\t opacity: 0; 
 
\t -webkit-transition: all 1s ease; /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; /* Firefox */ 
 
    -ms-transition: all 1s ease; /* IE 9 */ 
 
    -o-transition: all 1s ease; /* Opera */ 
 
    transition: all 1s ease; 
 
} 
 

 
.home-img-block:hover:after,.home-img-block:hover .hover, .home-img-block:hover .hover h2 { 
 
    opacity: 1; 
 
}
<div id="home-img-blocks"> 
 
\t <div class="home-img-block"> 
 
    <img src="http://optimumwebdesigns.com/images/test1.jpg"> 
 
\t <div class="hover"> 
 
    <h2>FIND OUT MORE</h2> 
 
    </div> 
 
    </div> 
 
</div>

相關問題