Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Switch Between two Jquery Based Layouts (when using desktop and mobile)
    primarykey
    data
    text
    <p>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:</p> <p><img src="https://i.stack.imgur.com/oYCED.jpg" alt="enter image description here"></p> <p><strong>What is the best way of doing this?</strong> I don't want to use separate files for Mobile/Desktop. I want it to be responsive. </p> <hr> <hr> <hr> <p>Here is some of the research I have done.</p> <p><strong>Attempt 1: Pure CSS solution.</strong></p> <p>A pure CSS solution would be the best, as CSS offers media queries which makes it easy to switch between layouts. <a href="http://css-tricks.com/css3-tabs/" rel="nofollow noreferrer">CSS offers methods for creating tabs</a>. But it doesn't seem to offer a method for an iPhone style list. (You can almost fake it with an <a href="http://www.mdawson.net/csstips/purecssaccordian.php" rel="nofollow noreferrer">accordion list</a> but this doesn't take the user to a separate page like on a true iPhone app). </p> <p><strong>Attempt 2: Jquery Solution</strong> (<a href="http://jsfiddle.net/big_smile/mfB5a/3/" rel="nofollow noreferrer">see Fiddle</a>)</p> <ul> <li><a href="http://jquerymobile.com/" rel="nofollow noreferrer">Jquery Mobile</a> offers an easy way to create <a href="http://jquerymobile.com/demos/1.2.0/docs/lists/index.html" rel="nofollow noreferrer">iPhone style list views</a> </li> <li><a href="http://jqueryui.com/" rel="nofollow noreferrer">Jquery UI</a> offers an easy way to create <a href="http://jqueryui.com/tabs/" rel="nofollow noreferrer">tabs</a>.</li> <li><a href="http://wickynilliams.github.com/enquire.js/" rel="nofollow noreferrer">Enquire.js</a> offers a Javascript equivalent for media queries.</li> </ul> <p><a href="http://jsfiddle.net/big_smile/mfB5a/3/" rel="nofollow noreferrer">In this fiddle</a>, I have tried to combine Jquery Mobile and Jquery Ui. Enquire.Js is used to call the necessary library depending on screen.</p> <p>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.</p> <p>In my fiddle, I have tried to do this, but it doesn't seem to work:</p> <pre><code>$(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('&lt;link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.css" /&gt;'); $('head').append('&lt;script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"&gt;&lt;\/script&gt;'); $("#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('&lt;link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" /&gt;'); $('head').append('&lt;script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"&gt;&lt;\/script&gt;'); $("#tabs").tabs(); }).listen(); }); </code></pre> <p>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.</p> <p><strong>Attempt 3: Dom Cloning</strong> (<a href="http://jsfiddle.net/big_smile/H4vaK/5/" rel="nofollow noreferrer">see Fiddle</a>)</p> <p>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 <a href="https://stackoverflow.com/a/5557728">here</a>, but it doesn't seem to work. The dom is just removed instead of being replaced.</p> <p>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.)</p> <p><strong>Attempt 4: Dynamically Assigning Data Attributes</strong></p> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload