admin管理员组文章数量:1302318
I'd like to hide an element and show an other element when the user resizes his browser window to a width size that is less than 990px.
Hide largeElement when window width is less than 990px
Show smallElement when window width is less than 990px
<div id="largeElement" style="width:900px;height:100px;background-color:#cc9966"></div>
<div id="smallElement" style="display:none;width:300px;height:100px;background-color:#39c"></div>
Does anyone know of a javascript (no jQuery) that can do this?
I'd like to hide an element and show an other element when the user resizes his browser window to a width size that is less than 990px.
Hide largeElement when window width is less than 990px
Show smallElement when window width is less than 990px
<div id="largeElement" style="width:900px;height:100px;background-color:#cc9966"></div>
<div id="smallElement" style="display:none;width:300px;height:100px;background-color:#39c"></div>
Does anyone know of a javascript (no jQuery) that can do this?
4 Answers
Reset to default 5here's a plain Javascript solution:
<div id="largeElement" style="width:900px;height:100px;background-color:#cc9966"></div>
<div id="smallElement" style="display:none;width:300px;height:100px;background-color:#39c"></div>
<script type="text/javascript">
toggle();
window.onresize = function() {
toggle();
}
function toggle() {
if (window.innerWidth < 900) {
document.getElementById('largeElement').style.display = 'none';
document.getElementById('smallElement').style.display = 'block';
}
else {
document.getElementById('largeElement').style.display = 'block';
document.getElementById('smallElement').style.display = 'none';
}
}
</script>
see working example: http://jsfiddle/4p3nhy8b/
hope that helps.
Try this : Using CSS:
@media (max-width : 990px){
#largeElement{
display : none;
}
#smallElement{
display : block;
}
}
@media (min-width : 991px){
#largeElement{
display : block;
}
#smallElement{
display : none;
}
}
Using Javascript :
if(window.innerWidth < 990){
document.getElementById("largeElement").style.display = "none";
document.getElementById("smallElement").style.display = "block";
}
else{
document.getElementById("largeElement").style.display = "block";
document.getElementById("smallElement").style.display = "none";
}
Using javascript :
function fun(){
var width = screen.width;
if( width < 990 ){
document.getElementById("largeElement").style.display = "none";
document.getElementById("smallElement").style.display = "show";
}
}
load in body <body onresize="fun()">
or
window.onresize = fun;
You could do it all with CSS3 media queries
<style>
@media (max-width: 990px) {
#largeElement {
display: none;
}
#smallElement {
display: block;
}
}
</style>
I know that's not exactly what you asked for but it's the best solution to the problem IMO.
本文标签: javascriptHide element and show other if browser window width is less thanStack Overflow
版权声明:本文标题:javascript - Hide element and show other if browser window width is less than - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741674022a2391774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论