04_样式与动画

4.1 修改内联CSS

既可以取得元素的css内容,也可以设置元素的css内容,

取得使用.css('background-color')

初始效果:

html:

Abraham Lincoln's Gettysburg Address

Abraham Lincoln's Gettysburg Address

Text Size

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.

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.

read more

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.

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.


效果1:

点击Bigger之后,speech内的文字会持续的变大

js:

$(document).ready(function(){var $speech = $('div.speech');$('#switcher-large').click(function() {var num = parseFloat($speech.css('font-size'));num *= 1.4;$speech.css('font-size',num + 'px');});
});

效果2:

点击smaller之后,speech内的文字可以持续的减小

js:

$(document).ready(function() {var $speech = $('div.speech');$('#switcher button').click(function() {var num = parseFloat($speech.css('fontSize'));if (this.id == 'switcher-large') {num *= 1.4;} else if (this.id == 'switcher-small') {num /= 1.4;}$speech.css('fontSize', num + 'px');});
});

效果3:

添加默认字体效果

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');});
});


4.2 基本的隐藏和显示

效果4:

基本的显示和隐藏

js:

  $('p').eq(1).hide();$('a.more').click(function() {$('p').eq(1).show('slow');$(this).hide();return false;});


4.3 效果和速度

4.3.1 指定显示速度

在show()和hide()中可以指定速度,毫秒数

4.3.2 淡入和淡出

fadeIn()和fadeOut()

4.3.3 滑上和滑下

slideDown()和slideUp()

4.3.4 复合效果

使用toggle()


4.4 创建自定义动画

包括两种方式,

第一种:

.animate({property1:'value1',property2:'value2'},speed,easing,function(){alert('finished');}
);

第二种:

.animate({property:'value1',property:'value2'},//-------------{duration : 'value',easing : 'value',specialEasing : {property1:'value1',property2:'value2'},complete : function(){},queue : true,step : callback}
);

4.4.1 手工创建效果

效果5:

将toggle效果用animate取代

js:

  var $firstPara = $('p').eq(1);$firstPara.hide();$('a.more').click(function() {$firstPara.animate({height: 'toggle'}, 'slow');var $link = $(this);if ($link.text() == 'read more') {$link.text('read less');} else {$link.text('read more');}return false;});

4.4.2 一次给多个属性添加效果

效果6:

同时具备滑动和淡入淡出效果

js:

  var $firstPara = $('p').eq(1);$firstPara.hide();$('a.more').click(function() {$firstPara.animate({opacity: 'toggle',height: 'toggle'}, 'slow');var $link = $(this);if ($link.text() == 'read more') {$link.text('read less');} else {$link.text('read more');}return false;});

效果7:

一个综合的效果

js:

  $('div.label').click(function() {var paraWidth = $('div.speech p').outerWidth();var $switcher = $(this).parent();var switcherWidth = $switcher.outerWidth();$switcher.css({position: 'relative'}).animate({borderWidth: '5px',left: paraWidth - switcherWidth,height: '+=20px'}, 'slow');});


4.5 并发与排队效果

4.5.1 处理一组元素

效果8:

一个排队的效果

js:

  $('div.label').click(function() {var paraWidth = $('div.speech p').outerWidth();var $switcher = $(this).parent();var switcherWidth = $switcher.outerWidth();$switcher.css({position: 'relative'}).fadeTo('fast', 0.5).animate({left: paraWidth - switcherWidth}, 'slow').fadeTo('slow', 1.0).slideUp('slow').slideDown('slow');});

效果9:

使用animate第二种方式实现并发效果

js:

  $('div.label').click(function() {var paraWidth = $('div.speech p').outerWidth();var $switcher = $(this).parent();var switcherWidth = $switcher.outerWidth();$switcher.css({position: 'relative'}).fadeTo('fast', 0.5).animate({left: paraWidth - switcherWidth}, {duration: 'slow',queue: false}).fadeTo('slow', 1.0).slideUp('slow').slideDown('slow');});

效果10:

非效果的方法如css()即时排队正确也不会有排队效果,需要使用queue()

js:

  $('div.label').click(function() {var paraWidth = $('div.speech p').outerWidth();var $switcher = $(this).parent();var switcherWidth = $switcher.outerWidth();$switcher.css({position: 'relative'}).fadeTo('fast', 0.5).animate({left: paraWidth - switcherWidth}, {duration: 'slow',queue: false}).fadeTo('slow', 1.0).slideUp('slow').queue(function(next) {$switcher.css({backgroundColor: '#f00'});next();}).slideDown('slow');});

4.5.2 处理多组元素

效果11:

对多个元素使用排队的方式并不会正真排队,而是会同时发生,可以使用下面的方法实现正真的排队

js:

  $('p').eq(2).css('border', '1px solid #333').click(function() {$(this).next().slideDown('slow', function() {$(this).slideUp('slow');});});

4.5.3 简单概括


4.6 小结


4.7 练习


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部