【问题标题】:How can I completely cancel a class when a CSS media style is used?使用 CSS 媒体样式时,如何完全取消课程?
【发布时间】:2014-09-07 01:18:40
【问题描述】:

我有一张如下所示的表格:

它是使用标准表结构构建的:

<table id="dashboard" class="table table-condensed table-hover table-striped table-bordered sortableTable responsive-table table-header-rotated">
   <thead>
        <tr>
            <th>Col 1</th>
            <th>Col 2</th>
            <th>Date Field</th>
            <th class="rotate"><div><span>Attribute 1</span></div></th>
            <th class="rotate"><div><span>Attribute 2</span></div></th>
            <th class="rotate"><div><span>Attribute 3</span></div></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td data-label="Col 1">Data 1</td>
            <td data-label="Col 2">Data 2</td>
            <td data-label="Date Field" class="success">2014-07-03</td>
            <td data-label="Attribute 1" class="success"><a href="#">&nbsp;</a></td>
            <td data-label="Attribute 2" class="success"><a href="#">&nbsp;</a></td>
            <td data-label="Attribute 3" class="success"><a href="#">&nbsp;</a></td>
        </tr>
        <tr>
            <td data-label="Col 1">Data 3</td>
            <td data-label="Col 2">Data 4</td>
            <td data-label="Date Field" class="warning">2014-06-03</td>
            <td data-label="Attribute 1" class="success"><a href="#">&nbsp;</a></td>
            <td data-label="Attribute 2" class="warning"><a href="#">&nbsp;</a></td>
            <td data-label="Attribute 3" class="success"><a href="#">&nbsp;</a></td>
        </tr>
    </tbody>
</table>

table-header-rotated 看起来像这样来获取垂直列标题

.table-header-rotated th.row-header{
  width: auto;
}

.table-header-rotated td{
  width: 10px;
  vertical-align: middle;
  text-align: center;
}

.table-header-rotated th.rotate{
  height: 80px;
  width: 10px;
  position: relative;
  vertical-align: bottom;
  padding: 0;
  font-size: 12px;
  line-height: 0.8;
}

.table-header-rotated th.rotate > div{
  position: relative;
  top: 0px;
  height: 100%;
  -ms-transform:skew(0deg,0deg);
  -moz-transform:skew(0deg,0deg);
  -webkit-transform:skew(0deg,0deg);
  -o-transform:skew(0deg,0deg);
  transform:skew(0deg,0deg);
  overflow: hidden;
}

.table-header-rotated th.rotate span {
  -ms-transform:skew(0deg,0deg) rotate(270deg);
  -moz-transform:skew(0deg,0deg) rotate(270deg);
  -webkit-transform:skew(0deg,0deg) rotate(270deg);
  -o-transform:skew(0deg,0deg) rotate(270deg);
  transform:skew(0deg,0deg) rotate(270deg);
  position: absolute;
  bottom: 30px; 
  left: -20px;
  display: inline-block;
  text-align: left;
  text-align: center; 
}

当页面小于一定宽度时,它会折叠成这样:

这是使用这个 CSS 完成的:

@media 
only screen and (max-width: 900px),
(min-device-width: 768px) and (max-device-width: 1024px)  {

    /* Force table to not be like tables anymore */
    #dashboard table, 
    #dashboard thead, 
    #dashboard tbody, 
    #dashboard th, 
    #dashboard td, 
    #dashboard tr { 
        display: block; 
    }

    /* Hide table headers (but not display: none;, for accessibility) */
    #dashboard thead tr { 
        position: absolute;
        top: -9999px;
        left: -9999px;
    }

    #dashboard tr { border: 1px solid #ccc; }

    #dashboard td { 
        /* Behave  like a "row" */
        border: none;
        border-bottom: 1px solid #eee; 
        position: relative;
        padding-left: 50%; 
        vertical-align: left;
        text-align: left;
    }

    #dashboard td:before { 
        /* Now like a table header */
        position: absolute;
        /* Top/left values mimic padding */
        top: 6px;
        left: 6px;
        width: 45%; 
        padding-right: 10px; 
        white-space: nowrap;
        /* Pull label from the data-label attribute */
        content: attr(data-label);
    }
}

