【问题标题】:CSS dynamic layout with mobile view versus normal view in 2 column layout带有移动视图的 CSS 动态布局与 2 列布局中的普通视图
【发布时间】:2014-02-24 23:48:34
【问题描述】:

我的问题如下。我有一个两列布局。但是我想做的事情有点复杂,而且我还没有在 stackoverflow 上找到一个很好的教程/问题来准确地处理我的问题。

我有以下图像,它代表了我目前的布局。

当前页面布局

在移动视图上,这是期望的结果:

想要的结果

如何确保第一个绿色方块位于红色和紫色方块之间,而第二个和第三个方块位于紫色方块下方。

这是一个问题,因为如果我应用Two column layout, fixed right column 的解决方案,第二个块位于红色块旁边的正常位置,并且在局部结束于红色块下方

我提供了一个 JSfiddle http://jsfiddle.net/mdibbets/LgKP5/ 来复制这种情况。

HTML

<div id="red">
    <div id="yellow1">
        <span>1</span>
    </div>
    <div id="yellow2">
        <span>2</span>
    </div>
</div>
<div id="black">
    <div id="green1">
        <span>1</span>
    </div>
    <div id="green2">
        <span>2</span>
    </div>
    <div id="green3">
        <span>3</span>
    </div>
</div>
<div id="blue">
    <div id="purple1">
        <span>1</span>
    </div>
    <div id="purple2">
        <span>2</span>
    </div>
    <div id="purple3">
        <span>3</span>
    </div>
</div>

CSS

span
    {
        display:inline-block;
        margin: 50px 50px;
        font-size:4em;
        font-weight:800;
        text-shadow: 3px 3px 10px gray;
        font-family:sans-serif;
    }

#red {
    background-color:red;
    border: 1px solid brown;
    display:inline-block;
}
#yellow1, #yellow2
    {
    background-color:yellow;
    border: 1px solid lightbrown;
    }
#black
    {
    display:inline-block;
    border:1px solid black;
    background-color:gray;
    }
#green1, #green2, #green3
    {
    display:inline-block;
    background-color:lime;
    border:1px solid darkgreen;
    }
#blue
    {
    display:inline-block;
    border:1px solid darkblue;
    background-color:lightblue;
    }
#purple1, #purple2, #purple3
    {
    display:block;
    border: 1px solid darkpurple;
    background-color:purple;
    }
/* desktops and big resolution screens that can handle the normal layout */
@media only screen and (min-width: 875px) {
#red {
    width:600px;
}

#yellow1, #yellow2
    {
    width:250px;
    height:300px;
    margin:20px;
    float:left;
    }
#black
    {
    float:right;
    }
#green1, #green2, #green3
    {
    clear:both;
    float:right;
    width:200px;
    height:150px;
    margin:20px;
    }
#blue
    {
    width:600px;
    }
#purple1, #purple2, #purple3
    {
    width:150px;
    margin:25px;
    float:left;
    height:200px;
    }
}
/* small screens */
@media only screen and (max-width: 875px) {
#red {
    width:100%;
}
#yellow1, #yellow2
    {
    width:90%;
    margin:5%;
    float:left;
    }
#black
    {
    width:100%;
    }
#green1, #green2, #green3
    {
    width:90%;
    margin:5%;
    }
#blue
    {
    width:100%;
    }
#purple1, #purple2, #purple3
    {
   width:90%;
    margin:5%;
    }
}

将“绿色块”作为一个整体进入动态模式是没有问题的。 但是“分裂”它们是一个完全不同的问题。

是否可以根据屏幕分辨率以 IE 条件 cmets 样式激活 html 代码?如果屏幕分辨率是……我可以呈现另一组 HTML...

【问题讨论】:

  • 您的 jsFiddle 与您的第二张图片不匹配
  • 是的,这就是我发布问题的原因...这是我想要的结果。
  • 唯一的方法是使用Flexbox
  • Flexbox 很有趣...但是在现实世界中,对于日常消费者来说,您必须处理“旧”标准...请记住它何时可能成为主流 2 或3年

标签: html css responsive-design


【解决方案1】:

我认为,你可以做到这一点

position: absolute;

并给出准确的

height

top

元素的属性。你能检查一下这个吗:jsFiddle

【讨论】:

  • 这不是我真正想要的,它有点违背动态流布局的目的,特别是因为元素的高度无法预测。不过要努力一点。
【解决方案2】:

我通过使用自己的书面库得出了自己的答案:

