如何使用Jquery根据屏幕大小更改Div的大小

时间:2021-07-28 18:55:09

What is wrong in my jquery? I would like to make my div bigger depending on screen size and there is plenty of good examples, but still no success.

我的jquery有什么问题?我想根据屏幕尺寸使我的div更大,并且有很多很好的例子,但仍然没有成功。

.picLeft {
    border: 4px solid black;
    border-radius: 15px;
    width: 130px !important;-------> I need to change this
    height: 130px;-------> I need to change this
    text-align: center;
    background-color: white;
    margin: 10px !important;
    padding: 0px;
    float: left;
}

**EDIT TWO DIVS LIKE THAT**
var height = window.innerHeight;
var width = window.innerWidth;
console.log("height------->" +height);
console.log("width-------->" +width);
// <div class="ui-block-a picLeft" id="leftDiv">
//$("#leftDiv").css("width","300 !important"); NOT working
$(".picLeft").css("width","300 !important"); //NOT Working

<div class="ui-grid-a"   style="background-color:black">
    <div class="ui-block-a picLeft" id="leftDiv">
    <img src="images/nk100x100.jpg" data-theme="c"
                id="pictureId" />
</div>
    <div class="ui-block-b picRight">
        <img src="images/th100x100.jpg" data-theme="c" id="pictureId2"  />
    </div>
</div>

Thank you! Sami

谢谢!萨米

5 个解决方案

#1


1  

You can use an expression in your css file something like the example below too:

您可以在css文件中使用类似于下面示例的表达式:

width: expression((document.body.clientWidth - 300)  + "px") !important;

Thought it might be useful. You can also do conditional ones like this too:

认为它可能有用。你也可以这样做有条件的:

width: expression( document.body.clientWidth > 1000 ? "1000px" : "99%" ); 

I know this isn't what you asked but others have already given good examples; this is just an alternative.

我知道这不是你问的,但其他人已经给出了很好的例子;这只是一种选择。

#2


2  

$(".picLeft").css("width","300px !important"); //Added "px"

#3


2  

How about this:

这个怎么样:

$(window).resize(function()
{
    $('#display').css('width', ($(this).width() - 100) + 'px');
});​

http://jsfiddle.net/qJCc9/

#4


2  

you have missed px:

你错过了px:

$(".picLeft").css("width","300px !important"); 

I suggest to use percentage values or CSS3 Media Queries for doing this.

我建议使用百分比值或CSS3媒体查询来执行此操作。

#5


1  

Try this...

 var width = jQuery(window).width();       
 jQuery('div.picLeft').css('width':width); 
 jQuery(window).resize(function() {
        var width = jQuery(window).width();  
        jQuery('div.picLeft').css('width':width);
    });

#1


1  

You can use an expression in your css file something like the example below too:

您可以在css文件中使用类似于下面示例的表达式:

width: expression((document.body.clientWidth - 300)  + "px") !important;

Thought it might be useful. You can also do conditional ones like this too:

认为它可能有用。你也可以这样做有条件的:

width: expression( document.body.clientWidth > 1000 ? "1000px" : "99%" ); 

I know this isn't what you asked but others have already given good examples; this is just an alternative.

我知道这不是你问的,但其他人已经给出了很好的例子;这只是一种选择。

#2


2  

$(".picLeft").css("width","300px !important"); //Added "px"

#3


2  

How about this:

这个怎么样:

$(window).resize(function()
{
    $('#display').css('width', ($(this).width() - 100) + 'px');
});​

http://jsfiddle.net/qJCc9/

#4


2  

you have missed px:

你错过了px:

$(".picLeft").css("width","300px !important"); 

I suggest to use percentage values or CSS3 Media Queries for doing this.

我建议使用百分比值或CSS3媒体查询来执行此操作。

#5


1  

Try this...

 var width = jQuery(window).width();       
 jQuery('div.picLeft').css('width':width); 
 jQuery(window).resize(function() {
        var width = jQuery(window).width();  
        jQuery('div.picLeft').css('width':width);
    });