学习JQuery - 12

来源:互联网 发布:淘宝客资源怎么找 编辑:程序博客网 时间:2024/04/29 01:29

    3. 建立自定义的动画

       我们把“显示/隐藏”按照时序排列就是动画了!

       jQuery提供了一个强大的方法:animate()

      我们今天主要讲使用2个参数时实现的效果。

.animate({properties},duration);
      在使用前,我们来重温一下:box-model



        因为在.animate()的properties中有许多关于他们的设置

1)backgroundPosition2)borderLeft3)borderLeftWidth4)borderRight5)borderRightWidth6)borderSpacing7)borderTop8)borderTopWidth9)borderWidth10)bottom11)fontSize12)left13)marginBottom14)marginLeft15)marginRight16)marginTop17)paddingBottom18)paddingLeft19)paddingRight20)paddingTop21)position22)right23)top

        还有三个经常使用的:height、width、opacity;这三个属性大部分使用“toggle”值以达到动画效果。

        第二个参数是期间设定(在多长的时间内完成),单位为毫秒(ms)。


        当我们隐藏内容时,显示“read more”链接;

        但我们显示内容时,显示“read less”链接;

        js代码

    var $firstPara = $('p').eq(1);$firstPara.hide();$('a.more').click(function(e) {        e.preventDefault();$firstPara.animate({height: 'toggle'}, 500);var $link = $(this);if ($link.text() == 'read more') {$link.text('read less');}else {$link.text('read more');}    });
        点击“read more”(显示第一行时的图片)

        完成时的效果

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 less

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.

        点击“read less”(未隐藏第一行时的图片)


        完成时的效果

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.

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.


        我们增加“模糊”和“字体”属性

        js代码

$firstPara.animate({height: 'toggle', opacity: 'toggle', fontSize: '16px'}, 500);
       效果1(点击“read more”):

       P-3 显示第二行时

         效果2(点击“read less”):


P-4 未隐藏第二行


附:

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


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


    chpater04-3.js

$(document).ready(function() {    var $firstPara = $('p').eq(1);$firstPara.hide();$('a.more').click(function(e) {        e.preventDefault();$firstPara.animate({height: 'toggle', opacity: 'toggle', fontSize: '16px'}, 2000);var $link = $(this);if ($link.text() == 'read more') {$link.text('read less');}else {$link.text('read more');}    });});


0 0