<head> <meta charset="UTF-8"> <title>test</title> <style> * { margin: 0; padding: 0; } textarea { position: fixed; right: 0; top: 50px; } div { margin-top: 10px; width: 100px; height: 30px; background: red; border: 1px solid green; cursor: pointer; opacity: 0.3; filter: alpha(opacity=30); } </style></head><body style="height:3000px;"> <textarea name="" id="" cols="30" rows="10"></textarea> <div>div1</div> <div>div2</div> <div>div3</div> <script src="test.js"> fontSize </script> <script> window.onload = function() { var oDiv1 = document.getElementsByTagName('div'); // for (var i = 0, len = oDiv1.length; i < len; i++) { // oDiv1[i].timer = null; // oDiv1[i].onclick = function() { // var that = this; // uniformMove(that, { // "width": 300, // "height": 100, // "opacity": 100 // }, 10, function() { // uniformMove(that, { // "width": 100, // "height": 30, // "opacity": 30 // }, -10); // }); // }; // } for (var i = 0, len = oDiv1.length; i < len; i++) { oDiv1[i].timer = null; oDiv1[i].onclick = function() { var that = this; startMove(that, { "width": 300, "height": 100, "opacity": 100 }, function() { startMove(that, { "width": 100, "height": 30, "opacity": 30 }); }); }; } } </script></body>/** * [getStyle 擷取計算出來的樣式] * @param {[type]} obj [元素對象] * @param {[type]} attr [屬性名稱] * @return {[type]} [對應屬性名稱的值] */function getStyle(obj, attr) { /*相容IE678*/ if (obj.currentStyle) { // IE return obj.currentStyle[attr]; } else { // 其他 return getComputedStyle(obj, false)[attr]; }}/** * [startMove 完美運動架構緩衝運動] * @param {[Ojbect]} obj [description] * @param {[Ojbect json]} jsonObj [json對象] * @return {[type]} [沒有傳回值] */function startMove(obj, jsonObj, fn) { clearInterval(obj.timer); obj.timer = setInterval(function() { // 用來存標記 var array = []; // 開始設都沒有達到目標點 var flag = false; for (var attr in jsonObj) { // 擷取當前值 if (attr == 'opacity') { // 因為opacity是小數,所以用 parseFloat // 用parseFloat之後會出現,52.00000000000001的情況,所以要加上Math.round // 這裡很奇怪 parseInt(parseFloat(getStyle(obj, attr) * 100));在Google裡面不相容 // 其他瀏覽器都得到了很好的相容 var curr = Math.round(parseFloat(getStyle(obj, attr) * 100)); } else { var curr = parseInt(getStyle(obj, attr)); }; // 求速度 var speed = (jsonObj[attr] - curr) / 6; speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); if (attr == 'opacity') { obj.style.opacity = (curr + speed) / 100; obj.style.filter = 'alpha(opacity=' + (curr + speed) + ')'; } else { obj.style[attr] = (curr + speed) + 'px'; }; // 檢測停止 if (jsonObj[attr] == curr) { flag = true; } else { flag = false; } array.push(flag); }; // console.log(array); for (var i = 0, len = array.length; i < len; i++) { // 在這個數組裡面只要有一個false就把標記flag設為false if (array[i] == false) { flag = false; } } // 清除定時器 if (flag) { clearInterval(obj.timer); if (fn) { fn(); } } // console.log('width' + ": " + parseInt(getStyle(obj, 'width'))); // console.log('height' + ": " + parseInt(getStyle(obj, 'height'))); console.log('opacity' + ": " + Math.round(parseFloat(getStyle(obj, 'opacity') * 100))); // console.log('fontSize' + ": " + parseInt(getStyle(obj, 'fontSize'))); }, 30);}function uniformMove(obj, json, speed, fn) { // 速度 // var speed = speed; clearInterval(obj.timer); obj.timer = setInterval(function() { var flag = false; var array = []; for (var attr in json) { // 取值 if (attr == 'opacity') { var curr = Math.round(parseFloat(getStyle(obj, attr)) * 100); } else { var curr = parseInt(getStyle(obj, attr)); } // 當最後一次的運動不足用來加speed的時候,直接運動到結束 if (Math.abs(json[attr] - curr) < Math.abs(speed)) { if (attr == 'opacity') { obj.style.opacity = json[attr] / 100; obj.style.filter = 'alpha(opacity=' + json[attr] + ')'; } else { obj.style[attr] = json[attr] + 'px'; }; } else { if (attr == 'opacity') { obj.style.opacity = (curr + speed) / 100; obj.style.filter = 'alpha(opacity=' + (curr + speed) + ')'; } else { obj.style[attr] = (curr + speed) + 'px'; }; }; if (curr == json[attr]) { flag = true; } else { flag = false; }; array.push(flag); }; // 檢測停止 for (var i = 0, len = array.length; i < len; i++) { if (array[i] == false) { flag = false; } }; if (flag == true) { clearInterval(obj.timer); if (fn) { fn(); } }; // console.log('width' + ": " + parseInt(getStyle(obj, 'width'))); // console.log('height' + ": " + parseInt(getStyle(obj, 'height'))); // console.log('opacity' + ": " + Math.round(parseFloat(getStyle(obj, 'opacity') * 100))); // console.log('fontSize' + ": " + parseInt(getStyle(obj, 'fontSize'))); console.log(parseFloat(getStyle(obj, 'opacity'))); }, 30);}