All you have to do is reset the the appropriate ‘right’ or ‘left’ property when changing the latter. The ‘left’ and ‘right’ CSS properties are different so if you don’t reset one of them, the other will be ignored.
You can do that by using the ‘intial’ value for the ‘left’ and ‘right’ properties:
I have 3 sections. Each section has their height. JSFiddle at the end of the question.
GOAL: I want an img (rocket picture) to follow the user as he scrolls and change positions (slide) from right to left when he scrolls pass a section.
I’ve managed to make the rocket follow me as I scroll down. Now I want the rocket to slide over to the left when I pass the first section, then slide back right when I pass the second section.
The rocket rotates when the user scrolls pass a section. That works great! But when I want it to slide
over to a different side, it only works on the left. I can’t get it to go right Here is the jQuery part for moving the rocket :
$(document).scroll(function() {
var scrollIs = $(window).scrollTop();
console.log("I've scrolled: ", scrollIs);
if($(window).scrollTop() >= topOfSecondSection - 230){ //passed first section
$(".rocket").css('transform', 'rotate(50deg)'); //turn rocket WORKS
//$(".rocket").css('left', '10px'); //JUMP CUTS THE ROCKET TO LEFT SIDE, DOESN'T SLIDE/MOVE IT
$(".rocket").stop().animate({ "left": "10px"}, 100); //ISN'T INSTANT-SMOOTH
$("#first-section").css("background","red");
$("#second-section").css("background","blue");
$("#third-section").css("background","orange");
}
if($(window).scrollTop() >= topOfThirdSection - 150){ //passed second section
$(".rocket").css('transform', 'rotate(1deg)'); //turns rocket back to look straight WORKS
//$(".rocket").css('right', '10px'); //move rocket right DOESNT'T MOVE
$(".rocket").stop().animate({ "right": "10px"}, 100); //DOESN'T MOVE
$("#first-section").css("background","purple");
$("#second-section").css("background","yellow");
$("#third-section").css("background","brown");
}
//DEFAULT
if($(window).scrollTop() < s1Height){
//return to normal
$(".rocket").css('transform', 'rotate(1deg)'); //WORKS
//$(".rocket").css('right', '10px'); //DOESN'T MOVE
$(".rocket").stop().animate({ "right": "10px"}, 100); //DOESN'T MOVE
$("#first-section").css("background","green");
$("#second-section").css("background","grey");
}
});
Didn’t really consider it for a simple animate scroll left-right
Comment posted by Akhil Aravind
this is a wrong answer, please check before posting’
Comment posted by Syll
Have you looked at the JSFiddle?
Comment posted by Akhil Aravind
yes, I do. just do a single scroll and check, rocket will be placed above the text content. check the last section, rocket is missing. As of my understanding, rocket should be in the empty region, but now its all over the content. Did you get a clear picture now.
Comment posted by Syll
I just showed OP how to move the rocket from left to right and vise-versa with animation. That’s what was asked.
Comment posted by Akhil Aravind
Rocket is not even visible
Comment posted by IkePr
@AkhilAravind Make the result screen bigger resolution
Comment posted by Akhil Aravind
now what s the difference between your question and ans except
Comment posted by i.imgur.com/0HJ5eyQ.png
The rocket moves left/right as I scroll, that is the end goal. Here is how I see the solution: