2015-04-02 14 views
2

我想改变浏览器的宽度和高度为.test股利。无论何时我重新调整浏览器的大小,当前值都会改变。重新调整div风格尊重浏览器窗口的宽度和高度 - jQuery的

$(document).ready(
 
    function() { 
 

 
    window.onresize = function(event) { 
 
     resizeDiv(); 
 
    } 
 

 
    function resizeDiv() { 
 
     var vpw=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; 
 
     var vph=window.innerHeight || document.documentElement.clientHeight ||document.body.clientHeight; 
 
     $(‘.test’).css({‘height': vph + ‘px’}); 
 
    } 
 
       
 
);
.test 
 
{ 
 
    background: green; 
 
    padding: 20px; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="test"></div>

+0

你会在'$更好高度(VPH);' – lshettyl 2015-04-02 11:22:33

回答

3

example

JS如果你想

function resize() { 
    $(".test").css({ 
    width: $(window).width(), 
    height: $(window).height() 
}); 
} 
resize(); 
$(window).resize(function(){ 
resize(); 
}); 

减去一些像素

function resize() { 
    $(".test").css({ 
     width: $(window).width() - 15, 
     height: $(window).height() - 15 
    }); 
} 
+0

运行良好。我还有一些疑问? – Azzah 2015-04-02 11:24:00

+0

?哪里不对?? – 2015-04-02 11:24:57

+0

没有错。我想从这个宽度和高度减去一些像素。我怎么能做到这一点。 – Azzah 2015-04-02 11:35:58

1

未捕获的SyntaxError:意外的标记非法

您使用“和”。为了使你的脚本来运行你需要使用“或”。

+0

是,$(' 测试')的CSS({。 'height':vph +'px'}); ==> $('。test')。css({'height',vph +'px'}); – websky 2015-04-02 11:25:41

0

您所要求的最佳做法是使用响应式设计:( '测试 '):

例如

@media only screen and (min-width: 1300px) { #divId {width:1000px;}} 

@media screen and (max-width: 860px) {#divId {width:800px;} } 

@media screen and (max-width: 650px) { #divId {width:600px;}} 

@media screen and (max-width: 480px) { #divId {width:400px;}} 
+0

我不想要css解决方案。因为我在做一些不同的用途。 – Azzah 2015-04-02 11:38:17

相关问题