Change Icon on jQuery Mobile Collapsible Sets - jquery-mobile

I'm working with jQuery Mobile for the first time and I'm having trouble accomplishing what seems like a simple change to the icons on a collapsible set (accordion).
I want to change the icon for each of the headings on a collapsible set to an up arrow for the expanded state and a down arrow for the collapsed state.
I've created a Fiddle that seems to have the same problem with code that I pretty much copied directly from the jQuery mobile site and modified a little.
Any help or pointers on this would be much appreciated. Thanks!

The docs page you refered actually uses the latest Work in Progress version of jquery mobile.So this is a feature we can expect in future versions of JQM.In the current stable version,we will not be able to change the icon by means of specifying a data attribute.
Here is an eg: which uses the Work in Progress version of jqm - http://jsfiddle.net/AAYjF/
But it is advisable to use the stable version.So you can use the following code to acheive this:
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
<style>
.ui-collapsible .ui-icon{
background-position: -180px 50%;/*Position of up icon in icon sprite*/
}
.ui-collapsible-collapsed .ui-icon{
background-position: -216px 50%;/*Position of down icon in icon sprite*/
}
</style>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>My Title</h1>
</div><!-- /header -->
<div data-role="content">
<div data-role="collapsible-set">
<div data-role="collapsible">
<h3>Section 1</h3>
<p>I'm the collapsible set content for section B.</p>
</div>
<div data-role="collapsible" >
<h3>Section 2</h3>
<p>I'm the collapsible set content for section B.</p>
</div>
</div>
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>
The logic is to use the background position of the down and up icons in the icon sprite for the collapsed and expanded icons
Demo : http://jsfiddle.net/6x8ew/

I'm sure you can improve upon this solution but here is a basic idea on one way to accomplish this without hacking into the JQM source.
$(document).on('pageinit',function(){
$('.ui-collapsible .ui-icon').removeClass('ui-icon-plus').addClass('ui-icon-arrow-u');
$('[data-role=collapsible]')
.on('expand',function(){
$(this).find('.ui-icon').removeClass('ui-icon-arrow-u').addClass('ui-icon-arrow-d');
})
.on('collapse',function(){
$(this).find('.ui-icon').removeClass('ui-icon-arrow-d').addClass('ui-icon-arrow-u');
});
});​
I am using jqm's collapsible events to switch to the appropriate icon.
See my working example.

I haven't done much with jQuery Mobile yet, so take it for what it's worth.
I think you need to put the attributes on the <div data-role="collapsible-set">, not on each data-role="collapsible". Obviously this would only work for all the children, and it wouldn't let you target individual children.

With jquery mobile version 1.4.0, you can accomplish this by having the following code in your CSS file
For custom right arrow
.ui-icon-arrow-r:after {
background: url("Path to your image file") no-repeat scroll 0px 0px transparent;
}
For custom down arrow
.ui-icon-arrow-d:after {
background: url("Path to your image file") no-repeat scroll 0px 0px transparent;
}

Related

Using custom icons on left and right in jquery mobile list

