简体   繁体   English

屏幕宽度更改后,CSS属性不适用。 同时使用CSS和jQuery

[英]CSS Property doesn't apply when screen width changed. Using both css and jQuery

HTML: HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<p>This is a paragraph.</p>
<div class="one">Hello World</div>

CSS: CSS:

p, div.one {
  color: black;
}

Javascript: Javascript:

$(window).resize(function() {
    console.log('resize called');
    var width = $(window).width();
    if (width >= 400 && width <= 500) {
      $(document).ready(function() {
        $("p").css("color", "red");
        $("div.one").css("color", "green");
      });
    }
  })
  .resize();

JSFiddle Link: https://jsfiddle.net/misteeque/q5t5j7q3/4/ JSFiddle链接: https ://jsfiddle.net/misteeque/q5t5j7q3/4/

Basically I want the text color changed to red and green when screen size is between 400 and 500, which happens without any problem, but when the screen size is changed back out of the range, ie either more than 500 or less than 400, the color remains red and green, and it is not changed back to black, as I've defined in CSS. 基本上,我希望在屏幕大小介于400到500之间时,将文本颜色更改为红色和绿色,这没有任何问题,但是当屏幕大小更改回超出范围时,即大于500或小于400时,颜色保持为红色和绿色,并且不更改为黑色,正如我在CSS中所定义的那样。

What is going wrong in here? 这里出了什么问题? Thanks. 谢谢。

Note that using both CSS and jQuery is mandatory here. 请注意,此处必须同时使用CSS和jQuery。

The styles that you've added through Javascript dont' get removed once the screen goes out of that 400-500 range. 屏幕超出该400-500范围时,您通过Javascript添加的样式不会被删除。

You could add a check to make sure they reset, but this is much, much easier with CSS media queries: 您可以添加检查以确保它们重置,但是使用CSS媒体查询则要容易得多:

@media (min-width: 400px) and (max-width:500px) {
  p { color: red }
  div.one { color: green }
}

https://jsfiddle.net/q5t5j7q3/7/ https://jsfiddle.net/q5t5j7q3/7/

Inline element styles have higher specificity than any css applied styles, so you can solve the issue by clearing style attribute of the elements in else clause of your if statement like this... 内联元素样式比任何CSS应用样式都具有更高的特异性,因此可以通过清除if语句else子句中元素的style属性来解决此问题,如下所示...

if (width >= 400 && width <= 500) {
  $("p").css("color", "red");
  $("div.one").css("color", "green");
} else {
  $("p,div.one").attr("style", "");
}

JSFiddle JSFiddle
https://jsfiddle.net/q5t5j7q3/8/ https://jsfiddle.net/q5t5j7q3/8/

You can add else and remove the css you have added (watch it in "full page" and change the screen size) 您可以添加else并删除已添加的CSS(在“整页”中观看并更改屏幕尺寸)

 $(window).resize(function() { console.log('resize called'); var width = $(window).width(); if (width >= 400 && width <= 500) { $(document).ready(function() { $("p").css("color", "red"); $("div.one").css("color", "green"); }); } else { $(document).ready(function() { $("p").css("color", ""); $("div.one").css("color", ""); }); } }) .resize(); 
 p, div.one { color: black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <p>This is a paragraph.</p> <div class="one">Hello World</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM