Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

My travelblog.ws project is built as a responsive site so that I do not have to have a desktop and a mobile version of the site. Unfortunately some features are hard to implement generically and need a specific mobile version. One of these is the 'trip timeline'. On the desktop version it appears as a sidebar, on the mobile version it is a pop-over DIV that covers the whole page. The pop-over is triggered by pressing the 3-bar stack icon at the top of the page.

This is what the two versions look like...
respvistoggle_1.png

respvistoggle_2.png


My use cases were: 1. display the desktop sidebar for desktop sized pages 2. do not display any sidebar on mobile version 3. allow a toggle of the timeline on the mobile version only.



What I realised very quickly was that if I simply toggled the mobile version of my timeline that as soon as the browser window would be resized to 'desktop' size it would mess up my page because the mobile version of the timeline would still be visible.

So instead of using the jQuery toggle() method to toggle the mobile timeline DIV, I used the toggleClass() method instead. That required me to set up some CSS as follows:
 CSS
.mobile_popover {
display: none;
...
}
.mobile_visible {
display: none;
}
@media screen and (max-width: 768px) {
.mobile_visible {
display: block;
}
}


...and the HTML for the mobile timeline DIV is something like...
 HTML
<div class="mobile_popover">...</div>


The mobile timeline DIV has the mobile_popover class set, so it's not visible by default. The mobile_visible class is set up with a media query to only appear on the 'mobile' sized browser window.

A bit of JavaScript is then used to toggle the mobile timeline DIV on/off by toggling whether it has the mobile_visible class assigned like so...
 JavaScript
$('.mobile_popover').toggleClass('mobile_visible');


When toggled on, the mobile_visible class overwrites the 'display' property of mobile_popover so the timeline becomes visible.

Now if the mobile timeline is toggled on and the window is resized back to desktop size because the mobile_visible class is set to not being displayed on a 'desktop' sized window, the timeline will disappear. As soon as the window is resized back to 'mobile' size, the timeline appears again.

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.