Verify that $(document).scrollTop()
returns 0. If you return 0, it is because it is at the top of the page. If you do not want to use jQuery, use document.documentElement.scrollTop
.
If the page is at the top, hide the <div id="um-id">
, but if the page scrolls down, div
appears.
The scroll
event can be used for this. See the code below:
$(document).scroll(function() {
$("#um-id").toggle($(document).scrollTop() !== 0);
});
.bla {
height: 200px;
}
#um-id {
background-color: red;
position: fixed;
display: none;
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script><divid="um-id">Teste</div>
<p class="bla">blablablabla</p>
<p class="bla">dadadada</p>
<p class="bla">qwaqwqaqwqa</p>
If the page loads and rolls directly to an element with id
, such as exemplo.com/#rodape
, and then the user scrolls to the top, an alert appears.
In this one, you have to use the elemento.offset().top
property to help. See the code below:
var rodape = $("#rodape");
$(document).scrollTop(rodape.offset().top);
$(document).scroll(function() {
$("#alerta").toggle($(document).scrollTop() < rodape.offset().top);
});
.bla {
height: 200px;
}
#alerta {
background-color: red;
position: fixed;
display: none;
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script><divid="alerta">Teste</div>
<p class="bla">blablablabla</p>
<p class="bla">dadadada</p>
<p class="bla" id="rodape">AQUI!</p>
<p class="bla">qwaqwqaqwqa</p>