課程
/前端開發
/JavaScript
/JS動畫效果
發代碼.......thanks
2014-11-19
源自:JS動畫效果 6-1
正在回答
<!DOCTYPE html><html lang="en"><head> ? ?<meta charset="UTF-8"> ? ?<title></title> ? ?<style> *{ ? ? ? ? ? ?margin: 0; padding: 0; } ? ? ? ?ul li{ ? ? ? ? ? ?width: 200px; height: 100px; background: lawngreen; margin-bottom: 20px; border: 4px solid brown; filter: alpha(opacity:30); opacity: 0.3; } ? ?</style> ? ?<script src="index.js"></script> ? ?<script> window.onload=function(){ ? ? ? ? ? ?var oLi=document.getElementById('li1'); oLi.onmouseover=function(){ ? ? ? ? ? ? ? ?startMove(oLi,{width:201,height:200,opacity:100}); } ? ? ? ? ? ?oLi.onmouseout=function(){ ? ? ? ? ? ? ? ?startMove(oLi,{width:200,height:100,opacity:30}); } ? ? ? ?} ? ?</script></head><body><ul> ? ?<li id="li1"></li></ul></body></html>
/** * Created by HL on 2016/1/2. */function getStyle(obj, attr) { ? ?if (obj.currentStyle) { ? ? ? ?return obj.currentStyle[attr]; ? ?} else { ? ? ? ?return getComputedStyle(obj, false)[attr]; ? ?}}function startMove(obj, json, fn) { ? ?var flag = true; ? ?clearInterval(obj.timer); ? ?obj.timer = setInterval(function () { ? ? ? ?for (var attr in json) { ? ? ? ? ? ?//1、獲取當前值 ? ? ? ? ? ?var icur = 0; ? ? ? ? ? ?if (attr == 'opacity') { ? ? ? ? ? ? ? ?icur = Math.round(parseFloat(getStyle(obj, attr)) * 100.0);//四舍五入 ? ? ? ? ? ?} else { ? ? ? ? ? ? ? ?icur = parseInt(getStyle(obj, attr)); ? ? ? ? ? ?} ? ? ? ? ? ?//2、計算速度 ? ? ? ? ? ?var speed = (json[attr] - icur) / 8; ? ? ? ? ? ?speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); ? ? ? ? ? ?//3、檢測停止 ? ? ? ? ? ?if (icur != json[attr]) { ? ? ? ? ? ? ? ?flag = false; ? ? ? ? ? ?} ? ? ? ? ? ?if (attr == 'opacity') { ? ? ? ? ? ? ? ?obj.style.filter = 'alpha(opacity:' + (icur + speed) + ')'; ? ? ? ? ? ? ? ?obj.style.opacity = (icur + speed) / 100.0; ? ? ? ? ? ?} else { ? ? ? ? ? ? ? ?obj.style[attr] = icur + speed + 'px'; ? ? ? ? ? ?} ? ? ? ? ? ?if(flag){ ? ? ? ? ? ? ? ?clearInterval(obj.timer); ? ? ? ? ? ? ? ?if(fn){ ? ? ? ? ? ? ? ? ? ?fn(); ? ? ? ? ? ? ? ?} ? ? ? ? ? ?} ? ? ? ?} ? ?}, 30);}
舉報
通過本課程JS動畫的學習,從簡單動畫開始,逐步深入各種動畫框架封裝
1 回答能不能把代碼放出來啊
2 回答我想看實例的代碼代碼呢?
3 回答求代碼代碼
2 回答完美框架代碼沒有效果,誰能看看哪里錯了
4 回答麻煩幫忙看下,我寫出來的代碼沒有效果
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-01-02
<!DOCTYPE html>
<html lang="en">
<head>
? ?<meta charset="UTF-8">
? ?<title></title>
? ?<style>
*{
? ? ? ? ? ?margin: 0;
padding: 0;
}
? ? ? ?ul li{
? ? ? ? ? ?width: 200px;
height: 100px;
background: lawngreen;
margin-bottom: 20px;
border: 4px solid brown;
filter: alpha(opacity:30);
opacity: 0.3;
}
? ?</style>
? ?<script src="index.js"></script>
? ?<script>
window.onload=function(){
? ? ? ? ? ?var oLi=document.getElementById('li1');
oLi.onmouseover=function(){
? ? ? ? ? ? ? ?startMove(oLi,{width:201,height:200,opacity:100});
}
? ? ? ? ? ?oLi.onmouseout=function(){
? ? ? ? ? ? ? ?startMove(oLi,{width:200,height:100,opacity:30});
}
? ? ? ?}
? ?</script>
</head>
<body>
<ul>
? ?<li id="li1"></li>
</ul>
</body>
</html>
2016-01-02
/**
* Created by HL on 2016/1/2.
*/
function getStyle(obj, attr) {
? ?if (obj.currentStyle) {
? ? ? ?return obj.currentStyle[attr];
? ?} else {
? ? ? ?return getComputedStyle(obj, false)[attr];
? ?}
}
function startMove(obj, json, fn) {
? ?var flag = true;
? ?clearInterval(obj.timer);
? ?obj.timer = setInterval(function () {
? ? ? ?for (var attr in json) {
? ? ? ? ? ?//1、獲取當前值
? ? ? ? ? ?var icur = 0;
? ? ? ? ? ?if (attr == 'opacity') {
? ? ? ? ? ? ? ?icur = Math.round(parseFloat(getStyle(obj, attr)) * 100.0);//四舍五入
? ? ? ? ? ?} else {
? ? ? ? ? ? ? ?icur = parseInt(getStyle(obj, attr));
? ? ? ? ? ?}
? ? ? ? ? ?//2、計算速度
? ? ? ? ? ?var speed = (json[attr] - icur) / 8;
? ? ? ? ? ?speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
? ? ? ? ? ?//3、檢測停止
? ? ? ? ? ?if (icur != json[attr]) {
? ? ? ? ? ? ? ?flag = false;
? ? ? ? ? ?}
? ? ? ? ? ?if (attr == 'opacity') {
? ? ? ? ? ? ? ?obj.style.filter = 'alpha(opacity:' + (icur + speed) + ')';
? ? ? ? ? ? ? ?obj.style.opacity = (icur + speed) / 100.0;
? ? ? ? ? ?} else {
? ? ? ? ? ? ? ?obj.style[attr] = icur + speed + 'px';
? ? ? ? ? ?}
? ? ? ? ? ?if(flag){
? ? ? ? ? ? ? ?clearInterval(obj.timer);
? ? ? ? ? ? ? ?if(fn){
? ? ? ? ? ? ? ? ? ?fn();
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?}
? ?}, 30);
}