简体   繁体   English

如何通过JQuery修改CSS

[英]How to modify CSS via JQuery

I want to get the screen and modify the css styles of my box. 我想获得屏幕并修改我的盒子的CSS样式。 Thats my code 那是我的代码

if ($('.content').height() > $(window).height() - 100) {
    $('.content').css("max-height", $(window).height() - 100);
}

Now I want to set the max height of the .content box lower than the height of my screen size. 现在我想将.content框的最大高度设置为低于屏幕大小的高度。 If my content box is higher than my screen size, the css should set de max height and change the overflow to auto. 如果我的内容框高于我的屏幕大小,则css应设置de max height并将溢出更改为auto。

Looks like you could do this - 看起来你可以做到这一点 -

var content = $(".content");
// Cache the height in variables, no need to access DOM again and again
var screenHeight = $(window).height();
var contentHeight = content.height();
if(contentHeight>windowHeight-100){
  content.css("overflow","auto"); // Note - this will change it for the current instance of the page and NOT in your CSS file
  content.css("max-height",(windowHeight-100) + "px");      
}

You can also combine the above two css properties in one single statement (written separately for explanation) - 您还可以将上述两个css属性合并到一个语句中(单独编写以供说明) -

content.css({"max-height":(windowHeight-100)+"px","overflow":"auto"});

I would suggest you to use the max height value along with px . 我建议你使用最大高度值和px

if ( $('.content').height() > ($(window).height() - 100) ) {
    $('.content').css("max-height", ( $(window).height() - 100 ) + "px" )
                 .css("overflow","auto");
}

This would resolve you issue. 这将解决您的问题。

Do this - 做这个 -

var maxHeight = $(window).height() - 100;
if ($('.content').height() > maxHeight) {
    $('.content').css({"max-height": maxHeight, overflow: "auto"});
}

使用单位“px”

 $('.content').css("max-height", ($(window).height() - 100)+"px");

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

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