开发者

need help understanding this code in book "jquery in action"

This code in the book jQuery in action, on page 156.

I don't understan开发者_开发百科d this part:

{opacity:'toggle'}

Can toggle be an opacity value?

$.fn.fadeToggle = function(speed){
return this.animate({opacity:'toggle'},speed);
};


From the documentation:

In addition to numeric values, each property can take the strings 'show', 'hide', and 'toggle'. These shortcuts allow for custom hiding and showing animations that take into account the display type of the element.

Using toggle will animate the opacity of the element at the speed you specify -- hiding it if it's visible, showing it if it's hidden.


The toggle is a short-cut string that toggles between the show/hide state of the element.

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

In addition to numeric values, each property can take the strings 'show', 'hide', and 'toggle'. These shortcuts allow for custom hiding and showing animations that take into account the display type of the element.


jQuery doc:

In addition to numeric values, each property can take the strings 'show', 'hide', and 'toggle'. These shortcuts allow for custom hiding and showing animations that take into account the display type of the element." Source

So yes, it can be a value.


Toggle, toggles between show and hide. From the same book:

In addition to specific values, we can also specify one of the strings hide, show, or toggle; jQuery will compute the end value as appropriate to the specification of the string. Using hide for the opacity property, for example, will result in the opacity of an element being reduced to 0. Using any of these special strings has the added effect of automatically revealing or removing the element from the display (like the hide() and show() commands). Did you notice when we introduced the core animations that there was no toggling command for the fade effects? That’s easily solved using animate() and toggle as follows


To help you maybe get around the English problem, here is a page that has a couple of demos right next to the code. Click once and it fades or slides. Click again and it returns to its original state.


If it's opaque it becomes transparent. If it's transparent it becomes opaque.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