I want to create a responsive website. If the browser is mobile sized, the content should be displayed with an iPhone Style List View If the browser is desktop sized, the content should be displayed with tabs:
What is the best way of doing this? I don't want to use separate files for Mobile/Desktop. I want it to be responsive.
Here is some of the research I have done.
Attempt 1: Pure CSS solution.
A pure CSS solution would be the best, as CSS offers media queries which makes it easy to switch between layouts. CSS offers methods for creating tabs. But it doesn't seem to offer a method for an iPhone style list. (You can almost fake it with an accordion list but this doesn't take the user to a separate page like on a true iPhone app).
Attempt 2: Jquery Solution (see Fiddle)
- Jquery Mobile offers an easy way to create iPhone style list views
- Jquery UI offers an easy way to create tabs.
- Enquire.js offers a Javascript equivalent for media queries.
In this fiddle, I have tried to combine Jquery Mobile and Jquery Ui. Enquire.Js is used to call the necessary library depending on screen.
When a library is called, it will alter the dom structure. So the unneeded library has to be removed, otherwise if the user re-sizes the browser (i.e. switches between mobile desktop), the unused library will break cause conflicts for the called library.
In my fiddle, I have tried to do this, but it doesn't seem to work:
$(document).ready(function() {
enquire.register("screen and (max-width: 600px)", function() {
$('script[src~="jquery-ui.js"]').remove();
$('link[rel=stylesheet][href~="jquery-ui.css"]').remove();
$('head').append('<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.css" />');
$('head').append('<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"><\/script>');
$("#tabs").tabs("destroy");
}).listen();
enquire.register("screen and (min-width: 601px)", function() {
$('script[src~="jquery.mobile-1.0a3.min.js"]').remove();
$('link[rel=stylesheet][href~="jquery.mobile-1.0a3.min.css"]').remove();
$('head').append('<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />');
$('head').append('<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"><\/script>');
$("#tabs").tabs();
}).listen();
});
Even though the unused library is removed, it causes conflicts for the new library. Also, you have to re-size the browser before any of the layouts are called. If the user doesn't re-size the browser, the content will be unstyled.
Attempt 3: Dom Cloning (see Fiddle)
This attempt is similar to attempt 2, except on page load it stores the DOM in a variable. Then when the user re-sizes the browser, the existing DOM is replaced with the stored DOM. I have followed the method outline here, but it doesn't seem to work. The dom is just removed instead of being replaced.
I would appreciate any helpful with this, as I have been tearing my hair out to find a solution! (I know I have asked a question similar to this, but I felt this new question was different enough to warrant a new post.)
Attempt 4: Dynamically Assigning Data Attributes
Jquery mobile uses data attributes in the mark up to make the scripts work. I have tried applying these dynamically, so they are only written to the document when the browser is at mobile size. However, when the browser is re-sized, the desktop tabs are still broken.