animate.css icon indicating copy to clipboard operation
animate.css copied to clipboard

How to activate animation when the content scrolls into view.

Open rakeshmali opened this issue 10 years ago • 19 comments

I have a some content and images that animates with animate.css and the animation currently activates as the page loads.

The problem I have is that the given content and images are placed off screen due to lots of content before it so by the time a user scrolls down to it, the animation has already finished.

I was looking for ways either through CSS3 or jQuery to only activate the animation on the content when the viewer sees the this.

If someone could point me in the right direction, it would be really helpful.

rakeshmali avatar Dec 17 '15 05:12 rakeshmali

Take a look at ScrollReveal https://github.com/jlmakes/scrollReveal.js/tree/master.

Ooops-404 avatar Dec 17 '15 05:12 Ooops-404

Is there any possibility for doing the same using animate.css? As I have already implemented it into my website :(

rakeshmali avatar Dec 17 '15 08:12 rakeshmali

First, thanks for your great work :-) I'm starting with MVC6 and just implemented your .css in my MVC6 app, to see what happens. Principally it works and is easy to implement (how it should be). But, I and also noted that problem (so... +1 to rakeshmali). => An animation only is useful, if the user is able to see it :-)

My feedback for now:

  • principally very useful and easy to intergrate
  • only usable in larger pages, if the animation is started, if the content comes to the viewable zone
  • works fine with small animated objects, that are viewable by start (in all browsers)
  • I have some large pages where I show some large objects in bootstrap-objects:
    • only browser that shows the animation of the large object like expected: Firefox
    • in GoogleChrome, it stucks a bit
    • in IE11 and Edge, it don't run correct

I have done the tests with VS2015 in localhost (what is slow anyway), so I have to test it on a selfhosting-host before I'm sure about the problems...

So my conclusion for now:

  • Thanks once again for your work!
  • If possible, your .css should handle, that the animation only is showed, if the content becomes visible => Else it's too complicated to take care about by implement some further stuff

Only add a .css-file, that handles all itself is really "sexy"

Can you say something about use the animations with Bootstrap (is there somethin special to note about)?

FredyWenger avatar Dec 17 '15 15:12 FredyWenger

@rakeshmali You'll have to use some javascript for this, there's no other way. Here a small jQuery function to do this: https://gist.github.com/eltonmesquita/bd803a21f27b12a58df1 If you prefer, I've done a better version with Vanilla Javascript + requestFrameAnimation for better performance, but it's more complex and have two small dependencies.

eltonmesquita avatar Dec 17 '15 21:12 eltonmesquita

@FredyWenger I'm not sure if I understood everything, but here's some answers: 1- Animate.css is a CSS library, so there's a limit to what it can do. It can't, for instance, start an animation programatically as when an element is visible int the viewport, for this you have Javascript. 2 - You have to consider performance when using animations on an webpage/webapp. You shouldn't animate big elements as it can be costly. A good read is some of Paul Lewis posts. It's worth to dig in his blog, you'll learn tons about performance. 3 - Animate.css should work fine with any framework, library or whatever you use, but be aware that CSS Animations use CSS property and it can override any property used in your selector, like if you use any transform in an element and add an animate.css class it'll probably override it and you'll have some problems. Another thing to be aware is that framework can have a lot of CSS for some elements and it can cause bad performance.

TL;DR: Animate.css takes care of the animations, how the animation is added and the good sense of using it performance wise, is up to you.

eltonmesquita avatar Dec 17 '15 22:12 eltonmesquita

