当悬停单个元素时,Jquery会触发多个元素



我正在创建多张卡,当我将鼠标悬停在其中一张卡上时,卡内的图像会变大,我已经在Jquery上完成了这一操作。我的问题是,当我放置多张卡并将鼠标悬停在一个卡元素上时,所有其他卡也会被触发。除了在CSS中为每张卡都有单独的类之外,还有什么方法可以解决这个问题吗?

$(document).ready(function() {
$('.cardjs-1,.cardjs-2').mouseover(function() {
$('.cardimage').addClass('card-js-hover');
$('.cards').mouseout(function() {
$('.cardimage').removeClass('card-js-hover');
});
});
});
.cards-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
align-content: center;
justify-content: center;
padding: 50px;
}
.cards {
display: grid;
grid-template-rows: 3fr 1fr 1fr 1fr;
align-items: center;
text-align: center;
width: 200px;
height: auto;
background: #f2f2f2;
border-radius: 15px;
box-shadow: 10px 10px 80px 10px #a29d9d;
overflow: hidden;
}
.cardimage {
display: block;
margin-left: auto;
margin-right: auto;
width: 100px;
height: 100px;
border-radius: 100px;
transition: border-radius .1s, width .1s, height .1s;
}
.card-js-hover {
border-radius: 0;
width: 100%;
height: 100%;
}
.btn-book {
padding: 15px;
background: #F25F5C;
color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cards-container">
<div class="cards cardjs-1">
<img src="https://images.pexels.com/photos/338515/pexels-photo-338515.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="paris-image" class="cardimage">
<h4>PARIS</h4>
<p>$500/4 days</p>
<a class="btn-book" href="#">Book Now</a>
</div>
<div class="cards cardjs-2">
<img src="https://images.pexels.com/photos/2570063/pexels-photo-2570063.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" class="cardimage">
<h4>BERLIN</h4>
<p>$200/3 days</p>
<a class="btn-book" href="#">Book Now</a>
</div>
</div>

问题是因为您选择了所有.cardimage元素。只需使用this关键字和jQuery的find()方法选择与悬停的.cards元素相关的元素。

还要注意在下面的示例中使用单个.cards选择器以及hover()toggleClass()来简化逻辑。

$(document).ready(function() {
$('.cards').hover(function() {
$(this).find('.cardimage').toggleClass('card-js-hover');
});
});
.cards-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
align-content: center;
justify-content: center;
padding: 50px;
}
.cards {
display: grid;
grid-template-rows: 3fr 1fr 1fr 1fr;
align-items: center;
text-align: center;
width: 200px;
height: auto;
background: #f2f2f2;
border-radius: 15px;
box-shadow: 10px 10px 80px 10px #a29d9d;
overflow: hidden;
}
.cardimage {
display: block;
margin-left: auto;
margin-right: auto;
width: 100px;
height: 100px;
border-radius: 100px;
transition: border-radius .1s, width .1s, height .1s;
}
.card-js-hover {
border-radius: 0;
width: 100%;
height: 100%;
}
.btn-book {
padding: 15px;
background: #F25F5C;
color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cards-container">
<div class="cards cardjs-1">
<img src="https://images.pexels.com/photos/338515/pexels-photo-338515.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="paris-image" class="cardimage">
<h4>PARIS</h4>
<p>$500/4 days</p>
<a class="btn-book" href="#">Book Now</a>
</div>
<div class="cards cardjs-2">
<img src="https://images.pexels.com/photos/2570063/pexels-photo-2570063.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" class="cardimage">
<h4>BERLIN</h4>
<p>$200/3 days</p>
<a class="btn-book" href="#">Book Now</a>
</div>
</div>

但是 ,值得注意的是,您不需要JS。仅在CSS中就可以实现完全相同的效果。这样做的好处是不依赖JS来使UI工作,而且由于CSS是硬件加速的,JS不是,所以执行更好。

.cards-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
align-content: center;
justify-content: center;
padding: 50px;
}
.cards {
display: grid;
grid-template-rows: 3fr 1fr 1fr 1fr;
align-items: center;
text-align: center;
width: 200px;
height: auto;
background: #f2f2f2;
border-radius: 15px;
box-shadow: 10px 10px 80px 10px #a29d9d;
overflow: hidden;
}
.cardimage {
display: block;
margin-left: auto;
margin-right: auto;
width: 100px;
height: 100px;
border-radius: 100px;
transition: border-radius .1s, width .1s, height .1s;
}
.cards:hover .cardimage, 
.card-js-hover {
border-radius: 0;
width: 100%;
height: 100%;
}
.btn-book {
padding: 15px;
background: #F25F5C;
color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cards-container">
<div class="cards cardjs-1">
<img src="https://images.pexels.com/photos/338515/pexels-photo-338515.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="paris-image" class="cardimage">
<h4>PARIS</h4>
<p>$500/4 days</p>
<a class="btn-book" href="#">Book Now</a>
</div>
<div class="cards cardjs-2">
<img src="https://images.pexels.com/photos/2570063/pexels-photo-2570063.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" class="cardimage">
<h4>BERLIN</h4>
<p>$200/3 days</p>
<a class="btn-book" href="#">Book Now</a>
</div>
</div>

您可以只使用css选择器.cards:hover .cardimage:

.cards-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
align-content: center;
justify-content: center;
padding: 50px;
}
.cards {
display: grid;
grid-template-rows: 3fr 1fr 1fr 1fr;
align-items: center;
text-align: center;
width: 200px;
height: auto;
background: #f2f2f2;
border-radius: 15px;
box-shadow: 10px 10px 80px 10px #a29d9d;
overflow: hidden;
}
.cardimage {
display: block;
margin-left: auto;
margin-right: auto;
width: 100px;
height: 100px;
border-radius: 100px;
transition: border-radius .1s, width .1s, height .1s;
}
.cards:hover .cardimage{
border-radius: 0;
width: 100%;
height: 100%;
}
.btn-book {
padding: 15px;
background: #F25F5C;
color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cards-container">
<div class="cards cardjs-1">
<img src="https://images.pexels.com/photos/338515/pexels-photo-338515.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="paris-image" class="cardimage">
<h4>PARIS</h4>
<p>$500/4 days</p>
<a class="btn-book" href="#">Book Now</a>
</div>
<div class="cards cardjs-2">
<img src="https://images.pexels.com/photos/2570063/pexels-photo-2570063.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" class="cardimage">
<h4>BERLIN</h4>
<p>$200/3 days</p>
<a class="btn-book" href="#">Book Now</a>
</div>
</div>

相关内容

  • 没有找到相关文章

最新更新