Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What is happening is that you have two functions working against each other: </p> <p>The first function has an "if-else" statement and the second function as well. That means that each function does something EVERYTIME you scroll. There are multiple ways of solving this.</p> <p>The way I would solve it is using a variable and updating the constraints. </p> <p>Let's say we have a variable onScreen that has value 1 if the paragraph is on the screen and value 0 if it isn't:</p> <p>For example:</p> <pre><code>&lt;div style="height: 800px"&gt;Example of scroll with fadeIn, fadeOut. &lt;p style="margin-top:300px;"&gt;These paragraphs will go away when you have scrolled more than 10 pixels from the top. They will appear again when you have scrolled to a proximity of 50 pixels from the bottom. They will also appear if you go within a proximity of 10 pixels of the top again.&lt;/p&gt; &lt;/div&gt; </code></pre> <p>Now for the jQuery code:</p> <pre><code>var $onScreen = 1; $(window).scroll(function(){ if($(window).scrollTop() &lt; 10){ if ($onScreen == 0) { $("p:first").stop(true,true).fadeIn("slow", "linear"); $onScreen = 1; } } if($(window).scrollTop() &lt;= 20 &amp;&amp; $(window).scrollTop() &gt;= 10){ if ($onScreen == 1) { $("p:first").stop(true,true).fadeOut("slow", "linear"); $onScreen = 0; } } if ($(window).scrollTop() + $(window).height() &gt;= $(document).height() - 50) { if ($onScreen == 0) { $("p:first").stop(true,true).fadeIn("slow", "linear"); $onScreen = 1; } } }); </code></pre> <p>Now this is not the most concise way of doing it and I didn't mean to do so: by making the code a bit more extensive I hope you can follow why it works now and didn't work before (such that you actually learn from it).</p> <p>I prepared a live example for you on Fiddle: <a href="http://jsfiddle.net/ycCAb/4/" rel="nofollow">http://jsfiddle.net/ycCAb/4/</a></p> <p>I hope this answers your question. Good luck!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload