【问题标题】:css apply style if parent width more than specific value如果父宽度大于特定值,css 应用样式
【发布时间】:2015-03-02 08:46:46
【问题描述】:

如果只有父元素宽度大于特定值,则希望应用 css 选择器。 CSS 不能使用媒体查询来实现这一点,因为视口宽度和父 div 宽度之间没有关系。

欢迎使用 Javascript,但请避免连续检查宽度。 例如。

<style>

/*  something like that  div[width>400px]  */
   .parent div[width>400px]{
       background:red;
    }


</style>


<!--ok width is more than 400 so apply style to child div-->
<div class="parent" style="width:500px;"> 
    <div>

    </div>
</div>


<!--width is still less than 400 so don;t apply styles-->
<div class="parent" style="width:300px;"> 
    <div>

    </div>
</div>

【问题讨论】:

    标签: javascript jquery html css web


    【解决方案1】:

    Resize 事件仅适用于窗口元素。

    Vanilla JavaScript 应该是这样的。

    var parentDivTriggrtWidth = 400;
    var childDivClassNames = ["red", "blue"];
    
    window.addEventListener("load", function(e){
        var list = document.getElementsByClassName("parent");
        for(var i=0; i<list.length; i++){
            var parentDiv = list[i];
            parentDiv.resizeParent = function(width){
                if(typeof width!="undefined"){ this.style.width = width+"px"; }
                var k = this.clientWidth > parentDivTriggrtWidth ? 1 : 0;
                this.children[0].className = childDivClassNames[k];
    // Debug only
    this.children[0].innerHTML = "Parent Size: "+this.clientWidth+"<br />Class: "+this.children[0].className;
            }
            window.addEventListener("resize", function(e){ parentDiv.resizeParent(); },false);
            parentDiv.resizeParent();
        }
    },false);
    

    查看“fullpage”中的sn-p

    var parentDivTriggrtWidth = 400;
    var childDivClassNames = ["red", "blue"];
    
    window.addEventListener("load", function(e){
    	var list = document.getElementsByClassName("parent");
    	for(var i=0; i<list.length; i++){
    		var parentDiv = list[i];
    		parentDiv.resizeParent = function(width){
    			if(typeof width!="undefined"){ this.style.width = width+"px"; }
    			var k = this.clientWidth > parentDivTriggrtWidth ? 1 : 0;
    			this.children[0].className = childDivClassNames[k];
    // Debug only
    this.children[0].innerHTML = "Parent Size: "+this.clientWidth+"<br />Class: "+this.children[0].className;
    		}
    		window.addEventListener("resize", function(e){ parentDiv.resizeParent(); },false);
    		parentDiv.resizeParent();
    	}
    },false);
    .parent {
      border: 3px solid #aaaaaa;
    }
    .parent div {
      width: 200px;
      height: 200px;
      color: white
    }
    .red {
      background-color: red;
    }
    .blue {
      background-color: blue;
    }
    <h3>width 500px (resize by JavaScript)</h3>
    
    <div id="p1" class="parent" style="width:500px;">
      <div></div>
    </div>
    
    <button type="button" onclick="document.getElementById('p1').resizeParent(800)">Resize +</button>
    <button type="button" onclick="document.getElementById('p1').resizeParent(200)">Resize -</button>
    <button type="button" onclick="document.getElementById('p1').resizeParent(500)">Reset</button>
    
    <hr />
    
    <h3>width 100% (resize window)</h3>
    
    <!--width is still less than 400 so don;t apply styles-->
    <div id="p2" class="parent" style="width:100%;">
      <div></div>
    </div>

    【讨论】:

      【解决方案2】:

      jQuery 是一个选项:

       $( document ).ready(function() {
      if ($(".parent").width() > 700) {
      $('.parent').css("background-color", "red");
      }
      });
      

      【讨论】:

        【解决方案3】:

        如果我没听错的话......

        JQuery:

        $(document).ready(function(){
            var ParentWidth = $('.parent').width();
            if (ParentWidth > 400) {
                $('.parent').addClass('newstyle');
            }
        });
        

        CSS:

        .newstyle
        {
            background: red;
            /*** add more css ***/
        }
        

        i 推荐你使用类,因为你可以在以后的类中添加更多的 css。

        【讨论】:

        • 好的,没错,但是改变父尺寸呢?如果我通过 javascript 将父宽度更改为小于 400px,则样式将保持不变。
        • 问题是什么时候...如果您的意思是在调整大小时更改父级,以便您可以使用调整大小 jquery 函数来检查宽度并根据结果添加或删除类...。
        • 父div宽度只能通过1.innerHTML变化2.其他css类3.Javascript来改变。所以它与 on resize 事件无关。
        • 所以所有可能的更改都已加载? (不是事件)
        猜你喜欢
        • 1970-01-01
        • 2017-11-19
        • 1970-01-01
        • 2013-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-17
        • 2015-07-01
        相关资源
        最近更新 更多