Click here to Skip to main content
15,887,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<style>
	.dv1{
    	width:200px;
        height:50px;
        background:green;
    }
    .dva,.dvb{
    	width:200px;
        height:150px;
        background:lightgreen;
        margin-top:0;
    }
    .article{
    	width:202px;
        background:pink;
        border: 1px solid red ;
    }
</style>
</head>
<body onresize="myFunction()">

<p>Try to resize the browser window to display the windows height and width.</p>

<p id="demo"></p>
<div class="article">
<div class="dv1"><button class="btn2a">Slide down Drinkss</button></div>
<div class="dva">
	<h3>List of Drinks:</h3>
      <ul>
        <li>Coffee</li>
        <li>Tea</li>
        <li>Coca Cola</li>
      </ul>
      
</div>
<button class="btn1a">Slide up</button>
</div>
<div class="article">
<div class="dv1"><button class="btn2b">Slide down Alcohols</button></div>
<div class="dvb">
	<h3>List of Alcohols:</h3>
      <ul>
        <li>Beer</li>
        <li>Vine</li>
        <li>Spirits</li>
      </ul>
      
</div>
<button class="btn1b">Slide up</button>
</div>
<!--
<button class="btn1">Slide up</button>
<button class="btn2">Slide down</button>-->
<script>
$(document).ready(function(){
  
});
function myFunction() {
  let w = window.outerWidth;
  let h = window.outerHeight;
  let txt = "Window size: width=" + w + ", height=" + h;
  document.getElementById("demo").innerHTML = txt;
  if (w<800){
   $(".dva").hide();
   $(".dvb").hide();
   
   $(".btn1a").show();
   $(".btn2a").show();
   $(".btn1b").show();
   $(".btn2b").show();
   $(".btn1a").click(function(){
    $(".dva").slideUp();
    
   });
   $(".btn1b").click(function(){
    
    $(".dvb").slideUp();
   });
  $(".btn2a").click(function(){
    $(".dva").slideDown();
    
  });
  $(".btn2b").click(function(){
    
    $(".dvb").slideDown();
  });
  $(".article").css("display","block");
  }
  else{
   $(".dva").show();
   $(".dvb").show();
   $(".btn1a").hide();
   $(".btn2a").hide();
   $(".btn1b").hide();
   $(".btn2b").hide();
   $(".article").css("display","inline"); 
   //$(".btn2").css("display","inline");   
  }
}
</script>

</body>
</html>


What I have tried:

As viewport is collapsing and getting close to mobile devices display inline is not working, but there are other problems too. Between itemdiv and title/button div, there is a gap that is not expexted to be. So on monitor, all divs containing choice items supposed to be visible, on mobile device should be hidden under title bars waiting to be clicked. I try to control the megamenu with resize funcion resizing window and change display inline on divs containing choices, to display block but it does not work. Thank you frank!
Posted
Updated 28-Aug-23 9:29am
v4

1 solution

The interaction between your CSS and the JavaScript code that handles the visibility of the 'divs' on different screen sizes seems to be your problem.

I have added a new CSS class called '.mobile-layout' that hides the '.dva' and '.dvb' divs when applied. In your JavaScript section, I am also adding and removing the '.mobile-layout' class to your 'body' element based on the window width. This will control the visibility of your divs on mobile screens -

HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<style>
    /*Your existing styles here as you have it above in your code... */

    /*I have added the new CSS class to handle the mobile layout... */
    .mobile-layout .dva,
    .mobile-layout .dvb {
        display: none;
    }
</style>
</head>
<body onresize="myFunction()">

<p>Try to resize the browser window to display the window's height and width.</p>

<p id="demo"></p>
<div class="article">
<div class="dv1"><button class="btn2a">Slide down Drinks</button></div>
<div class="dva">
    <h3>List of Drinks:</h3>
    <ul>
        <li>Coffee</li>
        <li>Tea</li>
        <li>Coca Cola</li>
    </ul>
</div>
<button class="btn1a">Slide up</button>
</div>
<div class="article">
<div class="dv1"><button class="btn2b">Slide down Alcohols</button></div>
<div class="dvb">
    <h3>List of Alcohols:</h3>
    <ul>
        <li>Beer</li>
        <li>Vine</li>
        <li>Spirits</li>
    </ul>
</div>
<button class="btn1b">Slide up</button>
</div>

<script>
$(document).ready(function(){
    //Your jQuery code here...
});

function myFunction() {
    let w = window.outerWidth;
    let h = window.outerHeight;
    let txt = "Window size: width=" + w + ", height=" + h;
    document.getElementById("demo").innerHTML = txt;

    //Check if the window width is less than 800 pixels...
    if (w < 800) {
        //Add the 'mobile-layout' class to the body, you are on a mobile phone...
        $("body").addClass("mobile-layout");
    } else {
        //Remove the 'mobile-layout' class from the body...
        $("body").removeClass("mobile-layout");
    }
}
</script>

</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900