Top 10 jquery mobile code snippets that you need to know

Source: Internet
Author: User
Tags set background

Jquery mobile is a framework for mobile Web apps and mobile websites with an aim to provide a unified user interface system provided SS platform mobile device platforms such iPhone, BlackBerry, Android and Windows Mobile.
Framework is built on top of one of the most popular JavaScript frameworks,
Jquery.

Followings are some of the most useful code snippets that I 've used in my recent web app developed using jquery mobile framework.

1. Disable Truncation for list items and buttons

If your list item or button has a long text, it will be truncated automatically by jquery mobile. To disable this truncation, add"white-space: normal;"To the CSS selector in question.

For example, to disable Truncation for buttons:

.ui-btn-text {white-space: normal;}

To disable Truncation for List descriptions:

.ui-li-desc {white-space: normal;}

To enable truncation, set it to"white-space: nowrap;".

2. display a random background image on page Load

Jquery mobile has a number of page initialization events that you can use to trigger certain methods on page load. the following CSS + javascript can be used to display a random background image every time a page is loaded.

CSS

.my-page  { background: transparent url(../images/bg.jpg) 0 0 no-repeat; }.my-page.bg1 { background: transparent url(../images/bg-1.jpg) 0 0 no-repeat; }.my-page.bg2 { background: transparent url(../images/bg-2.jpg) 0 0 no-repeat; }.my-page.bg3 { background: transparent url(../images/bg-3.jpg) 0 0 no-repeat; }

Javascript

$('.my-page').live("pagecreate", function() {var randombg = Math.floor(Math.random()*4); // 0 to 3$('.my-page').removeClass().addClass('bg' + randombg);});

3. disable a button action

To disable a button action (For eg: from opening a page), add the following JavaScript.

$('#home-button').button("disable");

And to re-enable it:

$('#home-button').button("enable");

4. Disable loading pop-up message

I find the loading pop-up message a bit annoying because it gets triggered everytime you load a different page. To disable this: Add the following line of code into your JS file.

$.mobile.pageLoading(true);

By default, it is enabled like so:

$.mobile.pageLoading();

5. Create a custom theme

Jquery mobile framework comes
5 themes-theme A, theme B, Theme C, Theme D and Theme E. But you can easily create a new theme for your web app.

The steps to create a new theme:
1. Copy CSS for any theme from jquery mobile CSS file and paste it into your own CSS file.
2. give your theme a name and rename the CSS selectors appropriately. your theme name can be any alphabet character (A to Z ). so for example, if you copied Theme C, and you want to call your theme Z, rename
.ui-btn-up-cTo.ui-btn-up-z,.ui-body-cTo
.ui-body-zAnd so on.
3. change colors and styles of your custom theme
4. To apply your custom theme Z to any element, just set the data-theme attribute to Z. For example:

<div data-role="page" data-theme="z">

6. Use a Custom font

There are a few font-replacement methods available such as cufon, sifr, FLIR, @ font-face and Google fonts API. when building a web app using jquery mobile, I found that @ font-face method is the easiest method to work with and the performance is quite satisfactory.
If you are interested in @ font-face, here is
Helpful tutorial with a demo on how to work with @ font-face method.

7. Create an image-only button with no text

Sometimes, you may not want to have any text for your button but still use the rest of the features that comes with a button element. this is usually the case with a home button or an info button. to hide any text associated with the button, Set Data-iconpos
Attribute to "notext". For example:

<a href="../index.html" data-icon="grid"class="ui-btn-right" data-iconpos="notext">Home</a>

8. Open a link without using Ajax with page transitions

To open a link without using Ajax with page transitions (ie: Reloading the full page the old-school way), set rel attribute to "external ".

<a href="../index.html" data-icon="grid"class="ui-btn-right" rel="external">Home</a>

9. Remove an arrow from a list item

By default, jquery mobile framework adds an arrow next to every list item. to disable this, Set Data-Icon attribute to "false" on the list item that you 'd like the arrow to be removed.

<li data-icon="false"><a href="contact.html">Contact Us</a></li>

10. Set background color of a page

This may sound simple but it took me a few minutes to figure out how to apply a background color to a page without having it overwritten by jquery mobile CSS. normally, you 'd set a background color to Body element but if you are using jquery mobile framework,
You need to set it to UI-page class.

.ui-page {background: #eee;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.