我正在建立一个响应的网站,可以在不同的视窗宽度下任意改变窗口小部件的位置。
从视觉上看,它的转折点是:
进入这个:
我的第一个方法是这样设置占位符div元素:
<div data-change-from=".my-element"></div>
使用response.js来检测宽度并使用jQuery移动元素,但是使用复杂的网格和更多的小部件,它变得不可管理。
什么是一种有效和可靠的方法来做到这一点?
最佳答案
使用CSS
它很棒,无所不能。
body { position: relative }
body:after {
content: '';
display: block;
clear: both;
}
#one, #two, #three, #four, #five, #six {
height: 40px;
background: #aaa;
margin: 8px 1%;
float: right;
}
#one-two {
position: absolute;
bottom: 0;
right: 0;
width: 45%;
}
#one, #two { width: 100% }
#three, #four, #five, #six {
float: left;
width: 100%;
}
#five, #six { width: 50% }
@media screen and (min-width: 1000px) {
#one-two {
position: static;
display: inline;
}
#one, #three, #five {
width: 60%;
float: left;
}
#two, #four, #six {
width: 35%;
float: right;
}
}
只需要一个额外的容器:
<div id="one-two">
<div id="two">2</div>
<div id="one">1</div>
</div>
<div id="three">3</div>
<div id="four">4</div>
<div id="five">5</div>
<div id="six">6</div>
http://jsfiddle.net/nce9b/
基本上我有一个默认的样式,除非在媒体查询中被覆盖。这通过调整浮动、宽度来重新排列元素,在
(min-width: 1000px)
和#one
应用#two
的情况下(将其移动到底部)。当窗口足够大时,媒体查询将覆盖形成一个简单网格的样式。关于javascript - 任意切换DOM元素位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23418588/