【问题标题】:Chrome box-shadow transition weirdnessChrome box-shadow 过渡怪异
【发布时间】:2017-08-13 01:06:37
【问题描述】:

我在 Google Chrome 上遇到了 box-shadow 问题。

在这个 gif 中,它在 Firefox 中可以正常工作:

但在谷歌浏览器中它的工作方式很奇怪:

在我的主题中,每个类别都有颜色。 首先,我将该颜色应用到 li 为 border-left 像这样

<a style="border-left:4px solid '.$color.';" ...

当悬停类别 li 时,我使用我的 jquery 代码将左边框颜色应用为 box-shadow

$(document).ready(function() {
    $('.side-category ul li a').hover(function() {
        $(this).css("box-shadow", "inset 200px 0 0 0 " + $(this).css("border-left-color"))
    }, function() {
        $(this).css("box-shadow", "")
    })
});

来自页面源的 HTML 代码:

<ul>
    <li><i class="fa fa-wordpress"></i><a style="border-left:4px solid #9b59b6;" href="http://localhost/demos/kisiselblog/category/wordpress/">Wordpress</a><span class="cat-count" style="background: #9b59b6;display:block;">12</span></li>
    <li><i class="fa fa-dot-circle-o"></i><a style="border-left:4px solid #3498db;" href="http://localhost/demos/kisiselblog/blog/">Blog</a><span class="cat-count" style="background: #3498db;display:none;"></span></li>
    <li><i class="fa fa-times"></i><a style="border-left:4px solid #060166;" href="http://localhost/demos/kisiselblog/category/no-show/">No show</a><span class="cat-count" style="background: #060166;display:block;">2</span></li>
</ul>

如您所见,在 Google Chrome 中它无法正常工作。 我该如何解决这个问题?

【问题讨论】:

  • 请分享您的HTML 代码。
  • 这是wordpress菜单,我是从functions.php文件中定义的。 prntscr.com/g7sj18 但我现在还是从源代码添加我的 html 代码

标签: jquery html css google-chrome


【解决方案1】:

试试这个

$(document).ready(function() {
  $('.side-category ul li a').mouseenter(function() {
    $(this).css("box-shadow", "inset 200px 0 0 0 " + $(this).css("border-left-color"))
  });

  $('.side-category ul li a').mouseleave(function() {
    $(this).css("box-shadow", "")
  })
});

使用mouseentermouseleave 代替hover

对于响应式anchor 标签,使用$(this).width(); 即时获取宽度

$(this).css("box-shadow", "inset "+ $(this).width() + "px 0 0 0 " + $(this).css("border-left-color"));

【讨论】:

  • 感谢您的帮助!实际上我再次检查了我的代码,我的代码是inset 445px 0 0 0。代码中的 200px 是错误的,对不起。在桌面 200px 就足够了,但在平板电脑上它不适合 prntscr.com/g7sl4u 。当我再次将其设为 200px 时,chrome 问题就消失了。但是有什么方法可以让 box-shadow 100% 吗?
  • 试试这个$(this).css("box-shadow", "inset "+ $(this).width() + "px 0 0 0 " + $(this).css("border-left-color"));。即时获取anchor 标签的宽度并将其分配给box-shadow。希望这会对你有所帮助。
  • 是的,效果很好!非常感谢:) 你能添加你的评论来回答吗?我会将其标记为正确答案。
猜你喜欢
  • 2012-11-24
  • 2016-01-27
  • 2019-01-30
  • 2014-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
相关资源
最近更新 更多