亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery循環(huán)動(dòng)畫與獲取組件尺寸的方法

 更新時(shí)間:2015年02月02日 11:57:33   作者:yongh701  
這篇文章主要介紹了jQuery循環(huán)動(dòng)畫與獲取組件尺寸的方法,實(shí)例分析了animate用法及組件的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了jQuery循環(huán)動(dòng)畫與獲取組件尺寸的方法。分享給大家供大家參考。具體分析如下:

一、前言

1、jQuery中的animate()方法允許您創(chuàng)建自定義的動(dòng)畫。

animate() 方法幾乎可以操作所有 CSS 屬性,不過當(dāng)使用 animate() 時(shí),必須使用Camel標(biāo)記法書寫所有的屬性名,比如,必須使用paddingLeft而不是padding-left,使用marginRight而不是margin-right,等等。同時(shí),色彩動(dòng)畫并不包含在核心 jQuery 庫中。如果需要生成顏色動(dòng)畫,您需要從 jquery.com 下載 Color Animations 插件。

2、通過jQuery,很容易處理元素和瀏覽器窗口的尺寸。
jQuery提供如下屬性獲取元素和瀏覽器窗口的尺寸。

二、基本目標(biāo)

如下圖:

在網(wǎng)頁中創(chuàng)建兩個(gè)按鈕,一個(gè)按鈕能夠使組件的尺寸在顯示與隱藏狀態(tài)中切換,一個(gè)按鈕能夠使循環(huán)動(dòng)畫在開始與停止?fàn)顟B(tài)中切換

單純的JQ沒有暫停與開始動(dòng)畫播放的功能,必須下載jQuery Pause插件完成。本例而僅僅通過JavaScript去控制循環(huán)動(dòng)畫,所以每次暫停僅能在其完成一次循環(huán)體才能夠打斷,并不能做到在隨意位置暫停開始的功能。

三、制作過程

以下是網(wǎng)頁所有代碼,之后再一部分一部分地解釋:

復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <title>JQ動(dòng)畫</title> 
        <script type="text/javascript" src="js/jquery-1.11.1.js"></script> 
        <script> 
var interval; 
var i = 0; 
var j = 0; 
function divanimate() { 
    $(".d_class").animate( {left : "+=100px"}, 500); 
    $(".d_class").animate( {top : "+=100px" }, 500); 
    $(".d_class").animate( {left : "-=100px"}, 500); 
    $(".d_class").animate( {top : "-=100px" }, 500); 

function cycle() { 
    divanimate(); 
    interval = setInterval("divanimate()", 2000); 

$(document).ready(function() { 
    $("#stop").click(function() { 
        i++; 
        if (i % 2 != 0) 
            cycle(); 
        else 
            clearInterval(interval); 
    }); 
    $("#show").click(function() { 
        j++; 
        if (j % 2 != 0) { 
            var txt = ""; 
            txt += "<p align=\"center\">高: " + $("#d_id").height() + "px</br>"; 
            txt += "寬: " + $("#d_id").width() + "px</p>"; 
            $("#d_id").html(txt); 
        } else { 
            var txt = ""; 
            $("#d_id").html(txt); 
        } 
    }); 
}) 
    </script> 
    </head> 
 
    <body> 
        <button id="show"> 
            顯示/隱藏方塊尺寸 
        </button> 
        <button id="stop"> 
            開始/停止動(dòng)畫的循環(huán) 
        </button> 
        <div id="d_id" class="d_class" 
            style="width: 100px; height: 100px; background-color: #000; position: absolute; top: 50px; color: #FFF; left:50px;"></div> 
    </body> 
</html>

1、<body>部分
沒有什么特別的,就是定義兩個(gè)按鈕在與一個(gè)圖層。值得注意的是,在圖層的style參數(shù)值中必須加入position:absolute一項(xiàng),否則此圖層無法在網(wǎng)頁中隨意移動(dòng)

background-color是圖層的背景顏色。color是圖層中的字體顏色。

需要定義id與class兩個(gè)參數(shù),因?yàn)镴Q動(dòng)畫需要通過class來控制,而JQ獲取組件尺寸則需要通過id來控制。

同時(shí),擺放圖層的位置需要注意,是用left與top來放置,而不是margin-left與margin-top去放置,因?yàn)镴Q動(dòng)畫控制代碼是用left與top去控制的。如果使用margin-left與margin-top去放置在動(dòng)畫開始的瞬間會(huì)有小幅度的失真。

