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?


Share Improve this question asked Dec 4, 2014 at 11:40 MacchiatoMacchiato 2604 gold badges9 silver badges23 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

here'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