/* Smartphones (portrait and landscape) ----------- */
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
    body { 
        padding: 0; 
        margin: 0; 
        width: 320px; }
    }

/* iPads (portrait and landscape) ----------- */
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
    body { 
        width: 495px; 
    }
}

现在,我的问题是,当我从表格中删除 table-header-rotated 类并使用较小的屏幕时,折叠的表格看起来像我想要的样子:

我的问题似乎是这个 CSS 块:

.table-header-rotated td{
  width: 10px;
  vertical-align: middle;
  text-align: center;
}

演示问题的小提琴: http://jsfiddle.net/38vXF/

它的工作原理(和非垂直标题)的 JSFiddle http://jsfiddle.net/zzUqL/1/

请记住,对于这两个小提琴,表格布局会根据窗口的大小而变化。

当页面较小并且媒体 CSS 正在呈现我的表格时,我如何才能完全删除这个 CSS 类 (table-header-rotated)? 显然,我不需要垂直标题。

【问题讨论】:

  • 为什么不把 CSS 包装在一个最小宽度的媒体查询中呢?喜欢@media only screen and (min-width: 900px) {...}
  • 由于您没有标记 JS/jQuery,我知道您可能不是在寻找使用它的解决方案。但是,如果您决定采用这种方式,您可以使用fiddle 中的代码。

标签: css twitter-bootstrap


【解决方案1】:

这可以通过在加载时动态添加/删除类以及使用 Javascript/jQuery 调整屏幕大小来实现。下面的示例使用 jQuery,但如果您不想使用 jQuery,可以将其移植回 vanilla JavaScript。

注意:this post 中提到的 JS/jQuery 宽度和 CSS 媒体查询宽度之间存在差异。因此,我们还必须使用那里提供的解决方法。

Updated Demo

宽度解决方法代码:

function viewport() {
    var e = window, a = 'inner';
    if (!('innerWidth' in window )) {
        a = 'client';
        e = document.documentElement || document.body;
    }
    return { width : e[ a+'Width' ] , height : e[ a+'Height' ] };
}

示例 1 - jQuery:

$(document).ready(function () {
    res = viewport();
    if (res.width<900) $('#dashboard').removeClass('table-header-rotated');
    $(window).on('resize', function () {
        res = viewport();
        console.log(res.width);
        if (res.width < 900) $('#dashboard').removeClass('table-header-rotated');
        else $('#dashboard').addClass('table-header-rotated');
    });
});

示例 2 - JavaScript:

window.onload = function (){
    res = viewport();
    if (res.width<900) {
        var elm = document.getElementById('dashboard');
        elm.className = elm.className.replace('table-header-rotated','');
    }
    window.onresize = function(){
        elm = document.getElementById('dashboard');
        res = viewport();
        if (res.width<900) {
            elm.className = elm.className.replace('table-header-rotated','');
        }
        else{
            if(elm.className.indexOf('table-header-rotated') == -1)
                elm.className += ' table-header-rotated';
        }
    };

};

