Home > Article > Web Front-end > JS implements display/hide window fixed position elements as the page scrolls_javascript skills
The element is displayed at a fixed position in the window. When the page height is greater than a certain height and the page is scrolled down, the element is displayed; when the page position is less than a certain height or the page is scrolled up, the element is hidden.
Let me show you the renderings first:
1.html
<p id="selected-case-count"><span class='form-control'>已选: <span class="casecount">0</span></span></p>
2.css
p#selected-case-count{ position:fixed; /*固定元素位置*/ top:2px; /*距顶端举例*/ right:40px; /*距右侧位置*/ color:red; }
3.js
$(function() { $("#selected-case-count").hide(); }); var preTop=0; var currTop=0; $(function () { $(window).scroll(function(){ currTop=$(window).scrollTop(); if(currTop<preTop){ $("#selected-case-count").fadeOut(200); }elseif ($(window).scrollTop()>600){ $("#selected-case-count").fadeIn(500); }else{ $("#selected-case-count").fadeOut(500); } preTop=$(window).scrollTop(); }); });
The above is the knowledge that the editor has shared with you about JS implementation of displaying/hiding fixed-position elements in windows as the page scrolls. I hope it will be helpful!