parish parab parish parab - 6 months ago 47
Javascript Question

animate image left to right on scroll

what i am trying to do is i have around 6 inline images i want slide them left to right on specific position and stop there for each image.And images have to slide at the time the scrool comes over them.
i tried this javascript for it (totally new to JS)

$(window).scroll(function(){
if($this.scrollTop()>300)
{
$('.onfoot1').slideright();
}
function slideright(){
var a = getElementsByClassName('.onfoot1');
var stoppos = 100;
if (parseInt(a.style.left)< stoppos )
{
a.style.left = parseInt(a.style.left) + 3 + "px";
setTimeout(slideright , 1);
}
}
});


Markup

<div class="onfoot1"></div>


CSS

div.onfoot1{
content:url(../img/onfoot1.jpg);
left:0;

}

Answer

I've put together a working examle for your code: https://jsfiddle.net/hmzw9y65/

I've made a few assumptions there... You are using $(...) syntax so I guessed you are using JQuery. JQuery has a .animate() function which should do the trick (http://api.jquery.com/animate/). Also I guessed that you may want to make the css-position of the div fixed so it stays on screen when you scroll.

EDIT: I noticed that you don't want you image on the bottom of the screen but animating when screen reaches it. Updated my fiddle to do that: https://jsfiddle.net/hmzw9y65/1/