抓住周末,多学点啊。第四章 样式和动画
Inline CSS modificatin
内联CSS修改
上一个章节中,我们预先定义了CSS样式,然后利用jQuery将这些样式插入到HTML中,这样是推荐的方式。然而有些时候,我们不能预先定义出CSS样式,需要JS计算出来CSS样式,jQuery这时候提供了.css()方法。
获得样式表属性的的值,.css('属性名'),比如.css('backgroundColor').
设置样式表的只值,.css('属性名', 'value'), 或者设置一组属性:
// Map of property-value pairs
.css({
property1: 'value1',
'property-2': 'value2'
})
我们以下面的html代码为例
- <div id="switcher">
- <div class="label">Text Size</div>
- <button id="switcher-default">Default</button>
- <button id="switcher-large">Bigger</button>
- <button id="switcher-small">Smaller</button>
- </div>
-
- <div class="speech">
- <p>Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.</p>
- <p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field as a final resting-place for those who here gave their lives that the nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow, this ground.</p>
- <a href="#" class="more">read more</a>
- <p>The brave men, living and dead, who struggled here have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember, what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.</p>
- <p>It is rather for us to be here dedicated to the great task remaining before us—that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion—that we here highly resolve that these dead shall not have died in vain—that this nation, under God, shall have a new birth of freedom and that government of the people, by the people, for the people, shall not perish from the earth.</p>
- </div>
我们单击Biger和Smaller按钮时会增大或减小
字体大小,单击Default时回到原始大小。如果我们只让字体变大一次,我们可以用.addClass()改变一次样式。如果我们想点击按钮时字体不断变大,就需要计算CSS样式了。
最终的JS代码如下:
- $(document).ready(function() {
- var $speech = $('div.speech');
- var defaultSize = $speech.css('fontSize');
-
- $('#switcher button').click(function() {
- var num = parseFloat($speech.css('fontSize'));
- switch (this.id) {
- case 'switcher-large':
- num *= 1.4;
- break;
-
- case 'switcher-small':
- num /= 1.4;
- break;
-
- default:
- num = parseFloat(defaultSize);
- }
-
- $speech.css('fontSize', num + 'px');
- });
- });
首先我们获得$('div.speech')对象,保存在$speech变量中,这里$可以作为变量名的合法字符。然后利用.css('fontSize')方法获得defaultSize。把三个按键绑定到一个单击事件中。parseFloat()函数将字符串转换为浮点数,因为fontSize的是14px。计算出新的字体大小后,调用$speech.css('fontSize', num + 'px')方法,设置新的字体大小样式。
Basic hide and show
隐藏与显示
我们给个例子展示hide() show()。
- $(document).ready(function() {
- $('p').eq(1).hide();
-
- $('a.more').click(function() {
- $('p').eq(1).show();
- $(this).hide();
- return false;
- });
- });
Effects and speed
效果和速度
当我们考虑给show() hide()方法指定持续时间时,就变成动画了。
Speeding in
.show('slow') .show('normal') .show('fast'), .show(850) 单位ms
Fading in and fading out
.fadeIn('slow') .fadeIn('normal') .fadeIn('fast')
Sliding up and sliding down
渐进渐出时,段落下面的内容一下子变换位置,用slideDown()则下面的内容是滑动。
Creating custom animations
自定义动画
暂不深入了,以后用到再回来看,继续第5章 操作DOM 第6章Ajax
阅读(1174) | 评论(0) | 转发(0) |