gpt4 book ai didi

jquery 悬停效果不适用于所有 div

转载 作者:行者123 更新时间:2023-11-28 04:20:51 26 4
gpt4 key购买 nike

我试图在悬停时在图像前面添加一些文本。但它只适用于第一个 div。当我悬停剩余的 div 时,它不起作用。如果我想在所有 div 中工作,我应该怎么做?

JsFiddle

Jquery

$("#image").hover(function () {

$("#imgtext").slideToggle(250, function () {
$("#showme").slideToggle(250);
});
}, function () {

$("#showme").slideToggle(250, function () {
$("#imgtext").slideToggle(250);
});
});

HTML

<div id="image">
<img src="first.jpg" width="260" height="160"/>
<div id="imgtext"> Description of above image </div>
<div id="showme"> Show me </div>
</div><br>
<div id="image">
<img src="first.jpg" width="260" height="160"/>
<div id="imgtext"> Description of above image </div>
<div id="showme"> Show me </div>
</div><br>
<div id="image">
<img src="first.jpg" width="260" height="160"/>
<div id="imgtext"> Description of above image </div>
<div id="showme"> Show me </div>
</div><br>

CSS

    #image
{
position: relative;
}
#imgtext
{
position: absolute;
bottom: 0;
background: rgba(0,0,0,0.2);
width: 260px;
height: 30px;
}
#showme
{
width: 260px;
height: 160px;
display: none;
position: absolute;
background: rgba(0,0,0,0.2);
top: 0;
color: #FFFFFF;
}

最佳答案

id="image"改为class="image" ID必须唯一

HTML

<div class="image">
<img src="first.jpg" width="260" height="160"/>
<div class="imgtext"> Description of above image </div>
<div class="showme"> Show me </div>
</div><br>
<div class="image">
<img src="first.jpg" width="260" height="160"/>
<div class="imgtext"> Description of above image </div>
<div class="showme"> Show me </div>
</div><br>
<div class="image">
<img src="first.jpg" width="260" height="160"/>
<div class="imgtext"> Description of above image </div>
<div class="showme"> Show me </div>
</div><br>

Jquery

$(".image").hover(function () {
$that = $(this);
$that.find(".imgtext").slideToggle(250, function () {
$that.find(".showme").slideToggle(250);
});
}, function () {
$that = $(this);
$that.find(".showme").slideToggle(250, function () {
$that.find(".imgtext").slideToggle(250);
});
});

CSS

.image{
position: relative;
}
.imgtext{
position: absolute;bottom: 0;
background: rgba(0,0,0,0.2);
width: 260px;height: 30px;
}
.showme{
width: 260px;height: 160px;display: none;
position: absolute;background: rgba(0,0,0,0.2);
top: 0;color: #FFFFFF;
}

Demo

如果你想创建一个 accordian 那么你可以尝试 jquery-ui-autocompletecss-trick-autocomplete

关于jquery 悬停效果不适用于所有 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21283510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com