jquery - 如何确定可见底层元素的背景颜色?

标签 jquery html css

我有一个固定位置“转到顶部”的页面<a>当用户滚动时,它会粘在页面底部。它位于页面的页脚 DIV 中,如下所示:

.pagedown div a { position: fixed; margin-left: 8px;  margin-top: 50px; width: 44px; height: 40px; color: #bd1300; background: none;}

<div class="footer"><a id="gototop" title="Go to top!" href="#">^</a></div>

使用 jQuery 或 JS 我希望能够更改 <a> 的颜色基于真实的底层背景颜色。到目前为止,我已经看过这里:

How do I detect the inherited background-color of an element using jQuery/JS?

此代码似乎不适用于我的用例。也许自从 <a>当页面滚动时,问题是否真的不是页面 DOM 其余部分的“内部”?

那么:有没有办法检测“真实”的底层背景颜色(即用户在视口(viewport)中实际看到的背景颜色)?

最佳答案

支持不是很好(没有 IE),但你可以使用 CSS 来实现。

mix-blend-mode @ MDN

.footer {
  font-size: 72px;
  text-align: center;
  margin-bottom: 10px;
  line-height: 100px
}
a {
  color: white;
  mix-blend-mode: difference;
}
body {
  background: red;
}
<div class="footer"><a class="gototop" title="Go to top!" href="#">^</a>
</div>

.footer {
  font-size: 72px;
  text-align: center;
  margin-bottom: 10px;
  line-height: 100px
}
a {
  color: white;
  mix-blend-mode: difference;
}
body {
  background: blue;
}
<div class="footer"><a class="gototop" title="Go to top!" href="#">^</a>
</div>

关于jquery - 如何确定可见底层元素的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35443187/

相关文章:

jquery - 折叠的树不起作用

css - 以响应方式显示表单的两个内联元素(调整大小)

javascript - 复制 Javascript 对象

javascript - 如何在调整浏览器大小时将 Google map 标记设置为中心?

javascript - 将括号内的文本转换为大写

javascript - 从 select 创建项目列表/数组

jquery - 如果页面处于事件状态,则更改 li 类

javascript - 可以 .slideToggle() 特定的 <li> 吗?

jQuery 验证成功指示器

javascript - jQuery 切换在我的页面中不起作用