Home > Web Front-end > JS Tutorial > Use JS to display/hide window fixed position elements as the page scrolls

Use JS to display/hide window fixed position elements as the page scrolls

不言
Release: 2018-06-25 10:23:07
Original
2185 people have browsed it

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. This article will introduce it to you below. JS realizes displaying/hiding the window fixed position element as the page scrolls. Friends who need it can refer to it

Window fixed position display element. When the page height is greater than a certain height and the page scrolls down, the element is displayed; when The element is hidden when the page position is less than a certain height, or when the page scrolls upward.

Let me show you the renderings first:

1.html

<p id="selected-case-count"><span class=&#39;form-control&#39;>已选: <span class="casecount">0</span></span></p>
Copy after login

2.css

p#selected-case-count{
position:fixed; /*固定元素位置*/
top:2px; /*距顶端举例*/
right:40px; /*距右侧位置*/
color:red; 
}
Copy after login

##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();
});
});
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to implement three-level linkage using Vue2

How to use singly linked lists and circular linked lists in JavaScript

How to solve the problem of pull-down refresh at the top of mobile browser

##

The above is the detailed content of Use JS to display/hide window fixed position elements as the page scrolls. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template