【讨论】:

    【解决方案2】:

    您可以简单地使用matchMedia 使用javascript 动态删除该类。 像这样:

    if (matchMedia) {
        var mq = window.matchMedia("(min-width: 900px)");
        mq.addListener(WidthChange);
        WidthChange(mq);
    }
    function WidthChange(mq) {
    
        if (mq.matches) {
            document.querySelector("table").classList.add("table-header-rotated");
        }
        else {
            document.querySelector("table").classList.remove("table-header-rotated");
        }
    }
    

    【讨论】:

    • +1 是一个非常好的选择。但是你知道这个matchMedia的浏览器支持吗?
    • 它在 IE10 上运行良好。但它存在一个来自 Paul Irish here 的非常好的 polyfill,适用于旧版浏览器。
    【解决方案3】:

    我已更新JS Fiddle

    无需删除“table-header-rotated”类。

    你可以使用这个类,但你只需要重新设置它的样式,就像下面媒体查询中提到的那样。

    @media only screen and (max-width: 900px), (min-device-width: 768px) and (max-device-width: 1024px) {
        .table-header-rotated td {
            width: auto;
        }
    }
    

    根据您的新要求,我只更改了 CSS 中的代码,参考您的comment

    这也可以使用 JS 来实现,但由于跨浏览器和不同的移动设备,最好使用 CSS 更改。

    请参考这个JS Fiddle Updated

    下面我已经添加了 CSS 样式代码。

        .table-header-rotated th.row-header{
          width: auto;
        }
    
        .table-header-rotated td{
          width: 10px;
          vertical-align: middle;
          text-align: center;
        }
    @media 
        only screen and (min-width: 901px) {
    
    
        .table-header-rotated th.rotate{
          height: 80px;
          width: 10px;
          position: relative;
          vertical-align: bottom;
          padding: 0;
          font-size: 12px;
          line-height: 0.8;
        }
    
        .table-header-rotated th.rotate > div{
          position: relative;
          top: 0px;
          height: 100%;
          -ms-transform:skew(0deg,0deg);
          -moz-transform:skew(0deg,0deg);
          -webkit-transform:skew(0deg,0deg);
          -o-transform:skew(0deg,0deg);
          transform:skew(0deg,0deg);
          overflow: hidden;
        }
    
        .table-header-rotated th.rotate span {
          -ms-transform:skew(0deg,0deg) rotate(270deg);
          -moz-transform:skew(0deg,0deg) rotate(270deg);
          -webkit-transform:skew(0deg,0deg) rotate(270deg);
          -o-transform:skew(0deg,0deg) rotate(270deg);
          transform:skew(0deg,0deg) rotate(270deg);
          position: absolute;
          bottom: 30px; 
          left: -20px;
          display: inline-block;
          text-align: left;
          text-align: center; 
        }
    }
    
    
    @media 
        only screen and (max-width: 900px),
        (min-device-width: 768px) and (max-device-width: 1024px)  {
    
            /* Force table to not be like tables anymore */
    /*
            #dashboard table, 
            #dashboard thead, 
            #dashboard tbody, 
            #dashboard th, 
            #dashboard td, 
            #dashboard tr { 
                display: block; 
            }
    */
            /* Hide table headers (but not display: none;, for accessibility) */
    /*      #dashboard thead tr { 
                position: absolute;
                top: -9999px;
                left: -9999px;
            }
        */  
            #dashboard tr { border: 1px solid #ccc; }
    
            #dashboard td { 
                /* Behave  like a "row" */
                border: none;
                border-bottom: 1px solid #eee; 
                position: relative;
                /*padding-left: 50%;*/ 
                vertical-align: left;
                text-align: left;
            }
    
            #dashboard td:before { 
                /* Now like a table header */
                position: absolute;
                /* Top/left values mimic padding */
                top: 6px;
                left: 6px;
                width: 45%; 
                padding-right: 10px; 
                white-space: nowrap;
                /* Pull label from the data-label attribute */
                /*content: attr(data-label);*/
            }
        }
    
        /* Smartphones (portrait and landscape) ----------- */
        @media only screen
        and (min-device-width : 320px)
        and (max-device-width : 480px) {
            body { 
                padding: 0; 
                margin: 0; 
                width: 320px; }
            }
    
        /* iPads (portrait and landscape) ----------- */
        @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
            body { 
                width: 495px; 
            }
        }
    

    如果您有任何其他查询或评论,那么您可以修改小提琴并重新发送。

    问候 D.

    【讨论】:

    • 不,如果您将table-header-rotated 添加到表中,它仍然会遇到同样的问题。这不仅仅是我们必须重置为初始值的一个属性。有多个类和属性。 OP 需要类 table-header-rotated 用于表,但只是想在较小的分辨率下使其效果无效。
    • 你能解释一下输出应该是什么!因为,从您在结果中添加的屏幕截图中可以看出,此代码在中小屏幕中完美地实现了它。
    • 嗨,你能用jsfiddle collabration告诉我你想要达到的目标吗...!!
    • 很简单,我想要this的大屏版(注意标题)和this的小屏版
    • 嗨安迪,我希望这是你想要的!请检查此JS Fiddle...问候 D.
    猜你喜欢
    • 2013-02-25
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多