I'm trying to figure out how to add custom icons on the left and right side of a list item. I found this example (see link and code below) but if you notice the icon does not show up, it's just an empty circle. At first I thought it was because the link was invalid but I tried it on my page with my own image but the same thing happened. Unfortunately I only have a reputation of 13 so I am not allowed to post pictures, however here is a sample of how I would like the li item to have icons on both ends.
-*****************************-
| icon_left TEXT icon_right |
-*****************************-
<html>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
<style>
.ui-icon-myapp-smiley {
background-image: url(http://swiftthemes.com/forums/images/icons/icon6.png) !important;
}
</style>
<div data-role="page">
<div data-role="content">
<ul data-role="listview">
<li data-icon="myapp-smiley">My LI!!!</li>
</ul>
</div>
</div>
</html>
Code on jsfiddle page http://jsfiddle.net/KYaQT/
jQM 1.0.1 is quite old and outdated now. You should upgrade to 1.4 and jQuery 1.9 or greater.
<ul data-role="listview" data-inset="true" >
<li data-icon="myicon"><span class="ui-icon-myicon2 ui-btn-icon-notext inlineIcon"></span>Text in list item 1</li>
</ul>
.ui-icon-myicon:after {
background-image: url("http://people.mozilla.org/~shorlander/files/australis-linux-svg-test/images-linux/toolbarButton-bookmark.svg");
}
.ui-icon-myicon2:after {
background-image: url("http://forum.librecad.org/images/gear.png");
}
.inlineIcon {
display: inline-block;
position: relative;
vertical-align: middle;
margin-right: 6px;
}
For the icon on the right you had the correct approach. Set the data-icon attribute on the <LI>. In jQM 1.4, the CSS uses the pseudo :after element for the background image.
To add an icon on the left, you can put a span inline with the text and add CSS to position it.
If you don't want the gray disk behind the icon, set the background color transparent:
.ui-icon-myicon2:after {
background-image: url("http://forum.librecad.org/images/gear.png");
background-color: transparent;
}
Here is a DEMO

Jquery Mobile overlay is turned in to page

I am using jquery mobile and wanted a overlay in to be visible when ever i need. I have this at first in my html
<body>
<div id="overlay"></div>
</body>
(if you are wondering about other pages then they are added afterwards)
but when jquery mobile 1.4.2 initializes it turns my div in to a page like so:
<div data-role="page" data-url="/" tabindex="0" class="ui-page ui-page-theme-a" style="min-height: 640px;">
<div id="overlay">
</div>
</div>
How can i make Jquery not touch this element?
Let me guess you are using it to show a splash screen.
jQuery Mobile requires at least one page to exist during initialization.
To solve this problem add one dummy jQuery Mobile page and just set it CSS to display: none; something like this:
<body>
<div id="overlay"></div>
<div data-role="page" style="display: none;">
<div/>
</body>
I seems it only does it if there are no pages initially in the body. but if you add a blank page every works fine:
<div data-role="page"></div>

jQuery-Mobile - GAP between header and content

When navigating with $.mobile.changePage("#page2"); I realized that jQM adds 1-3px gap under the header on the next page.
When calling the photolibrary with Cordova to select a photo, the gap will disappear, but I don't need the camera plugin, I want the gap not to appear in the first place.
(I am not talking about the white gap that gets added to the bottom when calling camera plugin - and can be resolved by changing the self web view in MainViewController under viewWillAppear - or any other of the gap issues I found).
The gap I am talking about is between header and ui-content.
I have been working with jQM before and the versions I used in past projects have not caused any similar problem.
I am using jQM 1.4.2, JQuery 1.11.0 and Cordova 3.1.0.
Here are 2 images, I changed to colors to make the problem more obvious.
darkgray -> header
red -> ui-page background
blue -> first (and only) DIV element in ui-content
1 image: no gap - this is if the page is the INDEX page
2 image: this is when the page is page 2 and gets navigated to with changePage
Between header and first DIV in content you can see a gap.
I have commented out every single line of code, except the basic structure and those to color the elements. Plus, the page WILL appear without gap IF it is the MAIN page. However, if I have a different main page and tap on a div which calls $.mobile.changePage("#page2"); the second page will be shown with a gap between header and content.
edit - UPDATE:
I created a clean project just to reproduce this issue. I don't know how to resolve it and if I can't fix this I can't continue with any project, because this happens in every Project with jQM 1.4.2:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />
<link rel="stylesheet" href="jquery.mobile-1.4.2.css" />
<script src="jquery-1.11.0.min.js"></script>
<script src="jquery.mobile-1.4.2.js"></script>
<script>
$(document).on('tap', '#gotop2', function(event){
event.preventDefault();
//$.mobile.changePage('#p2');
$.mobile.pageContainer.pagecontainer("change", "#p2")
});
</script>
</head>
<body>
<div data-role="page" id="p1">
<div data-role="header" id="header" data-position="fixed" data-tap-toggle="false" data-id="h1">
<h1>page 1</h1>
</div>
<div data-role="content" style="padding:0px; margin:0px;">
<div id="gotop2">go to page2</div>
</div>
</div>
<div data-role="page" id="p2">
<div data-role="header" id="header" data-position="fixed" data-tap-toggle="false" data-id="h1">
<h1>page 2</h1>
</div>
<div data-role="content" style="padding:0px; margin:0px;">
<div id="blueblock" style="height:20px; width:100%; background-color:#22272c; background-color:blue; padding:0px !important; margin:0px !important;"></div>
</div>
</div>
</body>
Don't forget to include jQ or jQM in the folder.
If you call "/index.html" in you browser and navigate to the second page you will see the gap.
If you call "/index.html#p2" directly in the browser there won't be any gap.

JqueryMobile - My Page Appears Before transition

I am setting up an app with JqueryMobile. Whenever I click on my navigation the next page appears on top of the current page for a second and then disappears and then the transition starts and the next page appears. Is this a known problem that anyone else is having and if so, how can I fix it. I know there's an issue with flashing transitions but I don't think this is the same. I'm using un-altered jquerymobile docs. Below is my code:
https://gist.github.com/2401211
Your complaint/issue is a common one. The transitions can be especially hideous on an android 2.x devices. Here is a quote form their blog
We did a ton of work leading up to 1.0 to make our transitions as
smooth as possible, but there were two significant things that turned
out to very difficult: the need to scroll the page between transitions
and Android’s poor animation performance. Source
That being said my suggestion would be to update to the JQM version 1.1.0 final.
Use this to get started
<html>
<head>
<title>My Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>My Title</h1>
</div><!-- /header -->
<div data-role="content">
<p>Hello world</p>
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>
Try this fix:
.ui-page {
-webkit-backface-visibility: hidden;
}
This may be an issue with using the same ID. Check to make sure your ID is different on each of your pages. That's what it sounds like to me
I've been facing with this problem several times, and just when I was going to suicide, found that the problem was in some custom CSS classes, and I've fixed it!
For example, I had in my body declaration:
body{
margin: 0px;
}
Remove margin, and voilá! Something was fixed!
In other app, I found at a custom wrapper class declaration:
.wrapper{
<blabla>
position:absolute;
<blabla>
}
Of course, this "Absolute" forces the engine to render the page at the absolute position, and then, initiates the transition.
So, how to fix your issue?
I recommend that you remark all your .css file, and start testing class by class, try the transition, and when the transition fails, there you have the "disturbing" class.
Try it, and let us know if it fixes the issues to you!

Adjust Dialog Width jQuery Mobile to non-Ajax loaded page

I need to open a popup window to a custom size. Using suggestions from jQuery mobile docs I can get the custom size to work when I load the page with ajax (in the same .html document). When I have a separate .html document, it doesn't load the custom width I specify.
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="IE=Edge;chrome=1" >
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css" />
<script src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>
<style>
.ui-dialog .ui-header,
.ui-dialog .ui-content,
.ui-dialog .ui-footer {
max-width: 800px;
margin: 10% auto 15px auto;
}
</style>
</head>
<body>
<div data-role="page" id="popup">
<div data-role="content">
<a data-role="button" href="#" data-rel="back">back</a>
</div>
</div>
</body>
</html>
This is the code. If I link to it within the same file, and just use
popup
It works fine. You can see the css rules that jQuery Mobile says to add if you want to change the width (jQuery Mobile Dialog docs).
I answered my own question. I found that with the ajax loading, when the page is fetched it only gets the content from the target page. If I want to provide some custom CSS to be applied to the target page, I need to have it on the parent page (or the page that is calling the popup).

Resources