如何根据浏览器API功能的存在应用CSS样式?

发布于 2025-01-28 19:13:56 字数 480 浏览 3 评论 0 原文

我有一个页面,该页面具有不可见的(不透明度= 0),并且基于 istertersectectect 属性,因为用户向下滚动不透明度变化为1。它非常简单,并且是对互隔板的基本实现。

但是,我在Safari支持方面遇到了一些问题,也许像我这样的其他人正在使用较旧的浏览器。因此,我在下面的JS文件中使用了功能检测

if ("IntersectionObserver" in window){
    
    // Javascript here
}

,这对于更早工作的浏览器来说正常工作。但是,我的CSS仍然具有不透明度为0,我需要向那些不支持IntersectionObserver的浏览器展示该内容为1。

我该怎么做?

,换句话说,有什么方法可以在CSS中应用条件,即使有的话,我该如何将JS变量放入CSS?

是否会不胜感激。

I have a page which has the content invisible (opacity = 0) and based on the isIntersecting property as the user scrolls down the opacity changes to 1. It's pretty simple and a basic implementation of the IntersectionObserver API.

However I was having some issue with Safari support and maybe there are others like me who are using older browsers. So I used feature detection in the JS file as below

if ("IntersectionObserver" in window){
    
    // Javascript here
}

And this works fine for the browsers where it was working even earlier. However my CSS still has the opacity as 0 and I need to show that content with opacity as 1 to those browsers who don't support IntersectionObserver..

How do I do this?

In other words, is there a way where I can apply conditions in CSS and even if there is, then how do I take a JS variable into CSS?

Any advise would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生路 2025-02-04 19:13:56

交叉点观察者不支持iOS 12下方,因此对于较旧的版本,您可以使用,然后无需更改其他CSS或其他任何内容。

Intersection Observer isn't supported below iOS 12, so for the older versions you can use https://www.npmjs.com/package/intersection-observer, and then no need for additional CSS changing or anything.

雨的味道风的声音 2025-02-04 19:13:56

您可以使用CSS变量设置不透明度并在JS中更改该不透明度。

if ("IntersectionObserver" in window){
    
    // Javascript here
}
else {
  document.querySelector('.content').style.setProperty('--opacity', 1);
}
.content {
  --opacity: 0;
  opacity: var(--opacity);
}
<div class="content">some content</div>

You could set the opacity using a CSS variable and change that in the JS.

if ("IntersectionObserver" in window){
    
    // Javascript here
}
else {
  document.querySelector('.content').style.setProperty('--opacity', 1);
}
.content {
  --opacity: 0;
  opacity: var(--opacity);
}
<div class="content">some content</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文