フェードアニメーションの自作【Vanilla JS】
カテゴリー
JavaScript
const fadeAnime = (obj) => {
let type = obj.type;
if(type !== 'toggle' && type !== 'in' && type !== 'out') return;
const target = document.getElementById(obj.target);
if(target === null) return;
const displayProperty = getComputedStyle(target).display;
if(type ==='toggle') {
type = (displayProperty === 'none')? 'in' : 'out';
}
const speed = obj.speed;
let inSpeed = 300,
outSpeed = 300 ;
if(speed instanceof Object) {
if(!isNaN(Number(speed.in))) inSpeed = speed.in;
if(!isNaN(Number(speed.out))) inSpeed = speed.out;
} else {
if(!isNaN(Number(speed))) {
inSpeed = speed;
outSpeed = speed;
}
}
let inCallBack = null,
outCallBack = null;
if(typeof obj.callBack === 'function') {
inCallBack = obj.callBack;
outCallBack = obj.callBack;
} else if(obj.callBack instanceof Object) {
if(typeof obj.callBack.in === 'function') inCallBack = obj.callBack.in;
if(typeof obj.callBack.out === 'function') outCallBack = obj.callBack.out;
}
let inBeforeAction = null,
outBeforeAction = null;
if(typeof obj.beforeAction === 'function') {
inBeforeAction = obj.beforeAction;
outBeforeAction = obj.beforeAction;
} else if(obj.beforeAction instanceof Object) {
if(typeof obj.beforeAction.in === 'function') inBeforeAction = obj.beforeAction.in;
if(typeof obj.beforeAction.out === 'function') outBeforeAction = obj.beforeAction.out;
}
let isFadein = null,
isFadeout = null;
const property = (obj.display)? obj.display : 'block';
const fadeIn = (obj) => {
cancelAnimationFrame(isFadeout);
const start = performance.now();
const target = obj.target;
const duration = obj.duration || 1000;
target.style.opacity = 0;
target.style.display = property;
const animate = (current_time) => {
const elapsed = current_time - start;
let progress = Math.min(elapsed / duration, 1);
target.style.opacity = progress;
if (progress < 1) {
isFadein = requestAnimationFrame(animate);
} else {
if(inCallBack !== null) inCallBack();
}
}
isFadein = requestAnimationFrame(animate);
}
const fadeOut = (obj) => {
cancelAnimationFrame(isFadein);
const start = performance.now();
const target = obj.target;
const duration = obj.duration || 1000;
target.style.opacity = 1;
const animate = (current_time) => {
const elapsed = current_time - start;
let progress = Math.max(1 - elapsed / duration, 0);
target.style.opacity = progress;
if (progress > 0) {
isFadeout = requestAnimationFrame(animate);
} else {
target.style.display = 'none';
if(outCallBack !== null) outCallBack();
}
}
isFadeout = requestAnimationFrame(animate);
}
if(type === 'in') {
if(inBeforeAction !== null) inBeforeAction();
fadeIn({
target: target ,
duration: inSpeed
});
} else if(type === 'out') {
if(outBeforeAction !== null) outBeforeAction();
fadeOut({
target: target ,
duration: outSpeed
});
}
}
fadeAnime({
type: 'out',
target: trigger,
speed: modal_close_speed
});
免責事項
- 当ブログでは、執筆者の経験に基づいた技術情報や知識を提供していますが、その正確性や普遍性を保証するものではありません。情報は執筆時点のものであり、技術の進展により古くなる可能性があります。これらの情報を利用する際は、自己責任で行ってください。必要に応じて専門家の助言を求めることをお勧めします。
- 当ブログで提供するプログラムコードは、執筆者の最善の知識に基づいていますが、その正確性や完全性を保証するものではありません。コードの利用や実行により生じた損害や問題については、一切の責任を負いかねます。コードの使用は、自己責任で行ってください。
- 当サイトで使用しているスクリーンショット画像について、著作権はサイトの権利者に帰属します。掲載に不都合がある場合、お手数ですがお問い合わせフォームよりご連絡ください。
- 当サイトからリンクよって他のサイトに移動された場合、移動先サイトで提供される情報、サービス等について一切の責任を負いかねますのでご了承ください。
- 当サイトに掲載された内容によって生じた損害等の一切の責任を負いかねますのでご了承ください。