響應式網頁前端設計

第一階段
加強熟識HTML&CSS

第二階段
學習JavaScript&jQuery

第三階段
Bootstrap協助專案設計

第四階段
w3.css協助專案設計

第五階段
自行開發網站專案設計

第六階段
Pug(Jade)&Sass協助開發效率

=== TSuiling write 2018.07 ===

響應式網頁前端設計【第二階段】學習JavaScript&jQuery

jQuery 自定動畫

jQuery 自定動畫的範例

運用範例練習,了解 jQuery 自定動畫

準備 HTML & CSS 部份

<body>
    <div>Ball-1</div>
    <div>Ball-2</div>
</body>
<style>
    div { width:80px; height:80px; border-radius: 50%; position:absolute; left:0;
          display: flex; justify-content: center; align-items: center; } 
    div:nth-of-type(1) { background:rgba(0, 59, 255, 0.5);  top:0px; }
    div:nth-of-type(2) { background:rgba(226, 0, 255, 0.5); top:100px; }
</style>

JavaScript & jQuery 部份---PART1:偵測寬高, 自動移動物件

※ 撰寫 jQuery 前得先載入 jQuery 函式庫主程式

<script>
 
    //偵測視窗的右下角, 讓二個圓球往右下角移動
    var targetLeft = $(document).width()-$('div').width();   //偵測視窗寬度-div的寬度=往右移動的目標位置
    var targetTop = $(document).height()-$('div').height();  //偵測視窗高度-div的高度=往下移動的目標位置

    $('div:eq(0)').animate({left:targetLeft},1000)
                  .animate({top:targetTop},1000);
    $('div:eq(1)').animate({left:targetLeft,top:targetTop},2000);
    
</script>

JavaScript & jQuery 部份---PART2:自定動畫增加加減速變化

※ 增加加減速變化前得先載入 jquery.easing 外掛程式

<script>
 
    //偵測視窗的右下角, 讓二個圓球往右下角移動
    var targetLeft = $(document).width()-$('div').width();   //偵測視窗寬度-div的寬度=往右移動的目標位置
    var targetTop = $(document).height()-$('div').height();  //偵測視窗高度-div的高度=往下移動的目標位置

    $('div:eq(0)').animate({left:targetLeft},1000,'easeOutBack')
                  .animate({top:targetTop},1000,'easeOutBack');
    $('div:eq(1)').animate({left:targetLeft,top:targetTop},2000,'easeOutCirc');
    
</script>

JavaScript & jQuery 部份---PART3:控制第2個球動畫結束才移動第1個球

<script>
 
    //偵測視窗的右下角, 讓二個圓球往右下角移動
    var targetLeft = $(document).width()-$('div').width();   //偵測視窗寬度-div的寬度=往右移動的目標位置
    var targetTop = $(document).height()-$('div').height();  //偵測視窗高度-div的高度=往下移動的目標位置
                  
    $('div:eq(1)').animate({left:targetLeft,top:targetTop},2000,'easeOutCirc',function(){
    
                      $('div:eq(0)').animate({left:targetLeft},1000,'easeOutBack')
                                    .animate({top:targetTop},1000,'easeOutBack');
                  
                   });
    
</script>