html5+css+js實現動畫特效,人物動畫
阿新 • • 發佈:2019-01-01
// 動畫結束事件
var animationEnd = (function() {
var explorer = navigator.userAgent;
if (~explorer.indexOf('WebKit')) {
return 'webkitAnimationEnd';
}
return 'animationend';
})();
///////////
//燈動畫 //
///////////
var lamp = {
elem: $('.b_background'),
bright: function() {
this.elem.addClass('lamp-bright');
},
dark: function() {
this.elem.removeClass('lamp-bright');
}
};
var container = $("#content");
var swipe = Swipe(container);
visualWidth = container.width();
visualHeight = container.height();
// 頁面滾動到指定的位置
function scrollTo(time, proportionX) {
var distX = visualWidth * proportionX;
swipe.scrollTo(distX, time);
}
// 獲取資料
var getValue = function(className) {
var $elem = $('' + className + '');
//走路的路線座標
return {
height: $elem.height(),
top: $elem.position().top
};
};
// 橋的Y軸
var bridgeY = function() {
var data = getValue('.c_background_middle');
return data.top;
}();
////////
//小女孩 //
////////
var girl = {
elem: $('.girl'),
getHeight: function() {
return this.elem.height();
},
setOffset: function() {
this.elem.css({
left: visualWidth / 2,
top: bridgeY - this.getHeight()
});
}
};
// 修正小女孩位置
girl.setOffset();
// 用來臨時調整頁面
swipe.scrollTo(visualWidth * 2, 0);
function doorAction(left, right, time) {
var $door = $('.door');
var doorLeft = $('.door-left');
var doorRight = $('.door-right');
var defer = $.Deferred();
var count = 2;
// 等待開門完成
var complete = function() {
if (count == 1) {
defer.resolve();
return;
}
count--;
}
doorLeft.transition({
'left': left
}, time, complete);
doorRight.transition({
'left': right
}, time, complete);
return defer;
}
// 開門
function openDoor() {
return doorAction('-50%', '100%', 2000);
}
// 關門
function shutDoor() {
return doorAction('0%', '50%', 2000);
}
/**
* 小孩走路
* @param {[type]} container [description]
*/
function BoyWalk() {
var container = $("#content");
// 頁面可視區域
var visualWidth = container.width();
var visualHeight = container.height();
// 獲取資料
var getValue = function(className) {
var $elem = $('' + className + '');
// 走路的路線座標
return {
height: $elem.height(),
top: $elem.position().top
};
};
// 路的Y軸
var pathY = function() {
var data = getValue('.a_background_middle');
return data.top + data.height / 2;
}();
var $boy = $("#boy");
var boyWidth = $boy.width();
var boyHeight = $boy.height();
// 設定下高度
$boy.css({
top: pathY - boyHeight + 25
});
// 暫停走路
function pauseWalk() {
$boy.addClass('pauseWalk');
}
// 恢復走路
function restoreWalk() {
$boy.removeClass('pauseWalk');
}
// css3的動作變化
function slowWalk() {
$boy.addClass('slowWalk');
}
// 用transition做運動
function stratRun(options, runTime) {
var dfdPlay = $.Deferred();
// 恢復走路
restoreWalk();
// 運動的屬性
$boy.transition(
options,
runTime,
'linear',
function() {
dfdPlay.resolve(); // 動畫完成
});
return dfdPlay;
}
// 開始走路
function walkRun(time, dist, disY) {
time = time || 3000;
// 腳動作
slowWalk();
// 開始走路
var d1 = stratRun({
'left': dist + 'px',
'top': disY ? disY : undefined
}, time);
return d1;
}
// 走進商店
function walkToShop(runTime) {
var defer = $.Deferred();
var doorObj = $('.door');
// 門的座標
var offsetDoor = doorObj.offset();
var doorOffsetLeft = offsetDoor.left;
var doorOffsetTop = offsetDoor.top;
// 小孩當前的座標
var posBoy = $boy.position();
var boyPoxLeft = posBoy.left;
var boyPoxTop = posBoy.top;
// 中間位置
var boyMiddle = $boy.width() / 2;
var doorMiddle = doorObj.width() / 2;
var doorTopMiddle = doorObj.height() / 2;
// 當前需要移動的座標
instanceX = (doorOffsetLeft + doorMiddle) - (boyPoxLeft + boyMiddle);
// Y的座標
// top = 人物底部的top - 門中見的top值
instanceY = boyPoxTop + boyHeight - doorOffsetTop + (doorTopMiddle);
// 開始走路
var walkPlay = stratRun({
transform: 'translateX(' + instanceX + 'px),translateY(-' + instanceY + 'px),scale(0.5,0.5)',
opacity: 0.1
}, 2000);
// 走路完畢
walkPlay.done(function() {
$boy.css({
opacity: 0
});
defer.resolve();
});
return defer;
}
// 走出店
function walkOutShop(runTime) {
var defer = $.Deferred();
restoreWalk();
// 開始走路
var walkPlay = stratRun({
transform: 'translateX(' + instanceX + 'px),translateY(0),scale(1,1)',
opacity: 1
}, runTime);
// 走路完畢
walkPlay.done(function() {
defer.resolve();
});
return defer;
}
// 計算移動距離
function calculateDist(direction, proportion) {
return (direction == "x" ?
visualWidth : visualHeight) * proportion;
}
return {
// 開始走路
walkTo: function(time, proportionX, proportionY) {
var distX = calculateDist('x', proportionX);
var distY = calculateDist('y', proportionY);
return walkRun(time, distX, distY);
},
// 走進商店
toShop: function() {
return walkToShop.apply(null, arguments);
},
// 走出商店
outShop: function() {
return walkOutShop.apply(null, arguments);
},
// 停止走路
stopWalk: function() {
pauseWalk();
},
setColoer: function(value) {
$boy.css('background-color', value)
},
// 獲取男孩的寬度
getWidth: function() {
return $boy.width();
},
// 復位初始狀態
resetOriginal: function() {
this.stopWalk();
// 恢復圖片
$boy.removeClass('slowWalk slowFlolerWalk').addClass('boyOriginal');
},
// 轉身動作
rotate: function(callback) {
restoreWalk();
$boy.addClass('boy-rotate');
// 監聽轉身完畢
if (callback) {
$boy.on(animationEnd, function() {
callback();
$(this).off();
});
}
},
// 取花
talkFlower: function() {
$boy.addClass('slowFlolerWalk');
}
}
}
var animationEnd = (function() {
var explorer = navigator.userAgent;
if (~explorer.indexOf('WebKit')) {
return 'webkitAnimationEnd';
}
return 'animationend';
})();
///////////
//燈動畫 //
///////////
var lamp = {
elem: $('.b_background'),
bright: function() {
this.elem.addClass('lamp-bright');
},
dark: function() {
this.elem.removeClass('lamp-bright');
}
};
var container = $("#content");
var swipe = Swipe(container);
visualWidth = container.width();
visualHeight = container.height();
// 頁面滾動到指定的位置
function scrollTo(time, proportionX) {
var distX = visualWidth * proportionX;
swipe.scrollTo(distX, time);
}
// 獲取資料
var getValue = function(className) {
var $elem = $('' + className + '');
//走路的路線座標
return {
height: $elem.height(),
top: $elem.position().top
};
};
// 橋的Y軸
var bridgeY = function() {
var data = getValue('.c_background_middle');
return data.top;
}();
////////
//小女孩 //
////////
var girl = {
elem: $('.girl'),
getHeight: function() {
return this.elem.height();
},
setOffset: function() {
this.elem.css({
left: visualWidth / 2,
top: bridgeY - this.getHeight()
});
}
};
// 修正小女孩位置
girl.setOffset();
// 用來臨時調整頁面
swipe.scrollTo(visualWidth * 2, 0);
function doorAction(left, right, time) {
var $door = $('.door');
var doorLeft = $('.door-left');
var doorRight = $('.door-right');
var defer = $.Deferred();
var count = 2;
// 等待開門完成
var complete = function() {
if (count == 1) {
defer.resolve();
return;
}
count--;
}
doorLeft.transition({
'left': left
}, time, complete);
doorRight.transition({
'left': right
}, time, complete);
return defer;
}
// 開門
function openDoor() {
return doorAction('-50%', '100%', 2000);
}
// 關門
function shutDoor() {
return doorAction('0%', '50%', 2000);
}
/**
* 小孩走路
* @param {[type]} container [description]
*/
function BoyWalk() {
var container = $("#content");
// 頁面可視區域
var visualWidth = container.width();
var visualHeight = container.height();
// 獲取資料
var getValue = function(className) {
var $elem = $('' + className + '');
// 走路的路線座標
return {
height: $elem.height(),
top: $elem.position().top
};
};
// 路的Y軸
var pathY = function() {
var data = getValue('.a_background_middle');
return data.top + data.height / 2;
}();
var $boy = $("#boy");
var boyWidth = $boy.width();
var boyHeight = $boy.height();
// 設定下高度
$boy.css({
top: pathY - boyHeight + 25
});
// 暫停走路
function pauseWalk() {
$boy.addClass('pauseWalk');
}
// 恢復走路
function restoreWalk() {
$boy.removeClass('pauseWalk');
}
// css3的動作變化
function slowWalk() {
$boy.addClass('slowWalk');
}
// 用transition做運動
function stratRun(options, runTime) {
var dfdPlay = $.Deferred();
// 恢復走路
restoreWalk();
// 運動的屬性
$boy.transition(
options,
runTime,
'linear',
function() {
dfdPlay.resolve(); // 動畫完成
});
return dfdPlay;
}
// 開始走路
function walkRun(time, dist, disY) {
time = time || 3000;
// 腳動作
slowWalk();
// 開始走路
var d1 = stratRun({
'left': dist + 'px',
'top': disY ? disY : undefined
}, time);
return d1;
}
// 走進商店
function walkToShop(runTime) {
var defer = $.Deferred();
var doorObj = $('.door');
// 門的座標
var offsetDoor = doorObj.offset();
var doorOffsetLeft = offsetDoor.left;
var doorOffsetTop = offsetDoor.top;
// 小孩當前的座標
var posBoy = $boy.position();
var boyPoxLeft = posBoy.left;
var boyPoxTop = posBoy.top;
// 中間位置
var boyMiddle = $boy.width() / 2;
var doorMiddle = doorObj.width() / 2;
var doorTopMiddle = doorObj.height() / 2;
// 當前需要移動的座標
instanceX = (doorOffsetLeft + doorMiddle) - (boyPoxLeft + boyMiddle);
// Y的座標
// top = 人物底部的top - 門中見的top值
instanceY = boyPoxTop + boyHeight - doorOffsetTop + (doorTopMiddle);
// 開始走路
var walkPlay = stratRun({
transform: 'translateX(' + instanceX + 'px),translateY(-' + instanceY + 'px),scale(0.5,0.5)',
opacity: 0.1
}, 2000);
// 走路完畢
walkPlay.done(function() {
$boy.css({
opacity: 0
});
defer.resolve();
});
return defer;
}
// 走出店
function walkOutShop(runTime) {
var defer = $.Deferred();
restoreWalk();
// 開始走路
var walkPlay = stratRun({
transform: 'translateX(' + instanceX + 'px),translateY(0),scale(1,1)',
opacity: 1
}, runTime);
// 走路完畢
walkPlay.done(function() {
defer.resolve();
});
return defer;
}
// 計算移動距離
function calculateDist(direction, proportion) {
return (direction == "x" ?
visualWidth : visualHeight) * proportion;
}
return {
// 開始走路
walkTo: function(time, proportionX, proportionY) {
var distX = calculateDist('x', proportionX);
var distY = calculateDist('y', proportionY);
return walkRun(time, distX, distY);
},
// 走進商店
toShop: function() {
return walkToShop.apply(null, arguments);
},
// 走出商店
outShop: function() {
return walkOutShop.apply(null, arguments);
},
// 停止走路
stopWalk: function() {
pauseWalk();
},
setColoer: function(value) {
$boy.css('background-color', value)
},
// 獲取男孩的寬度
getWidth: function() {
return $boy.width();
},
// 復位初始狀態
resetOriginal: function() {
this.stopWalk();
// 恢復圖片
$boy.removeClass('slowWalk slowFlolerWalk').addClass('boyOriginal');
},
// 轉身動作
rotate: function(callback) {
restoreWalk();
$boy.addClass('boy-rotate');
// 監聽轉身完畢
if (callback) {
$boy.on(animationEnd, function() {
callback();
$(this).off();
});
}
},
// 取花
talkFlower: function() {
$boy.addClass('slowFlolerWalk');
}
}
}