jQuery 淡入淡出上面的图像并更改后面的 CSS 背景图像
我有多个图标 IMG 在带有 css/背景图像的 UL 中并排列出。
当您将鼠标悬停在单个图标上时,IMG 会淡出为 0,然后 css/背景图像将变得可见。
我可以让它很好地淡出,但背景图像保持可见,并在 IMG 后面显示几个像素(假设主 IMG 是黑色,css/背景图像是橙色)。
如果我将背景图像位置设置为 0 0 那么它会在 IMG 的 fadeTo 完成之前消失。
所以基本上我希望背景位置在“正常,1”完成后改变。
我希望这是有道理的。
有人可以帮忙吗?
这是代码:
$("ul.socials li").hover(function() { //On hover...
var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'
//Set a background image(thumbOver) on the <a> tag - Set position to bottom
$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat 0 -37px'});
//Animate the image to 0 opacity (fade it out)
$(this).find("span").stop().fadeTo('normal', 0 , function() {
$(this).hide() //Hide the image after fade
});
} , function() { //on hover out...
//Fade the image to full opacity
$(this).find("span").stop().fadeTo('normal', 1).show();
$(this).find("a.thumb").css({'background-position' : '0 0'});
});
I have multiple icon IMG's listed side by side in a UL with css/background-images.
When you hover over a single icon the IMG fades to 0 and then the css/background-image becomes visible.
I can get it to fade back fine, but the background-image stays visible and shows a few pixels behind the IMG (lets say the main IMG is black and the css/background-image is orange).
If I set the background-image position to 0 0 then it disappears before the fadeTo of the IMG is finished.
So basically I want the background-position to change once the "normal, 1" has finished.
I hope that makes sense.
Can anybody help?
Here is the code:
$("ul.socials li").hover(function() { //On hover...
var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'
//Set a background image(thumbOver) on the <a> tag - Set position to bottom
$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat 0 -37px'});
//Animate the image to 0 opacity (fade it out)
$(this).find("span").stop().fadeTo('normal', 0 , function() {
$(this).hide() //Hide the image after fade
});
} , function() { //on hover out...
//Fade the image to full opacity
$(this).find("span").stop().fadeTo('normal', 1).show();
$(this).find("a.thumb").css({'background-position' : '0 0'});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“如果我将背景图像位置设置为 0 0,那么它会在 IMG 的 fadeTo 完成之前消失。所以基本上我希望背景位置在“正常,1”完成后立即更改。”
在
fadeTo
回调中更改背景位置:或
"If I set the background-image position to 0 0 then it disappears before the fadeTo of the IMG is finished. So basically I want the background-position to change once the "normal, 1" has finished."
Change your background position on
fadeTo
callback:or
您想向 .show() 添加回调并在演出完成后更改背景位置,如下所示
you want to add a callback to .show() and change background position after the show is completed like so