Top 10 jQuery Mobile Code Snippets that you need to know

jQuery Mobile is a framework for mobile web apps and mobile websites with an aim to provide a unified user interface system across many mobile device platforms such iPhone, BlackBerry, Android and Windows Mobile. The 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 with 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 Theme Z, rename.ui-btn-up-c to .ui-btn-up-z.ui-body-c to .ui-body-z and 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 a 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;
}

web development blog(转)的更多相关文章

  1. Web开发秘方(WEB DEVELOPMENT RECIPES)[47.5MB] PDF扫描版

    不借助插件怎样在移动设备上实现动画效果?怎样快速搭建HTML电子邮箱?怎样制作跨PC和移动设备显示的应用界面?怎样利用最新的JavaScript框架提高应用的响应速度?怎样有效利用CoffeeScri ...

  2. Learning web development with MDN

    Learning web development with MDN Server-side website programming Dynamic Websites – Server-side pro ...

  3. Beginners Guide To Web Development

    Web Development Front End Development Back End Development

  4. Web Development Terms

    I've come across lots of terms while learning web development. I'm feeling myself overwhelmed. Here ...

  5. Reloading Java Classes 301: Classloaders in Web Development — Tomcat, GlassFish, OSGi, Tapestry 5 and so on Translation

    The Original link : http://zeroturnaround.com/rebellabs/rjc301/ Copyright reserved by Rebel Inc In t ...

  6. 《Agile Web Development With Rails》读后感--rails基于web设计的best Practices

    最近看完<Agile Web Development with Rails>一书,受益匪浅.书中先是用一个简单的web应用带你进入Rails的世界,然后在你大致熟悉之后,再带你了解Rail ...

  7. 《Web Development with Go》实现一个简单的rest api

    设计模式完了之后,应该实现具体的应用了. 设计模式还得没事就要复习. web应用,学习的是网上的一本书. <Web Development with Go> package main im ...

  8. 【外文阅读】Web Development in 2020: What Coding Tools You Should Learn---Quincy Larson

    原文链接:https://mail.qq.com/cgi-bin/readtemplate?t=safety&check=false&gourl=https%3A%2F%2Fwww.f ...

  9. Full Stack Web Development

    Full Stack Web Development Web Stacks MEAN (Mongo, Express, Angular and Node) LAMP (Linux, Apache, M ...

随机推荐

  1. 观察者模式与Guava EventBus

    观察者模式 结构图 代码实现 public abstract class Subject { private List<Observer> observerList = new Array ...

  2. OTL翻译(10) -- OTL的流缓冲池

    OTL的流缓冲池 一般来讲,流一般作为一个局部的变量被使用,当使用完毕后就立刻关闭,如果需要再次使用就需要再次的声明变量,如此循环.OTL流的缓冲池(内存池)是一个解决以往的流性能低下的一个机制.当流 ...

  3. Python源码学习七 .py文件的解释

    Python源码太复杂了... 今天看了下对.py文件的parse, 云里雾里的 py文件是最简单的, 在python的交互式窗口 import这个模块 a = 10 print(a) 开始分析,堆栈 ...

  4. Android导航抽屉-Navigation Drawer

    Google今年七月份的时候更新了他们的Google+应用,采用了新的导航方式并抛弃了navigationdrawer.一时之间,又引发了一系列关于NavigationDrawer利弊的讨论,不过对于 ...

  5. 【反射】Reflect Class Field Method Constructor

    关于反射 Reflection 面试题,什么是反射(反射的概念)? 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义 ...

  6. [CSS3] Parent relative and child absoulte

    如果在一个容器中的子元素使用了position:absolute, 那么他可能会跑出父元素的框架范围. 如果想限定子元素在付元素的框架范围,可以在父元素上加position:relative; 对于一 ...

  7. 如何使用FF的Firebug组件中的net工具查看页面元素加载消耗时间

    1.安装FF的Firebug组件:点击FF的Tools的Add-ons菜单,输入Firebug关键字,并选择合适的版本Install. 2.安装完毕后地址栏右边会出现一个小虫图标,右边还有一个下拉箭头 ...

  8. 【Python学习笔记】

    目录 语法 if语句 while循环 字符串常用操作 列表 只读列表元组 字典的使用 语法 if语句 示例 #!/usr/bin/env python user = 'jack' passwd = ' ...

  9. 【解决】缺少libstdc++.so.6库的原因及解决的方法

    问题原因: 系统是64bit,该库是32bit的,在64bit系统上安装32bit库 解决的方法: 1. 查看哪个安装包包括该库:yum provides libstdc++.so.6    libs ...

  10. 修改字段结构之ArcGIS Diagrammer

    在ArcGIS中,修改字段名称.类似和物理顺序是一件不好办的事,特别是需要修改字段比较多的情况下.通常的做法是新建字段-字段计算器赋值-删除原有字段的方法来达到修改的目的.这里介绍另外一种方法. 现需 ...