jQuery:从当前不透明度淡出

发布于 2024-10-02 22:29:48 字数 404 浏览 4 评论 0原文

我在 mouseOver 上将对象从 0% 淡入到 100%,并在 mouseOut 上淡出到 0%。

当我快速执行 mouseInmouseOut 操作时,效果会“跳跃”,因为 mouseOut 从 100% 淡出 - 而且因为我执行了快速操作mouseInmouseOut,淡入不会一直淡出到 100%。也许它只会褪到 25% 或 10%。

我的问题是: 如何使淡出仅从特定百分比淡出?

如果 fadeIn 仅达到 20,则 fadeOut 应从 20 fadeOut

I'm fading an object from 0% to 100% on mouseOver, and fade back out to 0% on mouseOut.

When I do a quick mouseIn and mouseOut, the effect "jumps" because mouseOut is fading out from 100% - and because I do a quick mouseIn and mouseOut, the fade in doesn't fade all the way to 100%. Maybe it only fades to 25% or 10%.

My question is:
How can I make the fadeout only fading from the specific percentage?

If the fadeIn only gets to 20, the fadeOut should fadeOut from 20.

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

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

发布评论

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

评论(4

失而复得 2024-10-09 22:29:48

你可以尝试这样做:

$('#element').animate({opacity:0});

...而不是 fadeOut()。

You could try doing:

$('#element').animate({opacity:0});

...instead of fadeOut().

不离久伴 2024-10-09 22:29:48

我不确定您当前的代码是什么样的,但您需要使用 jQuery .animate() 函数:

http://api.jquery.com/animate/

这是一个示例:

$('#object').mouseover(function() {
  $(this).animate({
    opacity: 1,
  }, 1000, function() {
    //completion code?
  });
});

$('#object').mouseout(function() {
  $(this).animate({
    opacity: 0,
  }, 1000, function() {
    //completion code??
  });
});

I'm not sure what your current code looks like, but you'll want to use the jQuery .animate() function:

http://api.jquery.com/animate/

Here is an example:

$('#object').mouseover(function() {
  $(this).animate({
    opacity: 1,
  }, 1000, function() {
    //completion code?
  });
});

$('#object').mouseout(function() {
  $(this).animate({
    opacity: 0,
  }, 1000, function() {
    //completion code??
  });
});
翻了热茶 2024-10-09 22:29:48

使用 jQuery 的 .fadeTo() 方法 可以设置目标不透明度。

$('selector').fadeTo('slow', 1);
$('selector').fadeTo('slow', 0);

第一个参数是速度,第二个参数是不透明度。


如果您使用 .hover() 您可以这样做:

示例: http://jsfiddle.net/ecUdS/

$('selector').hover(function( evt ) {
    $(this).stop().fadeTo('slow', evt.type === 'mouseenter' );
});

这个 使用 .stop( ) 停止动画(如果正在运行)。

然后 因为 .hover() 将为 mouseenter 触发mouseleave,我添加了一个测试,如果它是 mouseenter,它将发送 true (这相当于 >1)。否则,它发送 false0

Use jQuery's .fadeTo() method which lets you set a target opacity.

$('selector').fadeTo('slow', 1);
$('selector').fadeTo('slow', 0);

The first argument is the speed, the second is the opacity.


If you're using .hover() you could do it like this:

Example: http://jsfiddle.net/ecUdS/

$('selector').hover(function( evt ) {
    $(this).stop().fadeTo('slow', evt.type === 'mouseenter' );
});

This uses .stop() to stop the animation if it is running.

Then because .hover() will fire for both the mouseenter and mouseleave, I added a test where if it is a mouseenter, it will send true (which will equate to 1). Otherwise it sends false or 0.

半夏半凉 2024-10-09 22:29:48

我曾经也遇到过同样的问题。你不能真正使用 jQuery 的 animate 函数,因为它总是想要完成动画。所以我为它编写了自己的函数..希望这会有所帮助(我也没想到有一天会分享这个,所以它的编写是为了适合我的使用方式):

//Custom fade effects.
//Similar to jQuery's fadeIn & fadeOut
//Except that it doesn't queue animations, and can cut the animation short at anytime
//Highly useful for quickly hovering over elements and desiring the fade effects to be cut on hover in/out.

function fade(elem, interval) 
{
    if(!(elem instanceof $)) {
        elem = $(elem);
    }

    if(elem.is(':not(:visible)')) {
        elem.css('opacity', '0').show();
    }

    elem.css('opacity', function() { 
            var current = $(this).data('fadeLevel') || 0;

            //normalize - accounts for tiny descrepancies in parsing
            if(current < 0) { current = 0; } 
            if(current > 1) { current = 1; } 

            $(this).data('fadeLevel', current + interval)

            return $(this).data('fadeLevel');
        });

    if(elem.data('fadeLevel') < 0 || elem.data('fadeLevel') > 1 ) {
        clearTimeout(elem.data('fadeTimer'));
    }
}

function fadeIn(elem) { fadeTo(elem, 0.04, 0); }
function fadeOut(elem) { fadeTo(elem, -0.04, 1); }
function fadeTo(elem, interval, level) {
    if(!$(elem).data('itemOpen')) {
        clearTimeout($(elem).data('fadeTimer'));
        $(elem).data('fadeLevel', level).data('fadeTimer', setInterval(function() { fade(elem, interval) }, 30));
    }
}

示例

http://jsfiddle.net/3AxHb/

I had this same issue once upon a time. You can't really use jQuery's animate functions because it always wants to complete the animation. So I wrote my own function for it.. hope this helps (also I wasn't expecting to share this someday, so it has been written to be suited to how I was using it):

//Custom fade effects.
//Similar to jQuery's fadeIn & fadeOut
//Except that it doesn't queue animations, and can cut the animation short at anytime
//Highly useful for quickly hovering over elements and desiring the fade effects to be cut on hover in/out.

function fade(elem, interval) 
{
    if(!(elem instanceof $)) {
        elem = $(elem);
    }

    if(elem.is(':not(:visible)')) {
        elem.css('opacity', '0').show();
    }

    elem.css('opacity', function() { 
            var current = $(this).data('fadeLevel') || 0;

            //normalize - accounts for tiny descrepancies in parsing
            if(current < 0) { current = 0; } 
            if(current > 1) { current = 1; } 

            $(this).data('fadeLevel', current + interval)

            return $(this).data('fadeLevel');
        });

    if(elem.data('fadeLevel') < 0 || elem.data('fadeLevel') > 1 ) {
        clearTimeout(elem.data('fadeTimer'));
    }
}

function fadeIn(elem) { fadeTo(elem, 0.04, 0); }
function fadeOut(elem) { fadeTo(elem, -0.04, 1); }
function fadeTo(elem, interval, level) {
    if(!$(elem).data('itemOpen')) {
        clearTimeout($(elem).data('fadeTimer'));
        $(elem).data('fadeLevel', level).data('fadeTimer', setInterval(function() { fade(elem, interval) }, 30));
    }
}

Examples

http://jsfiddle.net/3AxHb/

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