将Javascript变量转换为css
我正在处理一个小的html页面。我正在使用javascript获取屏幕的尺寸。我需要将该变量传递给css样式,如下所示。我该怎么做?我还注意到我需要把“px”放在值的末尾,比如top:100px而不是top:100。如何将其添加到变量中?谢谢你的帮助将Javascript变量转换为css,javascript,html,css,variables,Javascript,Html,Css,Variables,我正在处理一个小的html页面。我正在使用javascript获取屏幕的尺寸。我需要将该变量传递给css样式,如下所示。我该怎么做?我还注意到我需要把“px”放在值的末尾,比如top:100px而不是top:100。如何将其添加到变量中?谢谢你的帮助 <script type="text/javascript"> var percent =1; var myWidth = Math.round( screen.width * percent); var myH
<script type="text/javascript">
var percent =1;
var myWidth = Math.round( screen.width * percent);
var myHeight = Math.round( screen.height * percent);
</script>
<style type="text/css">
div.content {
margin: auto;
top: myHeight ;
width: myWidth ;
}
</style>
风险值百分比=1;
var myWidth=Math.round(screen.width*百分比);
var myHeight=Math.round(screen.height*百分比);
部门内容{
保证金:自动;
顶部:我的身高;
宽度:myWidth;
}
使用纯JS,您可以通过获取所有div,然后过滤内容
类名
对于较新的浏览器(尤其是IE):
- 您也可以这样做,然后过滤所有div李>
- 您可以使用直接返回匹配列表的
currentDiv.style.top = myHeight;
currentDiv.style.width = myWidth;
你也可以考虑使用不确定的东西,如果这是你的选择。
var myCss = "div.content { margin: auto; top: " +
myHeight + "; width: " + myWidth + " ; }";
var myStyle = document.createElement('style');
myStyle.type = 'text/css';
myStyle.styleSheet ? myStyle.styleSheet.cssText = myCss :
myStyle.appendChild(document.createTextNode(myCss));
document.getElementsByTagName("head")[0].appendChild(myStyle);
这将创建您描述的样式元素,并将其附加到文档的开头,然后应用它。如果您打算使用更少的CSS,您可以尝试这样做:
@percent: 1;
@height: `Math.round( screen.height * @{percent})`;
@width: `Math.round( screen.width * @{percent})`;
div.content {
margin: auto;
top: @height;
width: @width;
}
var percent=1;
$("div.content").css('top', Math.round( screen.height * percent)+'px');
$("div.content").width(Math.round( screen.width * percent));
var percent=1;
document.querySelector('div.content').style.top = Math.round( screen.height * percent)+'px';
document.querySelector('div.content').style.width = Math.round( screen.width * percent)+'px';
如果您打算使用JQUERY,您可以尝试以下方法:
@percent: 1;
@height: `Math.round( screen.height * @{percent})`;
@width: `Math.round( screen.width * @{percent})`;
div.content {
margin: auto;
top: @height;
width: @width;
}
var percent=1;
$("div.content").css('top', Math.round( screen.height * percent)+'px');
$("div.content").width(Math.round( screen.width * percent));
var percent=1;
document.querySelector('div.content').style.top = Math.round( screen.height * percent)+'px';
document.querySelector('div.content').style.width = Math.round( screen.width * percent)+'px';
如果您打算使用JS您可以尝试以下方法:
@percent: 1;
@height: `Math.round( screen.height * @{percent})`;
@width: `Math.round( screen.width * @{percent})`;
div.content {
margin: auto;
top: @height;
width: @width;
}
var percent=1;
$("div.content").css('top', Math.round( screen.height * percent)+'px');
$("div.content").width(Math.round( screen.width * percent));
var percent=1;
document.querySelector('div.content').style.top = Math.round( screen.height * percent)+'px';
document.querySelector('div.content').style.width = Math.round( screen.width * percent)+'px';
jQuery适用于所有浏览器,甚至包括Internet Explorer,但您需要jQuery。JavaScript适用于除Internet Explorer之外的所有浏览器。或者,您可以使用纯CSS,当禁用JavaScript时,它可以工作。试试哪个效果最好:) JavaScript 或jQuery 或CSS
有什么特殊原因不能在样式规则集中使用百分比吗?媒体查询也可能有用。你想在什么类型的屏幕上实现什么?智能手机、台式机?都是不错的回答。谢谢,我将使用JQuery