简体   繁体   English

动画不透明度隐藏/显示

[英]Animate opacity hide / show

so, what is the different between: 因此,有什么区别:

A) (work for me) A) (为我工作)

... .animate({opacity: "show", marginTop: "25"}); ...

... ...

B (doesn´t work for me) B (对我不工作)

... .animate({opacity: "1", marginTop: "25"}); ...

eg http://jsbin.com/iquwuc/6/edit#preview 例如http://jsbin.com/iquwuc/6/edit#preview

When you call hide() that's roughly equivalent to .css('display', 'none') , so later changing opacity back to 1 changes the opacity of a hidden element. 当您调用hide() ,它大致等效于.css('display', 'none') ,因此稍后将不透明度改回1会更改隐藏元素的不透明度。 And that's why show() works - because it makes the element block again. 这就是show()起作用的原因-因为它再次使元素阻塞。

It is because you are show and hiding, instead of animating the opacity. 这是因为您处于显示和隐藏状态,而不是为不透明度设置动画。 (Kinda obvious :P ). (金田明显:P)。

Edited code : http://jsbin.com/iquwuc/11/edit#preview 修改后的代码: http : //jsbin.com/iquwuc/11/edit#preview

You can make the following amendments to use the opacity setting: 您可以进行以下修改以使用不透明度设置:

Add the following css: 添加以下css:

.sub-menu li#access ul {opacity:0; display:none;} 

And change your script to this: 并将脚本更改为此:

$(document).ready(function(){

    $('.sub-menu').hover(function(){ 
          $('.sub-menu li#access ul').show();
        $('.sub-menu li#access ul').stop().animate({opacity: 1, marginTop: "25"}, 500);
        },
        function() {
                  $('.sub-menu li#access ul').stop().animate({opacity: 0, marginTop: "10"}, 500,function(){
                  $('.sub-menu li#access ul').hide();
                  });   
        });
});

Basically what is happening is: 基本上发生的是:

On hover, you are SHOW'ing the dropdown with opacity 0, then animation happens to set margin and opacity. 悬停时,您将不透明度0显示在下拉菜单中,然后动画会设置边距和不透明度。 and on hover-out, animating opacity to 0 and HIDE'ing it again. 然后将鼠标悬停,将不透明度设置为0,然后再次将其隐藏。

in the css file or inline. 在css文件或内联中。 Set the id or class to 将ID或类设置为

inline - <div id="myid" style="opacity:0;"></div> 内联- <div id="myid" style="opacity:0;"></div>

in css 在CSS

        #myid { opacity: 0; }
        .myclass {opacity: 0; }

that way when calling the animate opacity from jQuery it will function other wise your just calling an animation that is already at 1 opacity 这样,当从jQuery调用动画不透明度时,它将以其他方式起作用,您只需要调用已经具有1不透明度的动画即可

I would use dojo bibliothek for it ( http://dojotoolkit.org/reference-guide/dojo/animateProperty.html ). 我会为此使用dojo书目( http://dojotoolkit.org/reference-guide/dojo/animateProperty.html )。 You will find in DOJO more than only animating functionality, this framework offers a lot of components to solve big area of different problems. 在DOJO中,您不仅会发现动画功能,而且该框架提供了许多组件来解决各种不同的问题。

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

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