<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>scrollAnimation</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="main">
<div class="sample_box2 animation" data-animation="slideInUp">
Animation slideInUp
</div>
<div class="sample_box1">
No Animation
</div>
<div class="sample_box2 animation" data-animation="slideInUp" >
Animation slideInUp
</div>
<div class="sample_box2 animation" data-animation="slideInLeft">
Animation slideInLeft
</div>
<div class="sample_box2 animation" data-animation="slideInRight">
Animation slideInRight
</div>
<div class="sample_box1">
No Animation
</div>
</div><!-- /.main -->
</body>
</html>
@charset "UTF-8";
html{
overflow-x: hidden; /*画面外から現れるアニメーションの為に必要*/
}
body{
background-color: #f1f1f1;
}
div.main{
position: relative;
margin: 0 auto;
padding: 1px 30px;
max-width: 1200px;
background-color: #fff;
}
div.sample_box1{
padding: 50px;
height: 350px;
background-color: beige;
box-sizing: border-box;
margin: 30px 0;
}
div.sample_box2{
padding: 50px;
height: 200px;
box-sizing: border-box;
background-color: coral;
margin: 30px 0;
}
/* ====== Animation Setting ====== */
.animation{
visibility: hidden;
}
.animated {
animation-duration: 1s;
animation-duration: 1s;
animation-fill-mode: both;
animation-fill-mode: both;
}
/* 左からスラインドイン */
@-webkit-keyframes slideInLeft {
from {
transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
visibility: hidden;
}
to {
transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
visibility: visible;
}
}
@keyframes slideInLeft {
from {
transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
visibility: hidden;
}
to {
transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
visibility: visible;
}
}
.slideInLeft {
animation-name: slideInLeft;
animation-name: slideInLeft;
}
/* 右からスラインドイン */
@-webkit-keyframes slideInRight {
from {
transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0);
}
to {
transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
visibility: visible;
}
}
@keyframes slideInRight {
from {
transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0);
}
to {
transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
visibility: visible;
}
}
.slideInRight {
animation-name: slideInRight;
animation-name: slideInRight;
}
/* 下からスラインドイン */
@-webkit-keyframes slideInUp {
from {
transform: translate3d(0, 100%, 0);
transform: translate3d(0, 100%, 0);
}
to {
transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
visibility: visible;
}
}
@keyframes slideInUp {
from {
transform: translate3d(0, 100%, 0);
transform: translate3d(0, 100%, 0);
}
to {
transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
visibility: visible;
}
}
.slideInUp {
animation-name: slideInUp;
animation-name: slideInUp;
}
jQuery(function($) {
function animationSetting(){
$('.animation').each(function(){
var obj = $(this),
objH = $(this).outerHeight(),
scrolled = $(window).scrollTop(),
viewed = scrolled + $(window).height(),
objTop = $(this).offset().top,
objBottom = objTop + objH,
excnum = 0.4;
if((objTop + objH * excnum) <= viewed && (objBottom - objH * excnum) >= scrolled){
obj.addClass(obj.data('animation')).addClass('animated');
}
})
}
animationSetting();
$(window).on('scroll', function() {
animationSetting();
});
});
This Pen doesn't use any external CSS resources.