admin管理员组文章数量:1287656
screen_w = window.innerWidth;
screen_h = window.innerHeight;
to get the window deminsions and then set it to a variable. Is there a way to take that variable and then use it in my style?
Such as:
img
{
width:screen_w;
height:screen_h;
}
I have tried
<script type="text/javascript">
<style>
img..
</style>
</script>
but that doesn't seem to work.. is this even possible or is there some other way to pass a variable?
Thanks in advance.
///Added
Ok so the Link down there ---v
Is discussing making a node that is your "style"
<script type="text/javascript">
var head = document.getElementsByTagName('head')[0],
style = document.createElement('style'),
rules = document.createNode('img { width:screen_w; height:screen_h; }');
style.type = 'text/css';
if(style.styleSheet)
style.styleSheet.cssText = rules.nodeValue;
else style.appendChild(rules);
head.appendChild(style);
</script>
so then for my html I just have to insert my picture.
<img src="slide1.jpg">
but its still not quite passing somehow.
I can however get the "document.getElementById to work. Is there a way to do this without a button.. like onLoad()?
function changeSize()
{
document.getElementById("picture").height= screen_h;
document.getElementById("picture").width= screen_w;
}
</script>
</head>
<body>
<img id="picture" src="slide1.jpg" width="300" height="98" />
<br /><br />
<input type="button" onclick="changeSize()" value="Change size of image" />
screen_w = window.innerWidth;
screen_h = window.innerHeight;
to get the window deminsions and then set it to a variable. Is there a way to take that variable and then use it in my style?
Such as:
img
{
width:screen_w;
height:screen_h;
}
I have tried
<script type="text/javascript">
<style>
img..
</style>
</script>
but that doesn't seem to work.. is this even possible or is there some other way to pass a variable?
Thanks in advance.
///Added
Ok so the Link down there ---v
Is discussing making a node that is your "style"
<script type="text/javascript">
var head = document.getElementsByTagName('head')[0],
style = document.createElement('style'),
rules = document.createNode('img { width:screen_w; height:screen_h; }');
style.type = 'text/css';
if(style.styleSheet)
style.styleSheet.cssText = rules.nodeValue;
else style.appendChild(rules);
head.appendChild(style);
</script>
so then for my html I just have to insert my picture.
<img src="slide1.jpg">
but its still not quite passing somehow.
I can however get the "document.getElementById to work. Is there a way to do this without a button.. like onLoad()?
function changeSize()
{
document.getElementById("picture").height= screen_h;
document.getElementById("picture").width= screen_w;
}
</script>
</head>
<body>
<img id="picture" src="slide1.jpg" width="300" height="98" />
<br /><br />
<input type="button" onclick="changeSize()" value="Change size of image" />
Share
Improve this question
edited May 26, 2011 at 20:56
hobbywebsite
asked May 26, 2011 at 19:46
hobbywebsitehobbywebsite
1431 gold badge2 silver badges14 bronze badges
1
- Could perhaps use js to create a new style element with the desired css? (this is just speculation) – geekchic Commented May 26, 2011 at 19:50
3 Answers
Reset to default 4You can't pass variables to CSS.
You can, however, use javascript directly to change the image style (including dimensions).
You can use JS to modify CSS, see this question:
How to create a <style> tag with Javascript
<script >
window.onload = function() {
$.ajax({url: "api/<route-name>", success: function(result){
console.log(result.data[16]['value']);
document.documentElement.style.setProperty('--my-variable-name', result.data[14]['value']);
}});
}
<style>
.gender_radio .radio-list .is-checked {
background-color: var(--my-variable-name) !important;
}
本文标签: javascript pass variable to styleStack Overflow
版权声明:本文标题:javascript pass variable to style - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741317711a2372016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论