{"id":32723,"date":"2016-02-24T03:48:11","date_gmt":"2016-02-23T22:18:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=32723"},"modified":"2016-02-24T03:48:11","modified_gmt":"2016-02-23T22:18:11","slug":"slide-in-animation-while-scrolling-page","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/slide-in-animation-while-scrolling-page\/","title":{"rendered":"Slide In  animation while scrolling page"},"content":{"rendered":"<p>Nowadays there is a trend of using animated page elements where as you scroll down the page, items animates. Animated page elements provides a more extraordinary user experience.<\/p>\n<p>Here we will be building UI elements which will animate into view as the user scrolls down the page. The animation will be performed by CSS3 while scrolling will be done by jQuery.<\/p>\n<p>To start off, let\u2019s create a html layout of the page.<\/p>\n<p>[html]<br \/>\n&lt;div id=&quot;wrapper&quot;&gt;<br \/>\n  &lt;img src=&quot;http:\/\/placehold.it\/750&#215;300&quot; \/&gt;<br \/>\n  &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br \/>\nSed faucibus, augue ac maximus auctor, dui elit ornare libero,<br \/>\nat maximus sem mauris lacinia eros. Pellentesque turpis dolor,<br \/>\n aliquet eu eros vitae, congue volutpat orci. Nullam vel vulputate<br \/>\n    odio. Sed vestibulum felis vel convallis consequat.<br \/>\n Cras a lacinia ante.<br \/>\n  &lt;\/p&gt;<br \/>\n  &lt;div class=&quot;notAnimated animateBlock leftAlign left&quot;&gt;<br \/>\n    &lt;img src=&quot;http:\/\/placehold.it\/382&#215;152&quot; alt=&quot;apple devices&quot;&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n  &lt;div id=&quot;Txt&quot; class=&quot;notAnimated animateBlock rightAlign right&quot;&gt;<br \/>\n    &lt;h3&gt;Lorem ipsum&lt;\/h3&gt;<br \/>\n    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed<br \/>\n faucibus, augue ac maximus auctor, dui elit ornare libero, at maximus<br \/>\n sem mauris lacinia eros. Pellentesque turpis dolor, aliquet eu eros<br \/>\nvitae, congue volutpat orci. Nullam vel vulputate<br \/>\n      odio. Sed vestibulum felis vel convallis consequat. Cras a lacinia<br \/>\n ante.<br \/>\n    &lt;\/p&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n  &lt;div class=&quot;notAnimated animateBlock right&quot;&gt;<br \/>\n    &lt;img src=&quot;http:\/\/placehold.it\/750&#215;400&quot; alt=&quot;white iphone flat ui&quot;&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;!&#8211; wrapper &#8211;&gt;<br \/>\n&lt;script src=&quot;https:\/\/code.jquery.com\/jquery-1.12.0.js&quot;&gt;&lt;\/script&gt;<br \/>\n[\/html]<\/p>\n<p>Now that the HTML is in place, we can use CSS to arrange, animate and decorate each element.<\/p>\n<p>[css]<br \/>\n* {<br \/>\n  margin: 0;<br \/>\n  padding: 0;<br \/>\n}<\/p>\n<p>html {<br \/>\n  overflow-y: scroll;<br \/>\n}<\/p>\n<p>body {<br \/>\n  background: #f0f0f0;<br \/>\n  color: #000;<br \/>\n}<\/p>\n<p>img {<br \/>\n  border: 0;<br \/>\n  max-width: 100%;<br \/>\n}<\/p>\n<p>#wrapper {<br \/>\n  display: block;<br \/>\n  width: 750px;<br \/>\n  margin: 0 auto;<br \/>\n  padding-top: 30px;<br \/>\n  padding-bottom: 45px;<br \/>\n}<\/p>\n<p>.rightAlign {<br \/>\n  float: right;<br \/>\n}<\/p>\n<p>.leftAlign {<br \/>\n  float: left;<br \/>\n}<\/p>\n<p>.animateBlock {<br \/>\n  margin-top: 20px;<br \/>\n  display: inline-block;<br \/>\n  opacity: 0;<br \/>\n  filter: alpha(opacity=0);<br \/>\n  position: relative;<br \/>\n  -webkit-transition: all .55s ease-in;<br \/>\n  -moz-transition: all .55s ease-in;<br \/>\n  -ms-transition: all .55s ease-in;<br \/>\n  -o-transition: all .55s ease-in;<br \/>\n  transition: all .55s ease-in;<br \/>\n}<\/p>\n<p>.animateBlock.left {<br \/>\n  left: -20%;<br \/>\n}<\/p>\n<p>.animateBlock.right {<br \/>\n  right: -20%;<br \/>\n}<\/p>\n<p>.left.animated {<br \/>\n  left: 0%;<br \/>\n  opacity: 1;<br \/>\n  filter: alpha(opacity=100);<br \/>\n}<\/p>\n<p>.right.animated {<br \/>\n  right: 0%;<br \/>\n  opacity: 1;<br \/>\n  filter: alpha(opacity=100);<br \/>\n}<\/p>\n<p>#Txt {<br \/>\n  width: 300px;<br \/>\n  padding-left: 15px;<br \/>\n  padding-top: 12px;<br \/>\n  text-align: center;<br \/>\n}<\/p>\n<p>.clearfix:after {<br \/>\n  content: &quot;.&quot;;<br \/>\n  display: block;<br \/>\n  clear: both;<br \/>\n  visibility: hidden;<br \/>\n  line-height: 0;<br \/>\n  height: 0;<br \/>\n}<\/p>\n<p>.clearfix {<br \/>\n  display: inline-block;<br \/>\n}<\/p>\n<p>html[xmlns] .clearfix {<br \/>\n  display: block;<br \/>\n}<\/p>\n<p>* html .clearfix {<br \/>\n  height: 1%;<br \/>\n}<\/p>\n<p>  [\/css]<\/p>\n<p>The <strong>.leftAlign<\/strong> and <strong>.rightAlign<\/strong> classes represent floated elements to the left and right, respectively.<strong> .animateBlock<\/strong> elements are hidden by default using zero opacity and we have added transition property on it for animation.<\/p>\n<p>Now the final step is to bind the scroll event and exchanging classes with the help of JQuery. Once an element is inside our viewport, we will replace the class <strong>.notAnimated<\/strong> with <strong>.animated<\/strong><\/p>\n<p>[javascript]<br \/>\n$(function() {<br \/>\n  var $elements = $(&#8216;.animateBlock.notAnimated&#8217;); \/\/contains elements of nonAnimated class<br \/>\n  var $window = $(window);<br \/>\n  $window.on(&#8216;scroll&#8217;, function(e) {<br \/>\n    $elements.each(function(i, elem) { \/\/loop through each element<br \/>\n      if ($(this).hasClass(&#8216;animated&#8217;)) \/\/ check if already animated<br \/>\n        return;<br \/>\n      animateMe($(this));<br \/>\n    });<br \/>\n  });<br \/>\n});<\/p>\n<p>function animateMe(elem) {<br \/>\n  var winTop = $(window).scrollTop(); \/\/ calculate distance from top of window<br \/>\n  var winBottom = winTop + $(window).height();<br \/>\n  var elemTop = $(elem).offset().top; \/\/ element distance from top of page<br \/>\n  var elemBottom = elemTop + $(elem).height();<br \/>\n  if ((elemBottom &lt;= winBottom) &amp;&amp; (elemTop &gt;= winTop)) {<br \/>\n    \/\/ exchange classes if element visible<br \/>\n    $(elem).removeClass(&#8216;notAnimated&#8217;).addClass(&#8216;animated&#8217;);<br \/>\n  }<br \/>\n}<\/p>\n<p>[\/javascript]<\/p>\n<p><a href=\"https:\/\/jsfiddle.net\/L3bgwhcs\/\">Fiddle\u00a0link<\/a><\/p>\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nowadays there is a trend of using animated page elements where as you scroll down the page, items animates. Animated page elements provides a more extraordinary user experience. Here we will be building UI elements which will animate into view as the user scrolls down the page. The animation will be performed by CSS3 while [&hellip;]<\/p>\n","protected":false},"author":905,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"status","meta":{"iawp_total_views":6},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32723"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/905"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=32723"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32723\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=32723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=32723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=32723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}