jQuery碎語(3) 動畫特效

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   ar   2014   

5、動畫特效

● 自製摺疊內容塊

內容塊如下

    <div class="module">
        <div class="caption">
            <span>標題</span>
            <img src="rollup.gif" alt="rollup" title="rolls up this module"/>
        </div>
        <div class="body">
            近日,《體壇周報》記者馬德興在接受天津體育頻道《體壇新視野》節目採訪時表示自己對恒大[微博]的情況比較擔憂,恒大統治力比上賽季下降了很多,恒大外援存在位置重疊的問題,客場不輸給西雪梨流浪者就是一個可以接受的結果。該節目稱恒大聯賽3連勝勝之不武,恒大的惹不起不過爾爾,恒大失去了對其它球隊壓倒性的優勢,能力下降是恒大霸主地位有所動搖的根源所在。
        </div>
    </div>

 

給img元素繫結點擊事件。

        $(function() {
            $(‘div.caption img‘).click(function () {
                //先找到img的父級元素,再找該父級元素的子項目
                var $body = $(this).closest(‘div.module‘).find(‘div.body‘);
                if ($body.is(‘:hidden‘)) {
                    $body.show();
                } else {
                    $body.hide();
                }
            });
        });

 

切換元素的顯示狀態,還可以用toggle方法。

        $(function() {
            $(‘div.caption img‘).click(function () {
                $(this).closest(‘div.module‘).find(‘div.body‘).toggle();
            });
        });

 

以上是沒有動畫效果的,有時候感覺會很唐突。實際上,show,hide,toggle方法都可以有動畫效果。比如:

        $(function() {
            $(‘div.caption img‘).click(function () {
                $(this).closest(‘div.module‘).find(‘div.body‘).toggle(‘slow‘);
            });
        });

 

又比如:

        $(function() {
            $(‘div.caption img‘).click(function () {
                $(this).closest(‘div.module‘).find(‘div.body‘).toggle(‘slow‘, function() {
                    $(this).closest(‘div.module‘).toggleClass(‘rolledup‘, $(this).is(‘:hidden‘))
                });
            });
        });    

 

● 使元素淡入淡出
fadeIn(speed, callback)       
fadeOut(speed, callback)
fadeTo(speed, opacity, callback)

 

● 上下滑動元素

slideDown(speed, callback)
slideUp(speed, callback)
slideToggle(speed, callback)

 

● 停止動畫

stop(clearQueue, gotoEnd)

 

● 建立自訂動畫

animate(properties, duration, easing, callback)

$(‘.classname‘).animate({opacity:‘toggle‘},‘slow‘)

 

如果寫一個擴充函數。

$.fn.fadeToggle = function(speed){
    return this.animate({opacity:‘toggle‘},‘slow‘);
}

 

● 自訂縮放動畫

$(‘.classname‘).each(function(){
    $(this).animate({
        width: $(this).width() * 2,
        height: $(this).height() * 2
    });
});

 

● 自訂掉落動畫

$(‘.classname‘).each(function(){
    $(this)
        .css("position","relative")
        .animate({
            opacity: 0,
            top: $(window).height() - $(this).height() - $(this).position().top
        },‘slow‘,function(){ $(this).hide(); })
});

 

● 自訂消散動畫

$(‘.classname‘).each(function(){
    var position = $(this).position();
    $(this)
        .css({
            position: ‘absolute‘,
            top: position.top,
            left:position.left
        })
        .animate({
            opacity: ‘hide‘,
            width: $(this).width()*5,
            height: $(this).height()*5
            top: position.top - ($(this).height() * 5 / 2),
            left: position.left - ($(this).width() * 5 /2)
        },‘normal‘);
});

 

● 隊列中的動畫

//動畫插入隊列
$(‘img‘).queue(‘chain‘, function(){});
$(‘img‘).queue(‘chain‘, function(){});
$(‘img‘).queue(‘chain‘, function(){});
$(‘img‘).queue(‘chain‘, function(){});

$(‘button‘).click(function(){
    $(‘img‘).dequeue(‘chain‘); //刪除隊列中的動畫
})

cleaeQueue(name)//刪除所有未執行的隊列中的動畫
delay(duration, name)//為隊列中所有未執行的動畫添加延遲

 

參考資料:jQuery實戰(第二版)   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.