iOS CSS 不透明度 +可见性过渡

发布于 2024-12-06 13:41:40 字数 785 浏览 1 评论 0原文

在桌面浏览器 (JSFiddle) 中查看以下测试:

a {
  background: gray;
  display: block;
  margin: 100px;
  padding: 100px;
}
a span {
  opacity: 0;
  -webkit-transition: 0.5s;
  visibility: hidden;
}
a:hover span {
  opacity: 1;
  -webkit-transition: 0.5s;
  visibility: visible;
}
<a href="#">a <span>span</span></a>

将鼠标悬停在锚元素上,跨度元素就会像它应该的那样淡入。

现在通过 iOS 设备查看一下。 结果:什么也没做。

事实:

  • 如果没有过渡属性,它就可以工作。
  • 如果不透明度或可见性属性不存在,它就可以工作。
  • 不透明度或可见性属性不会触发 webkitTransitionEnd 事件。

有什么解决方法吗?

Take a look at the following test in a desktop browser (JSFiddle):

a {
  background: gray;
  display: block;
  margin: 100px;
  padding: 100px;
}
a span {
  opacity: 0;
  -webkit-transition: 0.5s;
  visibility: hidden;
}
a:hover span {
  opacity: 1;
  -webkit-transition: 0.5s;
  visibility: visible;
}
<a href="#">a <span>span</span></a>

You hover over the anchor element and the span element fades in like it should.

Now take a look via an iOS device.
Result: it does nothing.

Facts:

  • If the transition property is absent, it works.
  • If either the opacity or visibility property is absent, it works.
  • There is no webkitTransitionEnd event being fired for the opacity or visibility property.

Is there any workaround?

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

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

发布评论

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

评论(2

晌融 2024-12-13 13:41:40

通过对 transition 属性进行一些小的修改,这可以在 iOS 上运行。在 :hover 上,将 transition 限制为仅 opacity 属性,如下所示:

a:hover span {
    opacity:1;
    -webkit-transition: opacity 0.5s;
    transition: opacity 0.5s;
    visibility:visible;
}​

While 可见性是一个可动画属性,似乎有一个错误iOS 实现。当您尝试转换可见性时,整个转换似乎并未发生。如果您只是将过渡限制为不透明度,事情就会按预期进行。

需要明确:将 visibility 属性保留在 CSS 中,如果您希望在 Mobile Safari 中正常运行,请不要尝试转换它。

作为参考,这是我在 iPad 上测试的更新后的 fiddle

a {
  background: gray;
  display: block;
  margin: 100px;
  padding: 100px;
}
a span {
  opacity: 0;
  -webkit-transition: 0.5s;
  visibility: hidden;
}
a:hover span {
  opacity: 1;
  -webkit-transition: opacity 0.5s;
  visibility: visible;
}
<a href="#">a <span>span</span></a>

With some minor modifications to the transition property, this can work on iOS. On :hover, limit the transition to only the opacity property, like so:

a:hover span {
    opacity:1;
    -webkit-transition: opacity 0.5s;
    transition: opacity 0.5s;
    visibility:visible;
}​

While visibility is an animatable property, there seems to be a bug in the iOS implementation. When you try to transition visibility, it seems like the entire transition doesn't happen. If you simply limit your transition to opacity, things work as expected.

To be clear: Leave the visibility property in your CSS, just don't try to transition it if you want things to work in Mobile Safari.

For reference, here's your updated fiddle, which I tested on an iPad:

a {
  background: gray;
  display: block;
  margin: 100px;
  padding: 100px;
}
a span {
  opacity: 0;
  -webkit-transition: 0.5s;
  visibility: hidden;
}
a:hover span {
  opacity: 1;
  -webkit-transition: opacity 0.5s;
  visibility: visible;
}
<a href="#">a <span>span</span></a>

骑趴 2024-12-13 13:41:40

只有过渡时的不透明度很糟糕。

由于在 iPhone 上您需要点击才能聚焦某个元素,这就是我解决问题的方法:

.mydiv {
        visibility:hidden;
        opacity: 0;
        transition: all 1s ease-out; 
        -webkit-transition: all 1s ease-out;
        -moz-transition: all 1s ease-out;
        -o-transition: all 1s ease-out;
}
.mydiv:hover {
            visibility:visible;
            opacity: 1;
}
.mydiv:active {
            -webkit-transition: opacity 1s ease-out;
}

我已将不透明度过渡添加到 :active 中。

这样,它适用于 Chrome、Firefox 和 iPhone(点击)上的所有过渡动画(考虑到您想要将动画应用到高度或其他内容)。

感谢 Grezzo 和 Michael Martin-Smucker 发现了不透明过渡。

(复制/粘贴我的回复 CSS 动画可见性:可见;适用于 Chrome 和 Safari,但不适用于 iOS

Only opacity on transition sucks.

Since that on iPhone you need to tap in order to focus an element this is how I've fixed my problem:

.mydiv {
        visibility:hidden;
        opacity: 0;
        transition: all 1s ease-out; 
        -webkit-transition: all 1s ease-out;
        -moz-transition: all 1s ease-out;
        -o-transition: all 1s ease-out;
}
.mydiv:hover {
            visibility:visible;
            opacity: 1;
}
.mydiv:active {
            -webkit-transition: opacity 1s ease-out;
}

I've added the opacity transition to :active.

This way it works with all transition animation (consider that you want to apply animation to height or something else) on Chrome, Firefox and iPhone (on tap).

Thanks to Grezzo and Michael Martin-Smucker for spotting out about the opacity transition.

(copy/paste of my response from CSS animation visibility: visible; works on Chrome and Safari, but not on iOS)

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