学习JQuery - 13

来源:互联网 发布:上海linux培训 编辑:程序博客网 时间:2024/06/04 20:14

    4. 与动画等效的效果队列

        我们上一节已经使用了animate()方法。

        下面看一个示例:

        js代码

    $('div.label').click(function(e) {        var paraWidth = $('div.speech p').outerWidth();var $switcher = $(this).parent();var switcherWidth = $switcher.outerWidth();$switcher.css({postion: 'relative'}).animate({left: paraWidth - switcherWidth}, 'slow').animate({height: '+=20px'}, 'slow').animate({borderWidth: '5px'}, 'slow');    });
      实现的效果如下:

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.


        当然我们也可以这样写:

.animate({left: paraWidth - switcherWidth, height: '+=20px', borderWidth: '5px'}, 'slow');
        我们在执行上述代码时,会发现卡顿现象,能不能用什么方法处理他呢?

        这个时候,我们就要使用“效果链”(一系列的效果方法)。

       其中有一个方法我们没有介绍过:fadeTo()

       duration: 反应时间(ms);

                        slow: 600ms、fast: 200ms、default: 400ms。

       opacity: 模糊度 数值0.0-1.0

       js代码

.fadeTo('fast', 0.5).animate({left: paraWidth - switcherWidth, height: '+=20px'}, 'slow').fadeTo('slow', 1.0).slideUp('slow').animate({borderWidth: '5px'}, 50).slideDown('slow');

        我们希望两个“animate”变更为一个,有没有方法实现他呢?

        这就要用到队列方法了!

        js代码

.fadeTo('fast', 0.5).animate({left: paraWidth - switcherWidth, height: '+=20px'}, {duration: 'slow', querue: false}).fadeTo('slow', 1.0).slideUp('slow').queue(function(next) {$switcher.css({borderWidth: '5px'});next();}).slideDown('slow');
      
附:

    chapter4-2.html(详见《学习JQuery - 11》)


    chapter04.css(详见《学习JQuery - 11》)


    chapter04-4.js

$(document).ready(function() {    $('div.label').click(function(e) {        var paraWidth = $('div.speech p').outerWidth();var $switcher = $(this).parent();var switcherWidth = $switcher.outerWidth();$switcher.css({postion: 'relative'}).fadeTo('fast', 0.5).animate({left: paraWidth - switcherWidth, height: '+=20px'}, {duration: 'slow', querue: false}).fadeTo('slow', 1.0).slideUp('slow').queue(function(next) {$switcher.css({borderWidth: '5px'});next();}).slideDown('slow');    });});

0 0
原创粉丝点击