@eltonmesquita: Thanks for your work once again and your detailed answer. 1 - I have just started with MVC6 (and also CSS) but thought that there are some CSS "functions" that can be used to detect, if an element is viewable or not (and if yes, start the animation). I have created a new homepage for my company with wix.com. They have a stunning html-editor and various animations that can be configured easily (all run's only, it the element becomes visible and you can define, if they should run only once or every-time the element is showed). But they use reactjs and - maybe - do this by reactjs or javascript. As I have wrote, it would be sexy, if animate.css would take care of the problem itself (or you would "bundle" a matching javascript to animate.css so that the problem is solved within the bundled javascript and there where some properties link "animate one time" / "animate every time").
But, O.K.... I have to look myself... 2 - Thanks for the link, I will check it out. 3 - So... it is no problem to use animale.css also with bootstrap. TL,DR: That's clear and I will only use it where it make sense and do not downgrade the performance.

Finally thanks. And... greetings from Switzerland.

FredyWenger avatar Dec 18 '15 08:12 FredyWenger

@eltonmesquita Thanks for sharing the trick of using animate.css according to my project. And @FredyWenger, you said it very true, "there should be properties link for uses as accordingly like "animate one time" / "animate every time"... Animate.css do a great job, but have some disabilities in it's use cases. I think if they overcome from these disabilities then this will be the most popular/Useful library for all users [ Like me :) ].

rakeshmali avatar Dec 18 '15 12:12 rakeshmali

To add some javascript to handle some common use cases may be a good idea but the problem is the this is out of the library's scope. You know, Animate.CSS.

If everything is clear, please close the issue.

eltonmesquita avatar Dec 18 '15 14:12 eltonmesquita

I have done some short tests (played a bit) with Javascript myself now (note: I work with MVC6 RC) It seems to work with IE, Edge and Chrome, but not with FF. So the animation is applied (one time), when the users scrolls..

Test placeholder (Header)

<p>Test placeholder (Header)</p>
<div class="animated fadeInLeft">
    <p><strong>Animation is done without check if visible or not</strong></p>
</div>
<p>Test Placeholder (Header)</p>
<div class="test" id="test" onscroll='Animation();'>
    <p><strong>Animation with check if visible or not</strong></p>
</div>
@section scripts
{
    <script type="text/javascript">
        {
            window.addEventListener("scroll", Animation);
            function Animation()
            {
                $("#test:visible").addClass("animated fadeInLeft");
            }
        }
    </script>
}

So, this set an event-handler to the window-scroll event and then apply the class, if the element becomes visible (and... don't work with FF). Further, the case that the element is visible at startup is not handled yet and to take the scroll-event of the main-window may not be optimal... But I'm sure, you could do it better... :-)

Anyway... thanks for your work... For me, the case can be closed (but I'm not able to close it, as it isn't my case :-)

.

FredyWenger avatar Dec 18 '15 15:12 FredyWenger

@rakeshmali: Please close the case for @eltonmesquita

FredyWenger avatar Dec 18 '15 15:12 FredyWenger

Ok thanks a lot Guys for your kind suggestions.

rakeshmali avatar Dec 19 '15 08:12 rakeshmali

Is this JS defunct now? It's not working for me. I'm using animate.css as well. http://jsfiddle.net/jarrodwhitley/h57ovjLb/114/

ghost avatar Aug 01 '18 17:08 ghost

If you are using animate.css, then you have to include wow.js in your code.

Then animation will work as the text will be visible to viewport

nehadevli avatar Aug 02 '18 06:08 nehadevli

What about using Aniview https://jjcosgrove.github.io/jquery-aniview/

tomasts248 avatar Aug 15 '18 05:08 tomasts248

For all who still don't have a quick solution. I'm using it this way. The basic idea for scrolling came from Sam Sehnert, Digital Fusion. This sample is quick and dirty, but ready to copy&paste and try...

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="animate.css">
<style>
body {
  margin: 100px
}

.slide-item-wrapper,
.slide-item-wrapper-up 
{
  width: 300px;
  height: 200px;
  overflow: hidden; 
}

.slide-item-wrapper-up 
{
margin-left: 250px;
}

.slide-item-wrapper > *,
.slide-item-wrapper-up > *
{
  transition: 2s ease-in-out;
}

.box-item {
  display: flex;
  justify-content: center;
  align-items: center;
  background: red;
  color: white;
  width: 300px;
  height: 200px;
  margin-bottom: 20px;
}

.animate
{
	opacity: 0;
}
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slide-item-wrapper">
  <div>
    <div class="box-item animate --bounceInLeft">
      Lipsum
    </div>
     <div class="box-item">
      Lipsum2
    </div>
     <div class="box-item">
      Lipsum3
    </div>
     <div class="box-item">
      Lipsum4
    </div>
     <div class="box-item">
      Lipsum5
    </div>
     <div class="box-item">
      Lipsum6
    </div>
  </div>
</div>

<br>
<br>
<br>

<div class="slide-item-wrapper-up animate --bounceInRight">
  <div>
    <div class="box-item ">
      Lipsum
    </div>
     <div class="box-item">
      Lipsum6
    </div>
     <div class="box-item">
      Lipsum5
    </div>
     <div class="box-item">
      Lipsum4
    </div>
     <div class="box-item">
      Lipsum3
    </div>
     <div class="box-item">
      Lipsum2
    </div>
  </div>
</div>

<br>
<br>
<br>

<div class="slide-item-wrapper2">
  <div>
    <div class="box-item">
      Lipsum
    </div>
     <div class="box-item">
      Lipsum2
    </div>
     <div class="box-item animate --bounceInRight">
      Lipsum3
    </div>
     <div class="box-item">
      Lipsum4
    </div>
     <div class="box-item">
      Lipsum5
    </div>
     <div class="box-item animate --bounce">
      Lipsum6
    </div>
  </div>
</div>

<script>

	var win = jQuery(window);
	var slideUpMods = jQuery('.animate');
	
	function IsElementVisible (element, partial)
	{
		var jQueryt           = jQuery(element),
			jQueryw       = jQuery(window),
			viewTop       = jQueryw.scrollTop(),
			viewBottom    = viewTop + jQueryw.height(),
			_top          = jQueryt.offset().top,
			_bottom       = _top + jQueryt.height(),
			compareTop    = partial === true ? _bottom : _top,
			compareBottom = partial === true ? _top : _bottom;
    
		return ((compareBottom <= viewBottom) && (compareTop >= viewTop));
	}
	
	function runAnimations()
	{
		var isMobile = window.innerWidth < 768;		
		
		slideUpMods = jQuery('.animate');
		
  		slideUpMods.each(function(i, el) 
		{
			var el = jQuery(el);
			
			if (isMobile)
			{
				el.css('opacity', '1.0');
			}
			else
			{						
				if (IsElementVisible(el, false))
				{
					var classNames = el.attr("class").split(' ');
					
					for (var i = 0; i < classNames.length ; i++)
					{
						var clName = classNames[i];
						if (clName.startsWith('--'))
						{
							el.removeClass(clName);
							el.removeClass('animate');
							clName = clName.replace('--', '');
							el.addClass('animated'); 
							el.addClass(clName); 
						}
					};
				} 
			}
		})
	}
	
	runAnimations();

    win.scroll(function(event) 
	{  
		runAnimations();
  	});
</script>

</body>
</html> 

ghost avatar Nov 07 '18 10:11 ghost

wow.js works perfectly for me.

jarrodwhitley avatar Nov 07 '18 14:11 jarrodwhitley

Wow! Thank you! Very good tip!

ghost avatar Nov 07 '18 16:11 ghost

@rakeshmali You'll have to use some javascript for this, there's no other way. Here a small jQuery function to do this: https://gist.github.com/eltonmesquita/bd803a21f27b12a58df1

Isn't it bad practice to have javascript constantly running every time the user scrolls?

ekcom avatar Dec 14 '19 17:12 ekcom

For all who still don't have a quick solution. I'm using it this way. The basic idea for scrolling came from Sam Sehnert, Digital Fusion. This sample is quick and dirty, but ready to copy&paste and try...

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="animate.css">
<style>
body {
  margin: 100px
}

.slide-item-wrapper,
.slide-item-wrapper-up 
{
  width: 300px;
  height: 200px;
  overflow: hidden; 
}

.slide-item-wrapper-up 
{
margin-left: 250px;
}

.slide-item-wrapper > *,
.slide-item-wrapper-up > *
{
  transition: 2s ease-in-out;
}

.box-item {
  display: flex;
  justify-content: center;
  align-items: center;
  background: red;
  color: white;
  width: 300px;
  height: 200px;
  margin-bottom: 20px;
}

.animate
{
	opacity: 0;
}
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slide-item-wrapper">
  <div>
    <div class="box-item animate --bounceInLeft">
      Lipsum
    </div>
     <div class="box-item">
      Lipsum2
    </div>
     <div class="box-item">
      Lipsum3
    </div>
     <div class="box-item">
      Lipsum4
    </div>
     <div class="box-item">
      Lipsum5
    </div>
     <div class="box-item">
      Lipsum6
    </div>
  </div>
</div>

<br>
<br>
<br>

<div class="slide-item-wrapper-up animate --bounceInRight">
  <div>
    <div class="box-item ">
      Lipsum
    </div>
     <div class="box-item">
      Lipsum6
    </div>
     <div class="box-item">
      Lipsum5
    </div>
     <div class="box-item">
      Lipsum4
    </div>
     <div class="box-item">
      Lipsum3
    </div>
     <div class="box-item">
      Lipsum2
    </div>
  </div>
</div>

<br>
<br>
<br>

<div class="slide-item-wrapper2">
  <div>
    <div class="box-item">
      Lipsum
    </div>
     <div class="box-item">
      Lipsum2
    </div>
     <div class="box-item animate --bounceInRight">
      Lipsum3
    </div>
     <div class="box-item">
      Lipsum4
    </div>
     <div class="box-item">
      Lipsum5
    </div>
     <div class="box-item animate --bounce">
      Lipsum6
    </div>
  </div>
</div>

<script>

	var win = jQuery(window);
	var slideUpMods = jQuery('.animate');
	
	function IsElementVisible (element, partial)
	{
		var jQueryt           = jQuery(element),
			jQueryw       = jQuery(window),
			viewTop       = jQueryw.scrollTop(),
			viewBottom    = viewTop + jQueryw.height(),
			_top          = jQueryt.offset().top,
			_bottom       = _top + jQueryt.height(),
			compareTop    = partial === true ? _bottom : _top,
			compareBottom = partial === true ? _top : _bottom;
    
		return ((compareBottom <= viewBottom) && (compareTop >= viewTop));
	}
	
	function runAnimations()
	{
		var isMobile = window.innerWidth < 768;		
		
		slideUpMods = jQuery('.animate');
		
  		slideUpMods.each(function(i, el) 
		{
			var el = jQuery(el);
			
			if (isMobile)
			{
				el.css('opacity', '1.0');
			}
			else
			{						
				if (IsElementVisible(el, false))
				{
					var classNames = el.attr("class").split(' ');
					
					for (var i = 0; i < classNames.length ; i++)
					{
						var clName = classNames[i];
						if (clName.startsWith('--'))
						{
							el.removeClass(clName);
							el.removeClass('animate');
							clName = clName.replace('--', '');
							el.addClass('animated'); 
							el.addClass(clName); 
						}
					};
				} 
			}
		})
	}
	
	runAnimations();

    win.scroll(function(event) 
	{  
		runAnimations();
  	});
</script>

</body>
</html> 

I have used Code. It is working very well. Thank you for providing this code

amitprajapati2298 avatar Feb 08 '20 07:02 amitprajapati2298