iOS CSS 不透明度 +可见性过渡
在桌面浏览器 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过对
transition
属性进行一些小的修改,这可以在 iOS 上运行。在:hover
上,将transition
限制为仅opacity
属性,如下所示:While 可见性是一个可动画属性,似乎有一个错误iOS 实现。当您尝试转换
可见性
时,整个转换似乎并未发生。如果您只是将过渡限制为不透明度
,事情就会按预期进行。需要明确:将
visibility
属性保留在 CSS 中,如果您希望在 Mobile Safari 中正常运行,请不要尝试转换它。作为参考,这是我在 iPad 上测试的更新后的 fiddle:
With some minor modifications to the
transition
property, this can work on iOS. On:hover
, limit thetransition
to only theopacity
property, like so: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 toopacity
, 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:
只有过渡时的不透明度很糟糕。
由于在 iPhone 上您需要点击才能聚焦某个元素,这就是我解决问题的方法:
我已将不透明度过渡添加到 :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:
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)