简体   繁体   English

在顶部创建div会将所有其他内容下移

[英]Making a div on the top push all other content down

I'm programming a firefox plugin which shows a div on top the page and which pushes ALL other content down. 我正在编程一个firefox插件,该插件在页面顶部显示一个div,并将所有其他内容下推。 I tried to put it before the head tag but it doesn't seem to work. 我试图将其放在head标签之前,但似乎不起作用。

My code: 我的代码:

var allDivs = document.getElementsByTagName('div');
var headTag = document.getElementsByTagName('head')[0];

if (allDivs.mainplugindiv)
{
  alert("!!! Plugin LLIBrowser is running !!!");
}
else
{
  var div = document.createElement('div');
  div.id='mainplugindiv';
  div.style.position = 'absolute';
  div.style.zIndex = '100';
  div.style.width = '100%';
  div.style.height = '150px';
  div.style.background = '#313192';
  div.style.color = 'white';
  div.innerHTML="<script type=\"text/javascript\"   src="+self.options.angularLib+"></script>"+
"<script type=\"text/javascript\" src="+self.options.angularApp+"></script>"+
  "<form style=\"margin: 0 auto; left: 10px; top: 50px; width: 98%;align: middle; visibility: visible\">"+
    "<label for=\"video\" style=\"color: white\"> <input type=\"checkbox\" checked=\"checked\" name=\"video\" value=\"video\" id=\"video\" /> Video</label>"+
    "<label for=\"forum\" style=\"color: white\"> <input type=\"checkbox\" checked=\"checked\" name=\"forum\" value=\"forum\" id=\"forum\" /> Forum</label>"+
    "<img src="+self.options.closeImg+" alt=\"Help\" style=\"width:20px;height:20px;\" align=right onclick=\"var youSure=confirm('Are you sure you want to close the plugin?'); if (youSure){var element = document.getElementById('mainplugindiv');element.outerHTML = '';delete element;}\">"+
    "<img src="+self.options.helpImg+" alt=\"Close\" style=\"width:20px;height:20px;\" align=right onclick=\"alert('LLIBrowser')\">"+
  "</form>"+
  "<div style=\"margin: 0 auto; left: 10px; top: 50px; width: 98%; height: 50%; align: middle; background-color:#75bbf7; visibility: visible\">"+
    "<div ng-show=\"tab == 1\">"+
      "<form>"+
        "<h3 style=\"color: white; align: center\">Video1 Video2 Video3 Video4</h3>"+
      "</form>"+
    "</div>"+
    "<div ng-show=\"tab == 2\">"+
      "<form>"+
        "<h3 style=\"color: white; align: center\">Forum1 Forum2 Forum3 Forum4</h3>"+
      "</form>"+
    "</div>"+
  "</div>"+
  "<form style=\"margin: 0 auto; left: 10px; top: 50px; width: 98%;align: middle; background-color:#313192; visibility: visible\">"+
  "<a href=\"#video\" style=\"color: white; background-color: #75bbf7; height: 15px; padding: 5 auto\" ng-click=\"tab = 1\">Video</a>"+
  "<a href=\"#forum\" style=\"color: white; background-color: #313192; height: 15px; padding: 5 auto\" ng-click=\"tab = 2\">Forum</a>"+
  "</form>";
  headTag.parentNode.insertBefore(div, headTag);

Thanks! 谢谢!

instead of setting position to absolute set it relative and clear both sides. 而不是将位置设置为绝对,而是将其设置为相对并清除两侧。

div.style.position = 'relative';
div.style.clear = 'both';

This should work. 这应该工作。

Edit : Instead of appending in head tag, make it the first child of yourbody 编辑 :而不是附加在head标签,使其成为您的第一个孩子

document.body.insertBefore(div,body.firstChild);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM