Create an animation effect that runs only once, after a certain bearing height

Asked

Viewed 244 times

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.

1 answer

1


You can create a "flag" variable that stores the state of the animation to know if the element has already been animated. You can also compare the footer position on the page to get back position: absolute in case he’s bumping into the footer.

Suggestion:

(Example: http://jsfiddle.net/px5Ls8q7/)

var $j = jQuery.noConflict();
$j(document).ready(function() {
    var animationCompleted = false;
    var posicaoFooter = $j('#footer').offset().top;
    var posicaoFinalContent = $j('#content').position().top + $j('#content').outerHeight(
        true);
    var sidebarDiv = $j('.testing');
    $j(window).bind('scroll', function() {
        var scroll = $j(this).scrollTop();
        if (scroll + sidebarDiv.height() >= posicaoFooter) {
            sidebarDiv.css({
                position: 'absolute',
                top: posicaoFinalContent - sidebarDiv.height()
            })
            return;
        } else if (animationCompleted) {
            sidebarDiv.css("position", "fixed").css('top', '0')
        }
        if (scroll > 1150 && !animationCompleted) {
            sidebarDiv.css("position", "fixed").css('top', '0')
                .animate({
                    marginTop: '-40px'
                }, 100, function() {
                    sidebarDiv.animate({
                        marginTop: '20px'
                    }, 125).animate({
                        marginTop: '0px'
                    }, 150).animate({
                        marginTop: '10px'
                    }, 175);
                })
            animationCompleted = true
        } else if (scroll < 1156 && animationCompleted) {
            animationCompleted = false;
            sidebarDiv.css("position", "relative").stop().animate({
                marginTop: '-30px'
            }, 150).animate({
                marginTop: '15px'
            }, 150).animate({
                marginTop: '-7px'
            }, 150).animate({
                marginTop: '3px'
            }, 150).animate({
                marginTop: '0px'
            }, 150);
        }
    });
});
  • 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.

  • @Lunalaura has some error in the console?

  • If you are referring to the console error on the page, there is no.

  • @Lunalaura why it has 2x times the animation? .animate({marginTop: '30px'}, 500). You can use the callback on this one with 30px and remove the other line?

  • 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.

  • @Lunalaura if you put your HTML (or a jsFiddle) I can put an online example with the problem solved.

  • @Lunalaura I am now back at the computer and I think you have to change your else for something like else if($j(this).scrollTop() <= 1100) $j('.testing') because this new flag interferes with how the if works... take a look here: http://jsfiddle.net/dqpqh3y9/

  • 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.

  • @Lunalaura whatever happens when the scroll comes back up?

  • 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.

  • @Lunalaura then wants something like this: http://jsfiddle.net/ntb9cncn/1/ ?

  • 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/).

  • This code you sent is not working

  • @Lunalaura but works as you want in jsFiddle: http://jsfiddle.net/px5Ls8q7/ ?

  • 1

    YES! Perfect. I’ll just make some modifications. Thanks!!

  • 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

  • @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?

  • 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?

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.