jsfiddle上有一个功能示例:http://jsfiddle.net/mdibbets/U3m4m/

如果您需要更多解释http://all-purpose-programming.blogspot.nl/2014/02/chaning-order-of-appearance-of-elements.html

/* Copyright 2014 Michael Dibbets, all rights reserved. 
    This library is free to use for non commercial use, 
    if you leave a comment below the blog post at 
    http://all-purpose-programming.blogspot.nl/2014/02/chaning-order-of-appearance-of-elements.html
    where you are implementing this libary.
    For commercial use contact me to come to an licensing agreement.
    mdibbets [at] outlook [dot] com
    My prices are fair and reasonable.
    ususage of this script without notification of the author 
    in the before mentioned ways is not permitted.
*/
function JX_responsive_sort(precursor)
    {
    if(typeof precursor == 'undefined')
        {
        precursor = 'conditional';
        }
    // our sorting array
    var orderofthings = new Array();
    // window width 
    var windowwidth = $(window).innerWidth();
    // get all classes named conditional
    jQuery('.'+precursor).each(function()
       {
       // get all classes of the conditional element
       var classList = jQuery(this).attr('class').split(/\s+/);
       // loop through all classes of the current element
       // backwards compatible... of course
       for(c=0;c<classList.length;c++)
          {
          item=classList[c];
          // no need for this one bub
          if(item != precursor)
              {
              if(item.substring(0,11) == precursor)
                  {
                  // sanitise the item
                  item = item.substring(12,item.length);
                  // from which screenwidth is this rule active?
                  var startwidth = parseInt(item.substring(0,item.indexOf('_'))); 
                  // to which screenwidth is this rule active?
                  var endwidth = parseInt(item.substring(item.indexOf('_')+1,item.indexOf('_',item.indexOf('_')+1)));
                  if(windowwidth >= startwidth && windowwidth <= endwidth)
                      {
                      // remove the widths so all that remains are commands
                      item = item.substring(item.indexOf('_',item.indexOf('_')+1)+1,item.length);    
                      // split on hypens, since they are the commands separators
                      var commands = item.split(/-/);
                      for(i=0;i<commands.length;i++)
                          {
                          // get the instructions
                          var instructions = commands[i].split('_');
                          // to know the command remove the first element
                          var command = instructions.shift();
                          // command order - Instruction to display the elements in the desired order.
                          // This is a top level ordering of parent elements in the conditional_mainwrap 
                          // In order to change the ordering of child elements you'll have to "pop" them
                          // a level higher.
                          // allowed variables:
                          // 0 : order number priority. Try not to cause conflicts
                          if(command == 'order')
                              {
                              // add the this element at the desired position into the order of things.
                              orderofthings.push({order: instructions[0] , theobj : this });
                              }
                          // command pop - Instruction to remove the current element from its parent,
                          // and attach it to the parents parent.
                          if(command == 'pop')
                              {
                              // an item that switches positions needs an "returning" wrapper.
                              // that is the first parent.
                              // Then we have the normal container that needs to be escaped.
                              // Then we have parent where we wish to attach it to.
                              if(jQuery(this).parent().attr('id') == precursor + '_wrap_' + jQuery(this).attr('id'))
                                  {
                                  jQuery(this).appendTo($(this).parent().parent().parent());
                                  }
                              }
                          // command repop - Instruction to return the current element to it's designated position
                          // This is why a poppable element needs a wrapper, so we know where to return it.
                          if(command == 'repop')
                               {
                               jQuery(this).appendTo('#conditional_wrap_'+jQuery(this).attr('id'));
                               }
                          }
                      }
                  }
              }              
          }
       });
    // set a simple variable to get the jQuery object of the parent wrap
    parentkeeper = jQuery('.'+precursor+'_mainwrap');
    // sort the arrays so the orderofthings get's it's sorting from the ORDER command.
    orderofthings = orderofthings.sort(keysrt('order',false));
    for(c=0;c<orderofthings.length;c++)
        {
        if(typeof orderofthings[c] != 'undefined')
            {
            jQuery(orderofthings[c]['theobj']).appendTo(parentkeeper);
            }
        }
    }
jQuery(document).ready(function(){
JX_responsive_sort();
});
jQuery( window ).resize(function() 
    {
    JX_responsive_sort();
    });
function keysrt(key,desc) {
  return function(a,b){
   return desc ? ~~(a[key] < b[key]) : ~~(a[key] > b[key]);
  }
}

【讨论】:

    猜你喜欢
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    相关资源
    最近更新 更多