gpt4 book ai didi

javascript - 在悬停Jquery上添加图片背景和文字

转载 作者:行者123 更新时间:2023-11-28 16:19:14 26 4
gpt4 key购买 nike

我正在尝试更改以在悬停时更改图片。这是我到目前为止所做的:

HTML

<div class="picture">
<div class="text">
<h1>Hey everyone!</h1>
</div>

</div>

CSS

.picture{
width: 200px;
height: 200px;
background-image: url("https://www.w3schools.com/css/trolltunga.jpg");}.text{
background-color: rgba(255,0,0,0.8);
width:200px;
height: 200px;
display: none;}

javascript

$(".picture").hover(function(){
$(this).children(".text").fadeToggle();});

最佳答案

jQuery(改变背景/显示文本):

HTML:

<div class="picture">
<div class="text">
<h1>Hey everyone!</h1>
</div>
</div>

CSS:

.picture {
width: 200px;
height: 200px;
background-image: url("https://www.w3schools.com/css/trolltunga.jpg");
}

.text {
background-color: rgba(255, 0, 0, 0.4);
width: 200px;
height: 200px;
display: none;
}

JS:

$(".picture").hover(function() {

$(this).animate({
opacity: 0.7
}, 100, function() {
$(this).css("background-image", "url('https://upload.wikimedia.org/wikipedia/en/2/27/Bliss_%28Windows_XP%29.png')");
});

$(".text").fadeToggle();
}, function() {

$(this).animate({
opacity: 0.7
}, 100, function() {
$(this).css("background-image", "url('https://www.w3schools.com/css/trolltunga.jpg')");
});

$(".text").fadeToggle();
});


https://jsfiddle.net/wlecki/28vschr1/

仅 CSS 解决方案:

HTML:

<div class="picture">
<div class="text">
<h1>Hey everyone!</h1>
</div>
</div>

CSS:

.picture {
width: 200px;
height: 200px;
background-image: url("https://www.w3schools.com/css/trolltunga.jpg");
}

.text {
background-color: rgba(255, 0, 0, 0.4);
width: 200px;
height: 200px;
opacity: 0;
}

.picture:hover {
background-image: url("https://upload.wikimedia.org/wikipedia/en/2/27/Bliss_%28Windows_XP%29.png");
}

.picture:hover .text {
opacity: 1;
}

https://jsfiddle.net/wlecki/9vnokdec/

关于javascript - 在悬停Jquery上添加图片背景和文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54233252/

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