2、<head>部分

也就是核心代碼部分:

復(fù)制代碼 代碼如下:
<head> 
    <!--網(wǎng)頁編碼,標(biāo)題,使用JQ--> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <title>JQ動(dòng)畫</title> 
        <script type="text/javascript" src="js/jquery-1.11.1.js"></script> 
        <script> 
/*這相當(dāng)于記錄循環(huán)狀態(tài)的指針,為下面清理clearInterval()所用*/         
var interval; 
/*i是用來記錄點(diǎn)擊“開始/停止動(dòng)畫的循環(huán)”按鈕的次數(shù),如果此按鈕點(diǎn)擊奇數(shù)次就開始循環(huán),偶數(shù)次就終止循環(huán)*/ 
var i = 0; 
/*j是用來記錄點(diǎn)擊“顯示/隱藏方塊尺寸”按鈕的次數(shù),如果此按鈕點(diǎn)擊奇數(shù)次就顯示尺寸,偶數(shù)次就隱藏尺寸*/ 
var j = 0; 
/*由于沒有封裝好的toggle()方法,我們需要這么做*/ 
function divanimate() { 
    /*這里只能通過圖層的class值來控制圖層,獲取class值的前面的符號(hào)是.,而不是#*/ 
    <!--要求class為d_class的圖層,先向右偏移100px,再向下偏移100px,之后向左偏移100px,最后向上偏移100px回到原位,每個(gè)動(dòng)作500毫秒以內(nèi)完成,也就是0.5秒--> 
    <!--這里注意的是left的意義是x軸的正方向,屏幕最左為0px,越往右數(shù)值越大,top是y軸的負(fù)方向,屏幕最上為0px,越往下數(shù)值越大--> 
    $(".d_class").animate( {left : "+=100px"}, 500); 
    $(".d_class").animate( {top : "+=100px" }, 500); 
    $(".d_class").animate( {left : "-=100px"}, 500); 
    $(".d_class").animate( {top : "-=100px" }, 500); 

function cycle() { 
    /*此函數(shù)是首先執(zhí)行divanimate()函數(shù),之后,每2000毫秒,也就是每2秒執(zhí)行divanimate()一次。這個(gè)間隔剛好是一次動(dòng)畫所完成時(shí)間,如果不打斷此循環(huán)的話,剛好能夠無間隔地?zé)o縫完美執(zhí)行循環(huán)動(dòng)畫。*/ 
    /*第一行必須存在,否則每次點(diǎn)擊開始按鈕,都要等2秒,才開始動(dòng)畫*/ 
    divanimate(); 
    interval = setInterval("divanimate()", 2000); 

/*$(document).ready(function(){})意為一加載網(wǎng)頁就加載的函數(shù),所有按鈕的動(dòng)作類,必須放在其中*/ 
$(document).ready(function() { 
    $("#stop").click(function() { 
        i++; 
        if (i % 2 != 0) 
            cycle(); 
        else 
        /*終止循環(huán)與上面的開始循環(huán)是javascript的函數(shù),都說了jquery僅僅是javascript的擴(kuò)展。*/ 
            clearInterval(interval); 
    }); 
    $("#show").click(function() { 
        j++; 
        if (j % 2 != 0) { 
            var txt = ""; 
            txt += "<p align=\"center\">高: " + $("#d_id").height() + "px</br>"; 
            txt += "寬: " + $("#d_id").width() + "px</p>"; 
            /*這個(gè)方法能夠在相應(yīng)的組件標(biāo)簽中輸出文本*/ 
            $("#d_id").html(txt); 
        } else { 
            var txt = ""; 
            $("#d_id").html(txt); 
        } 
    }); 
}) 
</script> 
</head>

希望本文所述對大家的jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論