Ô˶¯¿ò¼ÜµÄʵÏÖ˼·
Ô˶¯£¬Æäʵ¾ÍÊÇÔÚÒ»¶Îʱ¼äÄڸıäleft¡¢right¡¢width¡¢height¡¢opactiyµÄÖµ£¬µ½´ïÄ¿µÄµØÖ®ºóÍ£Ö¹¡£
ÏÖÔÚ°´ÕÕÒÔϲ½ÖèÀ´½øÐÐÎÒÃǵÄÔ˶¯¿ò¼ÜµÄ·â×°:
ÔÈËÙÔ˶¯¡£
»º³åÔ˶¯¡£
¶àÎïÌåÔ˶¯¡£
ÈÎÒâÖµ±ä»¯¡£
Á´Ê½Ô˶¯¡£
ͬʱÔ˶¯¡£
£¨Ò»£©ÔÈËÙÔ˶¯
Ëٶȶ¯»
Ô˶¯»ù´¡
˼¿¼£ºÈçºÎÈÃdiv¶¯ÆðÀ´?
ÈçÏ£º
ÉèÖÃÔªËØΪ¾ø¶Ô¶¨Î»£¬Ö»Óоø¶Ô¶¨Î»ºó£¬left,topµÈÖµ²ÅÉúЧ¡£
¶¨Ê±Æ÷µÄʹÓ㨶¯Ì¬¸Ä±äÖµ£©£¬ÕâÀïʹÓÃsetInterval()ÿ¸ôÖ¸¶¨µÄʱ¼äÖ´ÐдúÂë¡£
¼ÆʱÆ÷setInterval(º¯Êý,½»»¥Ê±¼ä(ºÁÃë))£ºÔÚÖ´ÐÐʱ,´ÓÔØÈëÒ³Ãæºóÿ¸ôÖ¸¶¨µÄʱ¼äÖ´ÐдúÂë¡£
È¡Ïû¼ÆʱÆ÷clearInterval(º¯Êý) ·½·¨¿ÉÈ¡ÏûÓÉ setInterval() ÉèÖõĽ»»¥Ê±¼ä¡£
»ñÈ¡µ±Ç°µÄλÖ㬴óСµÈµÈ¡£offsetLeft£¨µ±Ç°ÔªËØÏà¶Ô¸¸ÔªËØλÖã©¡£
ËÙ¶È--ÎïÌåÔ˶¯µÄ¿ìÂý
¶¨Ê±Æ÷¼ä¸ôʱ¼ä
¸Ä±äÖµµÄ´óС
¸ù¾ÝÉÏÃæµÄÐÅÏ¢ÎÒÃǾͿÉÒÔ¿ªÊ¼·â×°Ô˶¯¿ò¼Ü´´½¨Ò»¸ö±ä»¯µÄdivÁË¡£
- /**
- * Ô˶¯¿ò¼Ü-1-¶¯ÆðÀ´
- * @param {HTMLElement} element ½øÐÐÔ˶¯µÄ½Úµã
- */
- var timer = null;
- function startMove(element) {
- timer = setInterval(function () {//¶¨Ê±Æ÷
- element.style.left = element.offsetLeft + 5 + "px";
- }, 30);
- }
Äãû¿´´í£¬¾ÍÊÇÄÇô¼òµ¥¡£µ«Êǵȵȣ¬ what£¿ Ôõô²»»áÍ££¿WTF£¿
ÄÇÊÇÒòΪÎÒÃÇûÓÐÔ˶¯ÖÕÖ¹Ìõ¼þ¡£ºÃÔÙ»¹ÊDZȽϼòµ¥¡£Ö±½ÓÔÚ¶¨Ê±Æ÷ÄÚ²¿£¬Åжϵ½´ïÄ¿±êÖµ£¬Çå³ý¶¨Ê±Æ÷¾ÍÐÐÀ£¡
- /**
- * Ô˶¯¿ò¼Ü-2-Ô˶¯ÖÕÖ¹
- * @param {HTMLElement} element ½øÐÐÔ˶¯µÄ½Úµã
- * @param {number} iTarget Ô˶¯ÖÕÖ¹Ìõ¼þ¡£
- */
- var timer = null;
- function startMove(element, iTarget) {
- timer = setInterval(function () {
- element.style.left = element.offsetLeft + 5 + "px";
- if (element.offsetLeft === iTarget) {//Í£Ö¹Ìõ¼þ
- clearInterval(timer);
- }
- }, 30);
- }
¾ÍÕâÑùÊDz»ÊǾÍÍê³ÉÁËÄØ£¿ÒѾokÁËÄØ£¿
no¡£»¹ÓÐһЩBugÐèÒª´¦Àí¡£
Ô˶¯ÖеÄBug
ËÙ¶ÈÈ¡µ½Ä³Ð©Öµ»áÎÞ·¨Í£Ö¹
µ½´ïλÖúóÔÙµã»÷»¹»áÔ˶¯
Öظ´µã»÷Ëٶȼӿì
ËÙ¶ÈÎÞ·¨¸ü¸Ä
½â¾öBUG
ËÙ¶ÈÈ¡µ½Ä³Ð©Öµ»áÎÞ·¨Í£Ö¹£¨Õâ¸öBugÉÔºó½â¾ö£¬ÔÚ½ø»¯¹ý³ÌÖÐ×ÔÈ»½â¾ö£©
°ÑÔ˶¯ºÍÍ£Ö¹¸ô¿ª(if/else)
ÔÚ¿ªÊ¼Ô˶¯Ê±,¹Ø±ÕÒÑÓж¨Ê±Æ÷
°ÑËÙ¶ÈÓñäÁ¿±£´æ
- /**
- * Ô˶¯¿ò¼Ü-3-½â¾öBug
- */
- var timer = null;
- function startMove(element, iTarget) {
- clearInterval(timer);//ÔÚ¿ªÊ¼Ô˶¯Ê±,¹Ø±ÕÒÑÓж¨Ê±Æ÷
- timer = setInterval(function () {
- var iSpeed = 5;//°ÑËÙ¶ÈÓñäÁ¿±£´æ
- //°ÑÔ˶¯ºÍÍ£Ö¹¸ô¿ª(if/else)
- if (element.offsetLeft === iTarget) {//½áÊøÔ˶¯
- clearInterval(timer);
- } else {
- element.style.left = element.offsetLeft + iSpeed + "px";
- }
- }, 30);
- }
ÕâÑùÒ»¸ö¼òµ¥µÄÔ˶¯¿ò¼Ü¾ÍÍê³ÉÁË¡£µ«ÊÇ£¬Ôٵȵȡ£Ö»ÄÜÏòÓÒ×ߣ¿±ð¼±£¬ÎÒÃDz»ÊǶ¨ÒåÁË°ÑËٶȱä³ÉΪÁ˱äÁ¿Âð£¿Ö»ÐèÒª¶ÔËü½øÐÐһЩ´¦Àí¾ÍÐÐÀ²£¡
var iSpeed = 5;
- //ÅжϾàÀëÄ¿±êλÖ㬴ﵽ×Ô¶¯±ä»¯ËÙ¶ÈÕý¸º
- var iSpeed = 0;
- if (element.offsetLeft < iTarget) {
- iSpeed = 5;
- } else {
- iSpeed = -5;
- }
͸Ã÷¶È¶¯»
ÓñäÁ¿alpha´¢´æµ±Ç°Í¸Ã÷¶È¡£
°ÑÉÏÃæµÄelement.offsetLeft¸Ä³É±äÁ¿alpha¡£
Ô˶¯ºÍÍ£Ö¹Ìõ¼þ²¿·Ö½øÐиü¸Ä¡£ÈçÏ£º
- //͸Ã÷¶Èä¯ÀÀÆ÷¼æÈÝʵÏÖ
- if (alpha === iTarget) {
- clearInterval(time);
- } else {
- alpha += speed;
- element.style.filter = 'alpha(opacity:' + alpha + ')'; //¼æÈÝIE
- element.style.opacity = alpha / 100;//±ê×¼
- }
£¨¶þ£©»º³å¶¯»
˼¿¼£ºÔõôÑù²ÅÊÇ»º³å¶¯»£¿
Ó¦¸ÃÓÐÒÔϼ¸µã£º
Öð½¥±äÂý,×îºóÍ£Ö¹
¾àÀëÔ½Ô¶ËÙ¶ÈÔ½´ó
ËÙ¶ÈÓɾàÀë¾ö¶¨
ËÙ¶È=(Ä¿±êÖµ-µ±Ç°Öµ)/Ëõ·ÅϵÊý
Bug :ËÙ¶ÈÈ¡Õû(ʹÓÃMath·½·¨)£¬²»È»»áÉÁ
ÏòÉÏÈ¡Õû¡£Math.ceil(iSpeed)
ÏòÏÂÈ¡Õû¡£Math.floor(iSpeed)
»¹ÊǶÔËÙ¶È×÷ÎÄÕ£º
- /**
- * Ô˶¯¿ò¼Ü-4-»º³å¶¯»
- */
- function startMove(element, iTarget) {
- clearInterval(timer);
- timer = setInterval(function () {
- //ÒòΪËÙ¶ÈÒª¶¯Ì¬¸Ä±ä£¬ËùÒÔ±ØÐë·ÅÔÚ¶¨Ê±Æ÷ÖÐ
- var iSpeed = (iTarget - element.offsetLeft) / 10; //(Ä¿±êÖµ-µ±Ç°Öµ)/Ëõ·ÅϵÊý=ËÙ¶È
- iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); //ËÙ¶ÈÈ¡Õû
- if (element.offsetLeft === iTarget) {//½áÊøÔ˶¯
- clearInterval(timer);
- } else {
- element.style.left = element.offsetLeft + iSpeed + "px";
- }
- }, 30);
- }
×öµ½ÕâÀ£¨ËÙ¶ÈÈ¡µ½Ä³Ð©Öµ»áÎÞ·¨Í£Ö¹)Õâ¸öBug¾Í×Ô¶¯½â¾öÀ²£¡
Àý×Ó:»º³å²Ëµ¥
¸úËæÒ³Ãæ¹ö¶¯µÄ»º³å²à±ßÀ¸
ÔÚÏßÑÝʾ£ºcodepen
DZÔÚÎÊÌâÄ¿±êÖµ²»ÊÇÕûÊýʱ
£¨Èý£©¶àÎïÌåÔ˶¯
˼¿¼£ºÈçºÎʵÏÖ¶àÎïÌåÔ˶¯£¿
µ¥¶¨Ê±Æ÷£¬´æÔÚÎÊÌ⡣ÿ¸ödivÒ»¸ö¶¨Ê±Æ÷
¶¨Ê±Æ÷×÷Ϊ¶ÔÏóµÄÊôÐÔ
Ö±½ÓʹÓÃelement.timer°Ñ¶¨Ê±Æ÷±ä³É¶ÔÏóÉϵÄÒ»¸öÊôÐÔ¡£
²ÎÊýµÄ´«µÝ:ÎïÌå/Ä¿±êÖµ
±È½Ï¼òµ¥°ÑÉÏÃæ¿ò¼ÜµÄ½øÐÐÈçϸü¸Ä£ºtimer-->element.timer
¾ÍÕâÑù¾ÍÐÐÀ²£¡
£¨ËÄ£©ÈÎÒâÖµ±ä»¯
¿È¿È¡£ÎÒÃÇÀ´¸ødiv¼Ó¸ö1pxµÄ±ß¿ò¡£boder :1px solid #000
È»ºóÀ´ÊÔÊÔÏÂÃæµÄ´úÂë
- setInterval(function () {
- oDiv.style.width = oDiv.offsetWidth - 1 + "px";
- }, 30)
àÅ£¬ÉñÆæµÄÊÂÇé·¢ÉúÁË£¡what£¿ÎÒÉèÖõIJ»ÊÇ¿í¶ÈÔÚ¼õÂð£¿ÔõôÄáÂêÔö¼ÓÁË£¡ ²»¶Ô°¡£¬´óÐֵܡ£
¾¿¾¹ÄÄÀï³öÁËÎÊÌâÄØ£¿
Ò»ÆðÕÒÕÒ×ÊÁÏ£¬¿´¿´Îĵµ£¬ÔÀ´offsetÕâһϵÁеÄÊôÐÔ¶¼»á´æÔÚ£¬±»ÆäËûÊôÐÔ¸ÉÈŵÄÎÊÌâ¡£
ºÃ°É£¬¼ÈÈ»²»ÄÜÓã¬ÄÇôÎÒÃǾÍ˳±ã°ÑÈÎÒâÖµ±ä»¯¸ø×öÁË°É¡£
µÚÒ»²½£º»ñȡʵ¼ÊÑùʽ
ʹÓÃoffsetLeft..µÈ»ñÈ¡Ñùʽʱ, ÈôÉèÖÃÁ˱߿ò, padding, µÈ¿ÉÒԸıäÔªËØ¿í¶È¸ß¶ÈµÄÊôÐÔʱ»á³öÏÖBUG..
ͨ¹ý²éÕÒ·¢ÏÖelement.currentStyle(attr)¿ÉÒÔ»ñÈ¡¼ÆËã¹ýÖ®ºóµÄÊôÐÔ¡£
µ«ÊÇÒòΪ¼æÈÝÐÔµÄÎÊÌ⣬Ðè·â×°getStyleº¯Êý¡££¨Íò¶ñµÄIE£©
µ±È»ÅäºÏCSSµÄbox-sizingÊôÐÔÉèΪborder-box¿ÉÒÔ´ïµ½Ò»ÑùµÄЧ¹û ? (×ÔÈÏΪ£¬Î´ÑéÖ¤)¡£
- /**
- * »ñȡʵ¼ÊÑùʽº¯Êý
- * @param {HTMLElement} element ÐèҪѰÕÒµÄÑùʽµÄhtml½Úµã
- * @param {String]} attr ÔÚ¶ÔÏóÖÐÑ°ÕÒµÄÑùʽÊôÐÔ
- * @returns {String} »ñÈ¡µ½µÄÊôÐÔ
- */
- function getStyle(element, attr) {
- //IEд·¨
- if (element.currentStyle) {
- return element.currentStyle[attr];
- //±ê×¼
- } else {
- return getComputedStyle(element, false)[attr];
- }
- }
µÚ¶þ²½£º¸ÄÔìÔº¯Êý
Ìí¼Ó²ÎÊý£¬attr±íʾÐèÒª¸Ä±äµÄÊôÐÔÖµ¡£
¸ü¸Äelement.offsetLeftΪgetStyle(element, attr)¡£
ÐèҪעÒâµÄÊÇ£ºgetStyle(element, attr)²»ÄÜÖ±½ÓʹÓã¬ÒòΪËü»ñÈ¡µ½µÄ×Ö·û´®,Àý£º10px¡£
±äÁ¿iCurrentʹÓÃparseInt(),½«Ñùʽת³ÉÊý×Ö¡£
element.style.leftΪelement.style[attr]¡£
- /**
- * Ô˶¯¿ò¼Ü-4-ÈÎÒâÖµ±ä»¯
- * @param {HTMLElement} element Ô˶¯¶ÔÏó
- * @param {string} attr ÐèÒª¸Ä±äµÄÊôÐÔ¡£
- * @param {number} iTarget Ä¿±êÖµ
- */
- function startMove(element, attr, iTarget) {
- clearInterval(element.timer);
- element.timer = setInterval(function () {
- //ÒòΪËÙ¶ÈÒª¶¯Ì¬¸Ä±ä£¬ËùÒÔ±ØÐë·ÅÔÚ¶¨Ê±Æ÷ÖÐ
- var iCurrent=0;
- iCurrent = parseInt(getStyle(element, attr));//ʵ¼ÊÑùʽ´óС
- var iSpeed = (iTarget - iCurrent) / 10; //(Ä¿±êÖµ-µ±Ç°Öµ)/Ëõ·ÅϵÊý=ËÙ¶È
- iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); //ËÙ¶ÈÈ¡Õû
- if (iCurrent === iTarget) {//½áÊøÔ˶¯
- clearInterval(element.timer);
- } else {
- element.style[attr] = iCurrent + iSpeed + "px";
- }
- }, 30);
- }
ÊÔÒ»ÊÔ£¬ÕâÑùÊDz»ÊǾͿÉÒÔÁËÄØ£¿
»¹¼ÇµÃÉÏÃæÎÒÃÇдµÄ͸Ã÷¶È±ä»¯Âð? ÔÙÊÔÊÔ
¹ûÈ»»¹ÊDz»ÐУ¬ £¨·Ï»°£¬Äã¼û¹ý͸Ã÷¶ÈÓÐ"px"µ¥Î»µÄô? - -°×ÑÛ £©
µÚÈý²½£ºÍ¸Ã÷¶È¼æÈÝ´¦Àí
˼¿¼£ºÐèÒª¶ÔÄÇЩÊôÐÔ½øÐÐÐ޸ģ¿
ÅжÏattrÊDz»ÊÇ͸Ã÷¶ÈÊôÐÔopacity ¡£
¶ÔÓÚËٶȽøÐд¦Àí¡£
Ϊ͸Ã÷¶Èʱ£¬ÓÉÓÚ»ñÈ¡µ½µÄ͸Ã÷¶È»áÊÇСÊý£¬ËùÒÔÐèÒª * 100
²¢ÇÒÓÉÓÚ¼ÆËã»ú´¢´æ¸¡µãÊýµÄÎÊÌ⣬»¹ÐèÒª½«Ð¡Êý£¬½øÐÐËÄÉáÎåÈëΪÕûÊý¡£Ê¹ÓãºMath.round(parseFloat(getStyle(element, attr)) * 100)¡£
·ñÔò£¬¼ÌÐøʹÓÃĬÈϵÄËٶȡ£
¶Ô½á¹ûÊä³ö²¿·Ö½øÐиü¸Ä¡£
ÅжÏÊÇ͸Ã÷¶ÈÊôÐÔ£¬Ê¹ÓÃ͸Ã÷¶È·½·¨
·ñÔò£¬Ê¹ÓÃʹÓÃĬÈϵÄÊä³ö¸ñʽ¡£
- /**
- * Ô˶¯¿ò¼Ü-5-¼æÈÝ͸Ã÷¶È
- * @param {HTMLElement} element Ô˶¯¶ÔÏó
- * @param {string} attr ÐèÒª¸Ä±äµÄÊôÐÔ¡£
- * @param {number} iTarget Ä¿±êÖµ
- */
- function startMove(element, attr, iTarget) {
- clearInterval(element.timer);
- element.timer = setInterval(function () {
- //ÒòΪËÙ¶ÈÒª¶¯Ì¬¸Ä±ä£¬ËùÒÔ±ØÐë·ÅÔÚ¶¨Ê±Æ÷ÖÐ
- var iCurrent = 0;
- if (attr === "opacity") { //Ϊ͸Ã÷¶ÈʱִÐС£
- iCurrent = Math.round(parseFloat(getStyle(element, attr)) * 100);
- } else { //ĬÈÏÇé¿ö
- iCurrent = parseInt(getStyle(element, attr)); //ʵ¼ÊÑùʽ´óС
- }
- var iSpeed = (iTarget - iCurrent) / 10; //(Ä¿±êÖµ-µ±Ç°Öµ)/Ëõ·ÅϵÊý=ËÙ¶È
- iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); //ËÙ¶ÈÈ¡Õû
- if (iCurrent === iTarget) {//½áÊøÔ˶¯
- clearInterval(element.timer);
- } else {
- if (attr === "opacity") { //Ϊ͸Ã÷¶Èʱ£¬Ö´ÐÐ
- element.style.filter = "alpha(opacity:" + (iCurrent + iSpeed) + ")"; //IE
- element.style.opacity = (iCurrent + iSpeed) / 100; //±ê×¼
- } else { //ĬÈÏ
- element.style[attr] = iCurrent + iSpeed + "px";
- }
- }
- }, 30);
- }
µ½ÕâÀÕâ¸öÔ˶¯¿ò¼Ü¾Í»ù±¾ÉÏÍê³ÉÁË¡£µ«ÊÇ£¬ÎÒÃÇÊÇ×·ÇóÍêÃÀµÄ²»ÊÇÂð£¿
¼ÌÐø½ø»¯£¡
£¨Î壩Á´Ê½¶¯»
Á´Ê½¶¯»£º¹ËÃû˼Ò壬¾ÍÊÇÔڸôÎÔ˶¯Í£Ö¹Ê±£¬¿ªÊ¼ÏÂÒ»´ÎÔ˶¯¡£
ÈçºÎʵÏÖÄØ£¿
ʹÓûص÷º¯Êý£ºÔ˶¯Í£Ö¹Ê±,Ö´Ðк¯Êý
Ìí¼ÓfuncÐβΣ¨»Øµ÷º¯Êý£©¡£
ÔÚµ±Ç°ÊôÐÔµ½´ïÄ¿µÄµØʱiCurrent === iTarget£¬ÅжÏÊÇ·ñÓлص÷º¯Êý´æÔÚ£¬ÓÐÔòÖ´ÐС£
if (iCurrent === iTarget) {//½áÊøÔ˶¯
clearInterval(element.timer);
if (func) {
func();//»Øµ÷º¯Êý
}
}
good£¬Á´Ê½¶¯»Íê³É£¡¾àÀëÍêÃÀ»¹²îÒ»²½£¡
£¨Áù£©Í¬Ê±Ô˶¯
˼¿¼£ºÈçºÎʵÏÖͬʱÔ˶¯£¿
ʹÓÃJSON´«µÝ¶à¸öÖµ
ʹÓÃfor inÑ»·£¬±éÀúÊôÐÔ£¬ÓëÖµ¡£
¶¨Ê±Æ÷ÎÊÌâ!(Ô˶¯ÌáÇ°Í£Ö¹)
ÔÚÑ»·ÍâÉèÖñäÁ¿,¼ÙÉèËùÓеÄÖµ¶¼µ½´ïÁËÄ¿µÄֵΪtrue
ÔÚÑ»·Öмì²âÊÇ·ñµ½´ïÄ¿±êÖµ,ÈôûÓÐֵδµ½ÔòΪfalse
ÔÚÑ»·½áÊøºó,¼ì²âÊÇ·ñÈ«²¿´ïµ½Ä¿±êÖµ.ÊÇÔòÇå³ý¶¨Ê±Æ÷
ʵÏÖ£º
ɾ³ýattrÓëiTargetÁ½¸öÐβΣ¬¸ÄΪjson
ÔÚº¯Êý¿ªÊ¼Ê±£¬ÉèÖÃÒ»¸ö±ê¼Çvar flag = true; //¼ÙÉèËùÓÐÔ˶¯µ½´ïÖÕµã.
ÔÚ¶¨Ê±Æ÷ÄÚʹÓÃfor in£¬±éÀúÊôÐÔÓëÄ¿±ê£¬¸ÄдÔÀ´µÄattrÓëiTarget£¬ÎªjsonµÄÊôÐÔÓëÖµ
ÐÞ¸ÄÔ˶¯ÖÕÖ¹Ìõ¼þ£¬Ö»ÓÐÿһÏîµÄʵ¼ÊÊôÐÔÖµiCurrent£¬µÈÓÚÄ¿±êÖµjson[attr]ʱ£¬flag²ÅΪtrue¡£Çå³ý¶¨Ê±Æ÷£¬ÅжÏÊÇ·ñ»Øµ÷¡£
·ñÔò£¬¼ÌÐøÖ´ÐдúÂ룬ֱµ½ËùÓÐÊôÐÔÖµµÈÓÚÄ¿±êÖµ¡£
ÍêÃÀÔ˶¯¿ò¼Ü
- /**
- * »ñȡʵ¼ÊÑùʽº¯Êý
- * @param {HTMLElement} element ÐèҪѰÕÒµÄÑùʽµÄhtml½Úµã
- * @param {String]} attr ÔÚ¶ÔÏóÖÐÑ°ÕÒµÄÑùʽÊôÐÔ
- * @returns {String} »ñÈ¡µ½µÄÊôÐÔ
- */
- function getStyle(element, attr) {
- //IEд·¨
- if (element.currentStyle) {
- return element.currentStyle[attr];
- //±ê×¼
- } else {
- return getComputedStyle(element, false)[attr];
- }
- }
- /**
- * ÍêÃÀÔ˶¯¿ò¼Ü
- * @param {HTMLElement} element Ô˶¯¶ÔÏó
- * @param {JSON} json ÊôÐÔ£ºÄ¿±êÖµ
- * @property {String} attr ÊôÐÔÖµ
- * @config {Number} target Ä¿±êÖµ
- * @param {function} func ¿ÉÑ¡£¬»Øµ÷º¯Êý£¬Á´Ê½¶¯»¡£
- */
- function startMove(element, json, func) {
- var flag = true; //¼ÙÉèËùÓÐÔ˶¯µ½´ïÖÕµã.
- clearInterval(element.timer);
- element.timer = setInterval(function () {
- for (var attr in json) {
- //1.È¡µ±Ç°µÄÊôÐÔÖµ¡£
- var iCurrent = 0;
- if (attr === "opacity") { //Ϊ͸Ã÷¶ÈʱִÐС£
- iCurrent = Math.round(parseFloat(getStyle(element, attr)) * 100);
- } else { //ĬÈÏÇé¿ö
- iCurrent = parseInt(getStyle(element, attr)); //ʵ¼ÊÑùʽ´óС
- }
- //2.ËãÔ˶¯ËÙ¶È,¶¯»»º³åЧ¹û
- var iSpeed = (json[attr] - iCurrent) / 10; //(Ä¿±êÖµ-µ±Ç°Öµ)/Ëõ·ÅϵÊý=ËÙ¶È
- iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); //ËÙ¶ÈÈ¡Õû
- //3.δµ½´ïÄ¿±êֵʱ£¬Ö´ÐдúÂë
- if (iCurrent != json[attr]) {
- flag = false; //ÖÕÖ¹Ìõ¼þ
- if (attr === "opacity") { //Ϊ͸Ã÷¶Èʱ£¬Ö´ÐÐ
- element.style.filter = "alpha(opacity:" + (iCurrent + iSpeed) + ")"; //IE
- element.style.opacity = (iCurrent + iSpeed) / 100; //±ê×¼
- } else { //ĬÈÏ
- element.style[attr] = iCurrent + iSpeed + "px";
- }
- } else {
- flag = true;
- }
- //4. Ô˶¯ÖÕÖ¹£¬ÊÇ·ñ»Øµ÷
- if (flag) {
- clearInterval(element.timer);
- if (func) {
- func();
- }
- }
- }
- }, 30);
- }
Ô˶¯¿ò¼Ü×ܽá
Ô˶¯¿ò¼ÜÑݱä¹ý³Ì
¿ò¼Ü ±ä»¯
startMove(element) Ô˶¯
startMove(element,iTarget) ÔÈËÙ-->»º³å-->¶àÎïÌå
startMove(element,attr,iTargrt) ÈÎÒâÖµ
startMove(element,attr,iTargrt,func) Á´Ê½Ô˶¯
startMove(element,json,func) ¶àÖµ(ͬʱ)-->ÍêÃÀÔ˶¯¿ò¼Ü
TAG:JavaScript
±¾Õ¾»¶ÓÈκÎÐÎʽµÄתÔØ£¬µ«ÇëÎñ±Ø×¢Ã÷³ö´¦£¬×ðÖØËûÈËÀͶ¯³É¹û
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-729-1.html
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-729-1.html