1

trying to make this menu slide in and then slide out when the close button is clicked. For some reason I am unable to get it to slide out.

    jQuery(function($){
    $('#menu-open').click(function() {
        $('#slider-background').toggleClass('section-transform-active');
        $('#slider-menu').toggleClass('menu-container-active');
    });
    $('#menu-close').click(function() {
        $('#slider-background').toggleClass('section-transform-active');
        $('#slider-menu').toggleClass('menu-container-active');
    });
});

/button>



<div id="slider-background" class="section-transform">
      <button id="menu-close" style="float:right">X CLOSE</button>
  <div id="slider-menu" class="menu-container">
  <ul>
  <li>MENU ITEM 1</li>
    <li>MENU ITEM 2</li>
      <li>MENU ITEM 3</li>
        <li>MENU ITEM 4</li> 
  </ul>
  </div>
</div>

.section-transform {
    cursor: pointer;
  opacity: 0;
  -webkit-transition: width 1s ease;
    -moz-transition: width 1s ease;
    -o-transition: width 1s ease;
    -ms-transition: width 1s ease;
    transition: width 1s ease;
 }
  
.menu-container {
  padding-top: 50px;
  color: #888;
  font-size: 20px;
  opacity: 0;
}
  
.menu-container li:hover {
  color: #aaa;
}
  
.menu-container-active {
    opacity: 1;
    height: 100%;
    width: 100%;
}
  
.section-transform-active {
  opacity: 1;
    height: 100%;
    width: 50%;
    z-index: 99999;
    background: rgba(0, 0, 0, 0.8); 
}

My fiddle : https://jsfiddle.net/sad437xv/38/

In the jsfiddle I am unable to make it slide in or out. it just pops in/out. I seem to be unable to replicate the Divi builder structure too well. I would like the menu text/buttons also to slide in with the background overlay - at present the way it goes from width:0 to 50% it makes all the letters of the menu items jiggly animate as their box width expands which looks weird.

Can anyone help to show where I am going wrong here? Thanks

ganoobi
  • 41
  • 4

1 Answers1

0

You can use slideToggle() check below snippet.

$(function($){
    $('#menu-open').click(function() { 
        $('#slider-background').animate({width:'toggle'}, 250);
    });
    $('#menu-close').click(function() { 
        $('#slider-background').animate({width:'toggle'}, 250);
    });
});
.section-transform { display:none; }
.menu-container { padding-top: 50px; color: #888;font-size: 20px;}
.menu-container li:hover {color: #aaa;}
.menu-container-active {opacity: 1;height: 100%;width: 100%;}
.section-transform-active {opacity: 1;height: 100%;width: 50%;z-index: 99999;background: rgba(0, 0, 0, 0.8);}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="menu-open">MENU OPEN</button>
<div id="slider-background" class="section-transform">
  <button id="menu-close" style="float:right">X CLOSE</button>
  <div id="slider-menu" class="menu-container">
    <ul>
      <li>MENU ITEM 1</li>
      <li>MENU ITEM 2</li>
      <li>MENU ITEM 3</li>
      <li>MENU ITEM 4</li> 
    </ul>
  </div>
</div>
Bhautik
  • 11,125
  • 3
  • 16
  • 38
  • Thanks very much Bhautik - so much simpler! One question - is there a way to make it go left/right rather than up/down. I tried to use this code I found: slideToggle("slow", {direction: "left"}, 100); but that breaks it. – ganoobi Mar 31 '21 at 06:44
  • You can check other answers here [slideToggle JQuery right to left](https://stackoverflow.com/questions/17271169/slidetoggle-jquery-right-to-left) – Bhautik Mar 31 '21 at 06:58
  • Thanks. I did your edits, now it comes in from top left corner diagonally to fill screen? I looked at other answers you linked and tried this: .toggle("slide", { direction: "left" }, 200); but that just generates an error in the console and keeps on generating the same error forever till window closed! (jquery-migrate.min.js?ver=3.3.2:2 Uncaught TypeError: Cannot read property 'length' of undefined) – ganoobi Mar 31 '21 at 14:48
  • Do you have an example that what you need? – Bhautik Mar 31 '21 at 15:24
  • Hi Bhautik, sorry for the delay. I am trying to achieve basically that menu slides in 50% to 100% depending on screen size (mostly opaque/dark background) - but that it slides in from left - its height is full screen and width 50% (mobile 100%). This fiddle is a box version : https://jsfiddle.net/1zn5Lepm/23/ but this jQuery code does not work in Divi/Wordpress. Your edits are perfect except for the fact the "menu" comes in from top left corner like a box expanding diagonally – ganoobi Apr 03 '21 at 05:52
  • Where are you putting this code? this `jsfiddle.net/1zn5Lepm/23` is working fine as per your need. – Bhautik Apr 03 '21 at 06:28
  • Yes it works in jsfiddle, but it doesnt work in Wordpress. I am using Divi Theme but the jQuery support is in the Wordpress layer and it does not seem to support that code. It generates an endlessley repeating error "Cannot read property 'length' of undefined)" – ganoobi Apr 04 '21 at 05:20
  • jQuery includes in WordPress by defaults. I think you need to debug more into this. – Bhautik Apr 04 '21 at 09:41
  • OK I think I found a problem - I am using wordpress button to do trigger click. If I use just text to do it the code does not give error and it mostly works. I think I can fix from there. When I use worpress button then it seems it keeps reloading the page for some reason and also it makes an error - the page loading is whjat is making the error keep repeating in console – ganoobi Apr 04 '21 at 13:55
  • Okay great. let me know if you have still an issue. – Bhautik Apr 04 '21 at 16:43
  • Cool. Thanks very much for your patience and help! I have it working now almost as I want. It animates the width of the container div from 0-50% or 100% and the only weird thing is the menu contents animate text from a single column as the div widens. Not a train smash tho :) – ganoobi Apr 05 '21 at 08:22
  • Can you share the site link or create a snippet. – Bhautik Apr 05 '21 at 08:31
  • Hi - at present its karlowens.com/mikeDev. Just starting it! :) – ganoobi Apr 05 '21 at 17:26
  • Yes, I am still trying to make the jsfiddle work. I have simplified it a lot (ie no menu for left right etc) and it works perfectly in jsfiddle but I am struggling to make it work on the wordpress menu. I used another method from here (https://www.laravelcode.com/post/how-to-create-jquery-slide-left-and-right-toggle-effect) but I am not entirely satisfied with the animation. It is not as smooth but it was easier to adapt to what I needed. – ganoobi Apr 06 '21 at 06:14
  • Also to make menu slide 50% for larger width than tablet and 100% for smaller max-width than tablet. I have changed it back now to the jsfiddle version, but I cannot clear the slider menu from the screen. It stays on (50%) all the time. The
    I cannot seem to match in wordpress/Divi structure to make it same as jsFiddle
    – ganoobi Apr 06 '21 at 06:14
  • At the moment I am using the jsFiddle code. Now I am getting that same error generating over and over again (console error number count in chrome is just running like a clock timer forever like infinite loop!). [jquery-migrate.min.js?ver=3.3.2:2 Uncaught TypeError: Cannot read property 'length' of undefined] – ganoobi Apr 06 '21 at 06:30
  • I can check that issue but I need access. – Bhautik Apr 06 '21 at 07:21
  • OK I sent you a message. I just cannot make that jsfiddle version work. I like the way it animates, its perfect. But the other one I can stop it halfway if desktop screen and it works without problems now, its just that the animation by way of expanding width is not as good looking as the other one. The one using jsfiddle is active at moment but it does not work – ganoobi Apr 06 '21 at 12:07