2
I’m trying to create an animation for a div after the scroll is higher than a value (in my case, 1100).
It works to some extent, but after it goes past 1100, every time you move the scroll this effect rotates again.
Here is my code:
<script type="text/javascript">
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j(window).bind('scroll', function(){
if($j(this).scrollTop() > 1100)
$j('.testing')
.css("position","fixed")
.css("top","10px")
.animate({marginTop: '30px'}, 500)
.animate({marginTop: '10px'}, 500);
else
$j('.testing')
.css("position","relative")
.css("top","0")
.animate({marginTop: '0px'}, 500);
});
});
</script>
.testing is my div and whenever the scroll passes 1100 it performs almost infinitely.
My intention is that when you get to 1100, it will run the animation and at the end of it, stay static with the position Fixed until the scroll is less than 1100.
I believe I did, more or less, what you explained, but I think I did something wrong. I edited the post to exclaim better, thanks.
– Luna Laura
@Lunalaura has some error in the console?
– Sergio
If you are referring to the console error on the page, there is no.
– Luna Laura
@Lunalaura why it has 2x times the animation?
.animate({marginTop: '30px'}, 500)
. You can use the callback on this one with30px
and remove the other line?– Sergio
It’s because I wanted to make an elastic effect, you know? then the marginTop would vary up and down to make that effect, but I tried only with the . Animate with callback and still has the same behavior.
– Luna Laura
@Lunalaura if you put your HTML (or a jsFiddle) I can put an online example with the problem solved.
– Sergio
@Lunalaura I am now back at the computer and I think you have to change your
else
for something likeelse if($j(this).scrollTop() <= 1100) $j('.testing')
because this new flag interferes with how theif
works... take a look here: http://jsfiddle.net/dqpqh3y9/– Sergio
This is amazing, but when the scroll comes back up, the effect stops running and if using the scroll down often the effect happens repeatedly, I wanted to make it repeat only once, after reaching a certain height. Could you help me? I don’t know how to fix this.
– Luna Laura
@Lunalaura whatever happens when the scroll comes back up?
– Sergio
The idea is as follows: Whenever it reaches 1100 it performs the effect, even making the scroll go up and down several times. To explain further: I would like to replicate the effect of this lateral column website.
– Luna Laura
@Lunalaura then wants something like this: http://jsfiddle.net/ntb9cncn/1/ ?
– Sergio
sorry for the delay, but your code was excellent, I just made some modifications so it would look like I wanted and it worked really well =) Look what I did: http://jsfiddle.net/Luna_Laura/qnvspv0y/3/ It was just so you could see graphically. The only problem now is that I can’t get . testing to respect the footer, in which case, don’t go behind or forward. I wanted when he reached the edge of the footer, he would stop right there, as in this [site] (http://www.ahnegao.com.br/).
– Luna Laura
This code you sent is not working
– Luna Laura
@Lunalaura but works as you want in jsFiddle: http://jsfiddle.net/px5Ls8q7/ ?
– Sergio
YES! Perfect. I’ll just make some modifications. Thanks!!
– Luna Laura
Hi Sergio, sorry to have resurrected the topic, but is that I was testing the code here and discovered a bug... For some reason, the box that gets fixed does not respect the limit, sometimes it goes down too much, sometimes it gets higher... I tried many things here but I could not, could you help me? Obs.: I tested it on my friend’s blog which is that, can check the behavior of the box that follows beyond the pages (1, 2, 3)... The behavior varies too
– Luna Laura
@Lunalaura took a look at the blog and I don’t see the problem. So the box
publi-left contour publi-fixed
is fixed after 2100px and seems to work well. Can you explain better what I should look for error? and which browser is?– Sergio
I use Chrome, but note that "publish-Fixed" is not on the same line as the last article, sometimes it is higher and sometimes lower, if you can look at the pages, you’ll see... That’s what I wanted to fix, you know?
– Luna Laura