简体   繁体   English

如何在响应中更改div位置

[英]How to Change div Position in responsive

This is my website which I am working on. 这是我正在开发的网站

As you can see there is a sidebar in desktop mode. 如您所见,桌面模式下有一个侧边栏。 But when you see it in mobile mode the sidebar goes down under the content which is showing on the left side. 但是,当您在移动模式下看到它时,侧栏会在左侧显示的内容下方下降。

Now what I want in mobile view is to make sidebar appear on top, and after that the content should appear. 现在,我想要在移动视图中使侧边栏显示在顶部,然后显示内容。 I've tried lots of things like position:absolute; 我已经尝试过很多诸如position:absolute;类的事情position:absolute; and margin but it's not working for me. margin但对我不起作用。

Please suggest what would be the correct way to do this task. 请提出执行此任务的正确方法。

jsFiddle of my code jsFiddle我的代码

This works for me 这对我有用

<script type="text/javascript">

var windowWidth = $(window).width();
//window.alert(windowWidth);
if(windowWidth<=767){
    $('.wf-span-4').insertBefore('.wf-span-8');
    }
    </script>

You should probably provide a simplified version of your code, however, here's what I've got. 您可能应该提供代码的简化版本,但是,这就是我所得到的。

You have one of two options: 您有以下两种选择之一:

  1. change the structure of the site so that the order is reversed in the first place. 更改网站的结构,以便首先颠倒顺序。
  2. Use jquery to move the content below a certain width ex: $('#sidebar').insertBefore('#content') 使用jquery将内容移动到特定宽度以下: $('#sidebar').insertBefore('#content')

The correct way imo would be to put your markup in the right order to begin with. imo的正确方法是按照正确的顺序放置标记。 Markup is structure and should be independent of styling (as much as possible). 标记是结构,应独立于样式(尽可能)。

Then your code would look something like this 然后你的代码看起来像这样

<section class="main">
    <div class="sidebar">Bye</div>
    <main class='content'>Hi</main> 
</section>

And all you would have to do is remove the floats on mobile, so the content goes back into the default flow. 而您所要做的就是删除移动设备上的浮动广告,这样内容就会恢复为默认流程。 Something like this: 像这样:

.content {
    width:75%;
    float:left;
}
.sidebar {
    width:25%;
    float:right
}

@media screen and (max-width:767px) {
    .content, .sidebar {
        float: none;
    }
}

(note that I updated your class names and markup, just so the code would be a bit better readable) (请注意,我更新了您的类名和标记,只是这样代码的可读性更好)

And here is your updated demo: http://jsfiddle.net/ehozb5v9/2/ 这是您更新的演示: http : //jsfiddle.net/ehozb5v9/2/

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

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