Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Move div when resize browser

I want to change this:

<div id="main 1" style="background: #ccc;" class="fourcol first clearfix" role="main">
LEFT
</div>

<div id="main middle" style="background: #ddd;" class="fourcol middle clearfix" role="main">
MENU
</div>  

<div id="main 3" style="background: #eee;" class="fourcol last clearfix" role="main">
RIGHT
</div> 

to this(order: MENU, LEFT, RIGHT) when I resize the window below 481px, with;

jQuery(document).ready(function($) {

$(window).resize(function() {
    var responsive_viewport = $(window).width();
    if (responsive_viewport < 481) {
            $('#inner-content').parent().prependTo('middle');
    }
});
like image 984
Mathias Kaisner Avatar asked Nov 24 '25 01:11

Mathias Kaisner


1 Answers

Use this

$(window).resize(function() {

   var responsive_viewport = $(window).width();

   //console.log(responsive_viewport);
   if (responsive_viewport < 481) {
      var middle_div = $('#main-middle').clone().remove();
      $("#main-1").before(middle_div);
   }
});

You div ids have space which is not allowed. I guessing there was either an hyphen or underscore which you have missed out. I have added hyphen in my code.

Also the above code keeps adding the menu when the viewport < 481. So try setting a flag or something once it has already moved. Otherwise you are going to have multiple menus.

Cheers!!

like image 159
bhb Avatar answered Nov 25 '25 15:11

bhb