简体   繁体   English

淡出背景图像

[英]Fade out a background-image

I have a simple image menu. 我有一个简单的图像菜单。 There is a background-image which disappears on hover to show the menu items. 有一个背景图像,该图像在悬停时消失以显示菜单项。 I am looking for jQuery solution to fade out this image slowly. 我正在寻找jQuery解决方案以逐渐淡出此图像。 Any ideas? 有任何想法吗?

HTML: HTML:

<div id="m1">
    <ul class="joomla-nav">
         <li>...</li>
    </ul>
</div>

CSS: CSS:

#m1 {
     background-image:url('../images/m1.jpg');
     width:320px;
     height:210px;
     background-color:#1F91B7;
     float:left;
}
 #m1:hover {
     background-image:none;
     background-color:transparent
}
 #m1:hover .joomla-nav {
     display:block!important;
}
 #m1 .joomla-nav {
     display:none!important;
}

Many thanks! 非常感谢!

The .fadeOut() method animates the opacity of the matched elements. .fadeOut()方法可对匹配元素的不透明度进行动画处理。 Once the opacity reaches 0, the display style property is set to none, so the element no longer affects the layout of the page 一旦不透明度达到0,则显示样式属性将设置为none,因此该元素不再影响页面的布局

We can animate a simple image: 我们可以制作一个简单的图像动画:

<div id="clickme">
  Click here
</div>
<img id="book" src="book.png" alt="" width="100" height="123" />

With the element initially shown, we can hide it slowly: 使用最初显示的元素,我们可以将其缓慢隐藏:

$('#clickme').click(function() {
  $('#book').fadeOut('slow', function() {
    // Animation complete.
  });
});

Try this, and tell if it works. 试试这个,并告诉它是否有效。

jQuery's .animate() is only able to animate numeric Values. jQuery的.animate()仅能对数字值进行动画处理。

Unlike background-color a background-image is not represented by numbers, so it cannot be animated. background-color不同, background-color background-image不是用数字表示的,因此无法进行动画处理。

What you can do is use another div with no background. 您可以做的是使用没有背景的另一个div And then fade the current div, so that it will look like the current background is fading out. 然后淡入当前div,以使它看起来像是在淡出当前背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM