0

Given I have this snippet

<div class="container">
    <div class="row">
        <div id="left" class="col-xs-5 col-sm-5">
          How are you?
        </div>
        <div id="right" class="col-xs-7 col-sm-7">
          Thanks, I'm fine.
        </div>
    </div>
</div>

If open on browser it should display

How are you? Thanks, I'm fine.

When browser is reduced to less than 768px I want the above snippet to yield

Thanks, I'm fine.
How are you?

I am using the following javascript to detect when viewport width of browser is <=767.

 /*
  @Leo
  Obtained from: 
  http://stackoverflow.com/questions/1766861/find-the-exact-height-and-width-of-the-viewport-in-a-cross-browser-way-no-proto
*/

var getviewport = function(){

    var viewPortWidth;
    var viewPortHeight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined') {
        viewPortWidth = window.innerWidth,
        viewPortHeight = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined'
    && typeof document.documentElement.clientWidth !=
    'undefined' && document.documentElement.clientWidth != 0) {
        viewPortWidth = document.documentElement.clientWidth,
        viewPortHeight = document.documentElement.clientHeight
    }

     // older versions of IE
    else {
        viewPortWidth = document.getElementsByTagName('body')[0].clientWidth,
        viewPortHeight = document.getElementsByTagName('body')[0].clientHeight
    }
    return [viewPortWidth, viewPortHeight];
};

var adjust = function(){
    var width = getviewport()[0];

    if(width<=767){
        $("#left").addClass("col-xs-push-7");
        $("#right").addClass("col-xs-pull-5");
    }
    else{
        $("#left").removeClass("col-xs-push-7");
        $("#right").removeClass("col-xs-pull-5");       }
};
adjust();
$(window).resize(adjust);

So when the viewport width is <=767. I try to order the statement

"How are you? Thanks, I'm fine." to  "Thanks, I'm fine. How are you?

By adding corresponding xs-push-* and xs-pull-* I was able to achieve the above ordering to "Thanks, I'm fine. How are you?"

However, I feel something can be further done to break the statement to

Thanks, I'm fine.
How are you?

I will be glad if someone can offer help.

camelCaseCoder
  • 1,447
  • 19
  • 32
amachree tamunoemi
  • 817
  • 2
  • 16
  • 33

1 Answers1

1

If I understand your question correctly, you can actually do this entirely with Bootstrap, no js needed!

To style the way it looks below the 768px breakpoint, just use col-xs-. To style the way it looks above 768, use col-sm-. You can't swap the order of the stacked columns (it'll always be the order they appear in the same order as in the markup), but you can use push and pull to change the order above the 768 breakpoint.

  1. In the markup, order your columns the way you want them to appear when stacked
  2. Add your xs classes - I'm not sure I understand what you want, but I think .col-xs-10.col-xs-offset-1
  3. Add your "bigger-than-xs" classes. So .col-sm-5 and .col-sm-7, remove the offsets with .col-sm-offset-0, and then switch the order by pushing the one column and pulling the other

Here's a working snippet. View it full-window to see how it works above 768. The result will look like this:

Wide (Bootstrap sm and above): enter image description here

Narrow (Bootstrap xs): enter image description here

(The pink area is just to show that the stacked columns are centered col-10)

@import 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css';

/* all of this just for demo purposes */
 body {
  margin: 10px;
}
.container {
  background: rgba(255,0,0,.4);
}
.row div {
  border: 1px solid #000;
  background: #fff;
  height: 40px;
}
.row div:first-child {
  background: #000;
  color: #fff;
}
<div class="container">
  <div class="row">
    <div class="col-xs-10 col-xs-offset-1 col-sm-5 col-sm-push-7 col-sm-offset-0"></div>
    <div class="col-xs-10 col-xs-offset-1 col-sm-7 col-sm-pull-5 col-sm-offset-0"></div>
  </div>
</div>
henry
  • 4,244
  • 2
  • 26
  • 37
  • Thanks for helping. i want it to behave like this, for example if 1-2 is inline on the same row as left side and right side, I want the right which is 2 to come first and occupy a row and the left which is 1 to go to new line and occupy a row of its own. 1 - 2 before but becomes 2 \n 1 – amachree tamunoemi Sep 03 '16 at 21:49
  • That's what this solution does :) you can see it if you click "expand snippet", then "run", then change your window size – henry Sep 03 '16 at 21:51
  • it's almost working. Please do it to work like this, when I "expand snippet" then run let it show 2 - 1 instead of 1 - 2, then when i reduce the browser to less than 768px let it be 1 \n 2. – amachree tamunoemi Sep 03 '16 at 22:11
  • Sorry, maybe I'm misunderstanding. It sounds like now you're talking about just changing the text content? You can make that whatever you want, it doesn't change the layout. I just used those numbers so that you can see the order changing. If that's wrong, try to explain again – henry Sep 03 '16 at 22:20
  • sorry, i've edited the question, you will understand me now.. thanks – amachree tamunoemi Sep 03 '16 at 22:46
  • I think you're getting tricked by the fact that I used numbers for the cell contents. It doesn't matter which one is called 1 and which one is called 2 - those numbers were just there to help you see that the order changes. I've edited my answer to use "blue" and "green" instead - maybe that will be more clear – henry Sep 04 '16 at 18:04
  • we are almost getting it. The expected behavior is that when I Expand script and run, it should display blue green, don't do the ordering to green blue. ordering occurs when viewport width is less than 768px, so when viewport width <=767 do ordering to green blue then finally should appear green centered col-xs-10 col-xs-offset-1 \newline blue centered by col-xs-10 col-xs-offset-1 – amachree tamunoemi Sep 04 '16 at 18:36
  • Is this what you want? >= 768: `• º`. < 768: `º \n •` – henry Sep 04 '16 at 18:42
  • I've re-edited the question and explained in further details. I know your solution is not too far from the answer, me too i'm playing with it to get it work. – amachree tamunoemi Sep 04 '16 at 19:03
  • That's what this solution does :) I've added screenshots – henry Sep 04 '16 at 19:10
  • WOW, thanks you are right. it works, i was having an error that interrupts Bootstrap. – amachree tamunoemi Sep 05 '16 at 10:52