无需访问html即可重新订购Div

时间:2016-11-07 17:33:20

标签: javascript jquery html css wordpress

我正在定制一个wordpress主题,并遇到了令人沮丧的阻碍者。

我想在全屏封面(.fullscreen-cover)之后和内容(.content)之前定位标题(header.non-sticky-nav)。

<header class="non-sticky-nav">
    <div id="navbar"></div>
</header>
<div id="wrapper">
    <div id="content">
        <div class="fullscreen-cover"></div>
        <div class="content"></div>
    </div>
</div>

current-result_vs_desired-result

我尝试使用“top:xpx”值重新定位导航栏,但显然这不起作用,因为.fullscreen-cover不是固定高度。

以下是我正在使用的测试页面:http://samburrough.design/test/

主题允许页面特定或全局css代码注入,并且随着主题定期更新,我想尝试坚持使用此功能而不是深入研究主题文件并在每次我想要更新时覆盖更改

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:2)

你能不能创建一个子主题并以这种方式修改DOM? 至少这样,每次发布父主题更新时,更改都不会被覆盖?

这将(应该)实际上是首选选项。

Smashing Magazine; Create and Customise Wordpress Child Theme

答案 1 :(得分:1)

不幸的是,如果主题不包括块定位,则需要编辑DOM。虽然你可能会在酒吧和英雄上使用一些不稳定的绝对定位,但绝对定位它们可能会导致一系列问题 - 从响应式导航开始。

答案 2 :(得分:1)

有一个javascript函数/方法可以让节点交换dom中的位置。 您可以尝试查看Node.replaceChild()

以下示例来自文档并创建新元素,但您也可以选择现有节点。

// create an empty element node
// without an ID, any attributes, or any content
var sp1 = document.createElement("span");

// give it an id attribute called 'newSpan'
sp1.id = "newSpan";

// create some content for the new element.
var sp1_content = document.createTextNode("new replacement span element.");

// apply that content to the new element
sp1.appendChild(sp1_content);

// build a reference to the existing node to be replaced
var sp2 = document.getElementById("childSpan");
var parentDiv = sp2.parentNode;

// replace existing node sp2 with the new span element sp1
parentDiv.replaceChild(sp1, sp2);

答案 3 :(得分:-1)

您可以尝试将order与flexbox一起使用。例如:

.container {
  display: flex;
  flex-wrap: wrap;
}

.one,
.two {
  width: 100%;
}

.one {
  order: 2;
  background: red;
}


.two {
  order: 1;
  background: blue;
}
<div class="container">
  <div class="one">
  1
  </div>
  <div class="two">
  2
  </div>
</div>

可能会有点问题,具体取决于你的标记是什么样的。并且不适用于旧浏览器。