帮助使用 Jquery 将淡入淡出应用到背景位置更改
我对 Jquery 很陌生,但最终弄清楚了当鼠标悬停在单独的“触发”图像上时如何更改 #contentContainer 的背景图像(精灵)。然而,我一生都无法弄清楚如何将淡入淡出效果应用于过渡。基本上,我希望它在鼠标悬停和鼠标移开时平滑淡入,而不是当前突然的背景图像过渡。
这是我的(不褪色但有效)脚本:
var $j = jQuery.noConflict();
$j(document).ready(function(){
$j("#swapTrigger").hover(
function() {
$j("#contentContainer").css("background-position", "0px 1401px");
},
function() {
$j("#contentContainer").css("background-position", "0px 0px");
});
});
非常感谢您的帮助!
I'm very new to Jquery but have finally worked out how to change the background-image (a sprite) of my #contentContainer when hovering over a separate 'trigger' image. However, for the life of me I cannot work out how to apply a fade effect to the transition. Basically, instead of the current abrupt background image transition I would like it to fade in smoothly on mouseover and mouseout.
Here is my (non fading but working) script:
var $j = jQuery.noConflict();
$j(document).ready(function(){
$j("#swapTrigger").hover(
function() {
$j("#contentContainer").css("background-position", "0px 1401px");
},
function() {
$j("#contentContainer").css("background-position", "0px 0px");
});
});
Very many thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 http://api.jquery.com/animate
Try using http://api.jquery.com/animate
您可以设置背景位置属性的动画,这将为您提供两个图像之间的平滑过渡:
如果您的问题是在两个图像之间切换时淡化图像的不透明度,则无法使用单个元素来做到这一点。您需要将背景图像添加到嵌套元素中,然后将它们淡入和淡出:
悬停时,根据需要淡入和淡出它们:
You can animate the background position property, which will give you a smooth transition between the 2 images:
If your question is about fading the opacity of the images as they switch between the 2, you can't do it with a single element. You'll need to add your background image to nested elements and then fade them in and out:
On hover, fade them in and out as required: