当前位置:网站首页>Issue 003 how to detect whether a sticky positioned element is in a pinned state
Issue 003 how to detect whether a sticky positioned element is in a pinned state
2022-06-24 17:07:00 【Front end gogogogo】
Scene description
sticky Elements of positioning , There are two states : Relative positioning and fixed positioning . When developing , There is a fixed position (Pined) state sticky Positioned elements plus additional styling requirements . Such as adding a shadow effect .
at present , It's impossible to pass CSS know sticky Whether it is in the state of fixed positioning .
Solution
For this scenario , It can be used JS Realization .
Determine whether the element is in a fixed positioning state , It is to judge the position relationship between this element and the scrolling parent element .
When the element part is in the fixed positioning state , It is not visible relative to the parent element portion of the scroll . It can be used Intersection Observer To listen for the positional relationship between this element and the scrolling parent element .
The following is the specific code implementation :
intersectionRatio: The visible proportion of the target element , namely intersectionRect Occupy boundingClientRect The proportion of , When fully visible 1, Less than or equal to when completely invisible 0.
threshold1: List of listening thresholds , In ascending order , Each threshold in the list is the ratio of the cross region to the boundary region of the listener . When fully visible 1, Less than or equal to when completely invisible 0.
// Target element
const el = document.querySelector(".myElement")
const observer = new IntersectionObserver(
([e]) =>
e
.target
.classList
.toggle(
"is-pinned",
e.intersectionRatio < 1
),
{
threshold: [1]
}
)
// monitor
observer.observe(el)/* sticky Elements */
.myElement {
position: sticky;
top: -1px;
}
/* Style of fixed positioning state */
.is-pinned {
color: red;
}If you are in a fixed position sticky Element shading , Yes CSS Solutions for : Shadowy CSS Sticky.
Reference resources
边栏推荐
- Markdown syntax -- Formula
- Abstract factory pattern
- Learn typescript with VAM (phase 1)
- Explore cloudera manager management software tuning (1)
- How to compile and debug go runtime source code
- Druid architecture and Implementation
- New MySQL 8.0 feature - enhanced logical backup recovery
- Activeindex selection and redirection in the menu bar on the right of easycvs
- Clickhouse high performance column storage core principle
- 构建跨公链平台解决DApp开发问题
猜你喜欢

A survey on model compression for natural language processing (NLP model compression overview)
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)

A survey on dynamic neural networks for natural language processing, University of California

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)

A survey of training on graphs: taxonomy, methods, and Applications

Why do you develop middleware when you are young? "You can choose your own way"

MySQL learning -- table structure of SQL test questions
随机推荐
FPGA project development: experience sharing of lmk04821 chip project development based on jesd204b (I)
Elastic searchable snapshot function (frozen Tier 3)
让UPS“印象派用户”重新认识可靠性
Introduction to koa (II) building the koa program
Audio knowledge (I)
Can you remember the code of a programming boss? Can you hit it out without Baidu?
Will the easycvr video channel of the urban intelligent video monitoring image analysis platform occupy bandwidth after stopping playing?
Introduction to visual studio shortcut keys and advanced gameplay
The RTSP video structured intelligent analysis platform easynvr stops calling the PTZ interface through the onvif protocol to troubleshoot the pending status
With the solution, the nickname of the applet suddenly becomes "wechat user", and the avatar cannot be displayed?
Experience and suggestions on cloud development database
Easycvr, an urban intelligent video monitoring image analysis platform, plays national standard equipment videos and captures unstable packets for troubleshooting
[tke] nodelocaldnschache is used in IPVS forwarding mode
zblog判断某个插件是否安装启用的内置函数代码
Page scrolling effect library, a little skinny
Markdown syntax -- Formula
A survey of training on graphs: taxonomy, methods, and Applications
A survey on model compression for natural language processing (NLP model compression overview)
Tencent security officially released the IOT security capability map
New MySQL 8.0 feature - enhanced logical backup recovery