首页 > 解决方案 > 将固定/粘性标题注入任何网页

问题描述

我想将一个粘性/固定标题注入用户在我的 Chrome 扩展程序中加载的任何网页。

使用JQuery我可以插入一个 div 并设置它的样式。我尝试使用教程中为粘性标题指定的几种不同样式,但它与页面重叠,而不是将页面进一步向下推。

如果我正在编写网页,则处理此问题会很好,但在这种情况下,网页可以包含任何 html 或 css。

这是我尝试过的一个例子。

$(function () {
  $("body").prepend(
    "<div id='my-header'>This is displayed at the top of the page</div>'"
  );

  $("#my-header").css({
    "z-index": 2000,
    overflow: "hidden",
    "background-color": "#333",
    position: "fixed",
    top: 0,
    width: "100%",
    height: "200px",
  });
});

实现这一目标的正确方法是什么?

标签: javascriptjquerygoogle-chrome-extension

解决方案


推荐阅读