Chinaunix首页 | 论坛 | 博客
  • 博客访问: 58963
  • 博文数量: 7
  • 博客积分: 188
  • 博客等级: 入伍新兵
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-11 01:05
个人简介

自由软件工程师,come on, 改变世界的力量!

文章分类
文章存档

2013年(5)

2012年(2)

分类: 系统运维

2013-01-13 11:18:36

抓住周末,多学点啊。第四章 样式和动画

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代码为例

  1. <div id="switcher">
  2.     <div class="label">Text Size</div>
  3.     <button id="switcher-default">Default</button>
  4.     <button id="switcher-large">Bigger</button>
  5.     <button id="switcher-small">Smaller</button>
  6. </div>
  7.   
  8. <div class="speech">
  9.     <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>
  10.     <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>
  11.     <a href="#" class="more">read more</a>
  12.     <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>
  13.     <p>It is rather for us to be here dedicated to the great task remaining before us&mdash;that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion&mdash;that we here highly resolve that these dead shall not have died in vain&mdash;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>
  14. </div>
我们单击Biger和Smaller按钮时会增大或减小
字体大小,单击Default时回到原始大小。如果我们只让字体变大一次,我们可以用.addClass()改变一次样式。如果我们想点击按钮时字体不断变大,就需要计算CSS样式了。
最终的JS代码如下:

  1. $(document).ready(function() {
  2.     var $speech = $('div.speech');
  3.     var defaultSize = $speech.css('fontSize');
  4.     
  5.     $('#switcher button').click(function() {
  6.         var num = parseFloat($speech.css('fontSize'));
  7.         switch (this.id) {
  8.         case 'switcher-large':
  9.             num *= 1.4;
  10.             break;
  11.         
  12.         case 'switcher-small':
  13.             num /= 1.4;
  14.             break;
  15.         
  16.         default:
  17.             num = parseFloat(defaultSize);
  18.         }
  19.         
  20.         $speech.css('fontSize', num + 'px');
  21.     });
  22. });
首先我们获得$('div.speech')对象,保存在$speech变量中,这里$可以作为变量名的合法字符。然后利用.css('fontSize')方法获得defaultSize。把三个按键绑定到一个单击事件中。parseFloat()函数将字符串转换为浮点数,因为fontSize的是14px。计算出新的字体大小后,调用$speech.css('fontSize', num + 'px')方法,设置新的字体大小样式。

Basic hide and show
隐藏与显示
我们给个例子展示hide() show()。

  1. $(document).ready(function() {
  2.     $('p').eq(1).hide();
  3.     
  4.     $('a.more').click(function() {
  5.         $('p').eq(1).show();
  6.         $(this).hide();
  7.         return false;
  8.     });
  9. });






阅读(1174) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~