gpt4 book ai didi

jQuery 背景图像淡入

转载 作者:行者123 更新时间:2023-11-28 13:27:30 25 4
gpt4 key购买 nike

我正在尝试将背景图像淡入到我的列表项中。我已经尝试了所有方法,但找不到它...

HTML:

<li id="een">
<h1>Title</h1>
<p id="text1">Text</p>
</li>

jQuery:

$('#een').mouseenter(function(){
$('#een').css("background-image", "url(images/een.png)");
});

最佳答案

您需要制作一个绝对定位在相对父元素中的背景 img 元素。

jsFiddle

这是我的意思的一个例子。

HTML Notice I add a div with the class li-background as first item in each li. This will be edited by CSS to not show on load and be absolutely positioned within its li parent which is relatively positioned. I also set its z-index to -1 so it doesnt show in from of text or any other item in li, so its a background!

<ul>
<li id="een">
<div class="li-background"><img src="http://www.freenew.net/upload/pscreen/47/gradient-screensaver-32.jpg" /></div>
<h1>Title</h1>
<p id="text1">Text</p>
</li>
<li id="een">
<div class="li-background"><img src="http://www.freenew.net/upload/pscreen/47/gradient-screensaver-32.jpg" /></div>
<h1>Title2</h1>
<p id="text1">Text2</p>
</li>
<li id="een">
<div class="li-background"><img src="http://www.freenew.net/upload/pscreen/47/gradient-screensaver-32.jpg" /></div>
<h1>Title3</h1>
<p id="text1">Text3</p>
</li>
<li id="een">
<div class="li-background"><img src="http://www.freenew.net/upload/pscreen/47/gradient-screensaver-32.jpg" /></div>
<h1>Title4</h1>
<p id="text1">Text4</p>
</li>
</ul>

CSS

ul li { position: relative; }
ul li:hover { color: white; }
.li-background {
display: none;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
z-index: -1;
}
.li-background img {
height: 100%;
width: 100%;
}

jQ Script

$("li").hover(function(eIn) {
$(this).children(".li-background").stop().fadeIn();
},
function(eOut) {
$(this).children(".li-background").stop().fadeOut();
});

关于jQuery 背景图像淡入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13185127/

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