1
votes

I want a div (a menu) to appear when the users scrolls, then fadeout after a brief time so it does not interfere with the reading, then appear again if the user scrolls. So far, the following code does the job, but I want a fadein effect when the div appears.

CSS

.onscrollbutton {
  display: none;
}

jQuery

<script>
  $(window).scroll(function(){
    $(".onscrollbutton").css("display","block").delay(5000).fadeOut("slow");
    });
</script>

Erasing the CSS and changing the jQuery code to this results in a fadein/fadeout loop, even if the user does not scroll:

<script>
  $(window).scroll(function(){
    $(".onscrollbutton").fadeIn("slow").delay(5000).fadeOut("slow");
    });
</script>

How can I achieve the first effect, but with a smooth transition?

1
Sorry, I want the div (a menu) to appear every time the user scrolls. In case the user does not scroll for 5 seconds, the div fades out so it does not interfere with the user's reading. I will edit my question. The thing is the loop on the second code happens even if the user does not scroll.Ricardo Ortiz
Did you wrap that script in a ready? $(function(){}); I ask this only because it is possible that the page length is so long that is creates a scroll bar and triggers the script.Adrianopolis

1 Answers

0
votes

fadeOut is causing the scroll event to trigger because it is basically removing the height of the .onscrollbutton element. Try absolute positioning the element and it should work. Worked for me.