Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript更改浏览器窗口大小时更改div宽度_Javascript_Html_Css - Fatal编程技术网

如何使用javascript更改浏览器窗口大小时更改div宽度

如何使用javascript更改浏览器窗口大小时更改div宽度,javascript,html,css,Javascript,Html,Css,使用javascript(无jQuery)更改浏览器窗口大小时,如何更改div宽度? 我想在用户调整浏览器大小时动态执行此任务。 请马上帮我。。。 对css的这项工作有什么建议吗?使用百分比。例如width=“50%”这将在浏览器大小更改时更改宽度。使用百分比。例如width=“50%”这将在浏览器大小更改时更改宽度。您可以使用CSS或Javscript进行设置 使用%的ie可以轻松完成CSS div { width: 95%; } JS将很容易使用 var element = doc

使用javascript(无jQuery)更改浏览器窗口大小时,如何更改div宽度? 我想在用户调整浏览器大小时动态执行此任务。 请马上帮我。。。
对css的这项工作有什么建议吗?

使用百分比。例如
width=“50%”
这将在浏览器大小更改时更改宽度。

使用百分比。例如
width=“50%”
这将在浏览器大小更改时更改宽度。

您可以使用CSS或Javscript进行设置

使用%的ie可以轻松完成CSS

div {
    width: 95%;
}
JS将很容易使用

var element = document.getElementById("x");
window.addEventListener('resize', function(event) {
    element.style.width = window.style.width;
});

您可以使用CSS或Javscript设置此选项

使用%的ie可以轻松完成CSS

div {
    width: 95%;
}
JS将很容易使用

var element = document.getElementById("x");
window.addEventListener('resize', function(event) {
    element.style.width = window.style.width;
});

此代码应适用于所有浏览器。但是你真的应该用CSS来代替

<!DOCTYPE html >
<html>
<head>
    <meta charset="utf8" />
    <title>untitled</title>
</head>
<body>
<div id="myDiv" style=" height: 200px; margin:10px auto; background: green;"></div>

<script type="text/javascript">
var myElement = document.getElementById('myDiv');
function detectWidth(){
    var myWidth = 0;
    if(typeof (window.innerWidth)== 'number'){
        myWidth = window.innerWidth; 
    }
    else {
        myWidth = document.documentElement.clientWidth;  //IE7
    }
    myElement.style.width=myWidth-300+'px';
}
window.onload=function(){
    detectWidth();  
};
window.onresize = function (){
    detectWidth();
};
</script>
</body>
</html>

无标题
var myElement=document.getElementById('myDiv');
函数detectedwidth(){
var myWidth=0;
if(typeof(window.innerWidth)=‘number’){
myWidth=window.innerWidth;
}
否则{
myWidth=document.documentElement.clientWidth;//IE7
}
myElement.style.width=myWidth-300+'px';
}
window.onload=function(){
detectedwidth();
};
window.onresize=函数(){
detectedwidth();
};

此代码应适用于所有浏览器。但是你真的应该用CSS来代替

<!DOCTYPE html >
<html>
<head>
    <meta charset="utf8" />
    <title>untitled</title>
</head>
<body>
<div id="myDiv" style=" height: 200px; margin:10px auto; background: green;"></div>

<script type="text/javascript">
var myElement = document.getElementById('myDiv');
function detectWidth(){
    var myWidth = 0;
    if(typeof (window.innerWidth)== 'number'){
        myWidth = window.innerWidth; 
    }
    else {
        myWidth = document.documentElement.clientWidth;  //IE7
    }
    myElement.style.width=myWidth-300+'px';
}
window.onload=function(){
    detectWidth();  
};
window.onresize = function (){
    detectWidth();
};
</script>
</body>
</html>

无标题
var myElement=document.getElementById('myDiv');
函数detectedwidth(){
var myWidth=0;
if(typeof(window.innerWidth)=‘number’){
myWidth=window.innerWidth;
}
否则{
myWidth=document.documentElement.clientWidth;//IE7
}
myElement.style.width=myWidth-300+'px';
}
window.onload=function(){
detectedwidth();
};
window.onresize=函数(){
detectedwidth();
};

为什么不只使用css?您阅读过调整大小事件吗?到目前为止你试过什么?您应该使用CSS来完成这么简单的任务,因为在重载情况下,CSS执行jQuery的性能要好得多。您为什么要通过
Javascript
?使用
CSS
。您可以执行类似于
div{width:80%;}
的操作,根据窗口大小调整大小,使
div
保持在窗口的
80%
。如果不是的话,你可以看看为什么不仅仅是css?你有没有阅读过调整大小事件?到目前为止你试过什么?您应该使用CSS来完成这么简单的任务,因为在重载情况下,CSS执行jQuery的性能要好得多。您为什么要通过
Javascript
?使用
CSS
。您可以执行类似于
div{width:80%;}
的操作,根据窗口大小调整大小,使
div
保持在窗口的
80%
。如果没有,您可以查看此代码对我不起作用,因为div元素的父元素具有固定的宽度,shoulde具有固定的宽度。此代码对我不起作用,因为div元素的父元素具有固定的宽度,shoulde具有固定的宽度。到底什么不起作用?JSFIDLE将很好地帮助您解决哪些根本不起作用的问题?JSFIDLE将非常乐意帮助您