簡體   English   中英

根據瀏覽器窗口的寬度和高度調整div樣式的大小-jQuery

[英]Re-size a div style respect to browser window width and height - Jquery

我正在嘗試將瀏覽器的寬度和高度更改為.test div。 每當我調整瀏覽器大小時,當前值都應更改。

 $(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> 

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
    });
 }

Uncaught SyntaxError:意外的令牌非法

您使用'和'。 為了運行腳本,您需要使用'或'。

您所要求的最佳實踐是使用響應式設計: http : //sensitivedesign.is/examples/playgrounds-and-sandboxes/lots-of-donuts

例如:

@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;}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM