jQuery li 图像在背景中淡入淡出(在类中淡入淡出)

发布于 2024-09-02 01:48:33 字数 283 浏览 1 评论 0原文

我目前正在使用这段代码:

var gallery = $('ul#gallery').children();

$(gallery).filter(':even').not(':last').hover(function () {$(this).toggleClass('next')});

我正在尝试让它淡入这个新类。目前,有一个

  • ;里面有图像,没有背景。添加“下一个”类时,将鼠标悬停在其上方时会为其提供背景图像。有没有办法在新类中淡入淡出而不使图像闪烁/淡出?
  • I'm currently using this code:

    var gallery = $('ul#gallery').children();
    
    $(gallery).filter(':even').not(':last').hover(function () {$(this).toggleClass('next')});
    

    I'm trying to make it fade this new class in. Currently, there's an <li> with an image in it, no background. When the 'next' class is added, it gives it a background image when hovered over. Is there a way to just fade in the new class without making the image blink/fade at all?

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    箹锭⒈辈孓 2024-09-09 01:48:33

    如果您安装了 jQueryUI,您可以通过添加持续时间来淡出动画类。

    $(this).toggleClass('next', 500);
    

    http://jqueryui.com/demos/toggleClass/

    但据我所知,有没有单独的不透明度设置仅影响背景图像。因此,如果您想淡入该元素,则需要淡入整个元素,正如您所说,这不是您想要的。

    如果您确实想要这种效果,另一种方法可能是在您为该类提供的元素前面添加一个单独的元素,并淡入该元素(及其背景图像)。

    该元素需要具有绝对定位,以便它不会影响其余内容。

    你最终会得到类似这样的结果:

    CSS:

    li {
        position: relative;
    }
    
    .background {
        width: 100%;
        height: 100%;
        background:orange;  // This would be your background image instead
        position: absolute;
        top: 0;
        left: 0;
    }
    
    .content {
        position: relative;
    }
    

    HTML:

    <ul id='gallery'>
            <li>
                <div class='background'></div>  // Prepend and fade in
                <div class='content'>hi there</div>
            </li>
    </ul>
    

    jQuery:

    hover() 有两个函数。一个是当您鼠标进入时,另一个是当您鼠标离开时。

    // 将所有 .background 元素的不透明度设置为 0
    $('.background').css({不透明度: 0});

    var gallery = $('ul#gallery').children();
    
    gallery.filter(':even').not(':last').hover(
      function () {
           $(this).find('.background').animate({'opacity': 1}, 500);
      },
      function () {
           $(this).find('.background').animate({'opacity': 0}, 500);
    });
    

    编辑:

    仅供参考,您可以更改选择器以立即获得您想要的内容,而无需调用 .filter() 等。

    var gallery = $('ul#gallery li:even:not(:last)');
    
    gallery.hover(...
    

    编辑:

    更改了第一句中的措辞并添加了文档链接。

    If you have jQueryUI installed, you can fade animate a class by adding a duration.

    $(this).toggleClass('next', 500);
    

    http://jqueryui.com/demos/toggleClass/

    But as far as I know, there is no separate opacity setting that affects only the background image. So if you want to fade that in, you would need to fade the entire element, which, as you stated, is not what you want.

    If you really want the effect, an alternative may be to prepend a separate element to the one you were giving the class, and fade in that element (with its background image).

    The element would need to have absolute positioning so that it doesn't affect the rest of the content.

    You would end up with something like:

    CSS:

    li {
        position: relative;
    }
    
    .background {
        width: 100%;
        height: 100%;
        background:orange;  // This would be your background image instead
        position: absolute;
        top: 0;
        left: 0;
    }
    
    .content {
        position: relative;
    }
    

    HTML:

    <ul id='gallery'>
            <li>
                <div class='background'></div>  // Prepend and fade in
                <div class='content'>hi there</div>
            </li>
    </ul>
    

    jQuery:

    hover() takes two functions. One when you mouseenter, the other when you mouseleave.

    // Set opacity to 0 for all .background elements
    $('.background').css({opacity: 0});

    var gallery = $('ul#gallery').children();
    
    gallery.filter(':even').not(':last').hover(
      function () {
           $(this).find('.background').animate({'opacity': 1}, 500);
      },
      function () {
           $(this).find('.background').animate({'opacity': 0}, 500);
    });
    

    EDIT:

    FYI, you can change your selector to get what you want right away without having to call .filter(), and so on.

    var gallery = $('ul#gallery li:even:not(:last)');
    
    gallery.hover(...
    

    EDIT:

    Changed wording in first sentence and added link to docs.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文