移动开发10段常用的JavaScript代码 - 满天窜

stu-xu 2021-08-06 原文


移动开发10段常用的JavaScript代码


在开发移动网站过程中,有一些Javascript代码会经常用到。以下是10段常用的JavaScript代码。需要注意的是某几段代码需要jQuery mobile framework支持。

1.如果网页是在iPhone或Android浏览器中查看,则在主体元素中添加“iPhone”或“Android” 类名

if (navigator.userAgent.match(/iPhone/i)) {
	$(\'body\').addClass(\'iPhone\');
} else if (navigator.userAgent.match(/Android/i)) {
         $(\'body\').addClass(\'Android\');
}

iPhone用户浏览示例:

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3
Mozilla/5.0 (iPhone; U; XXXXX like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A477d Safari/419.3

Android用户浏览示例:

Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 1.6; en-gb; Dell Streak Build/Donut AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/ 525.20.1
Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17
Mozilla/5.0 (Linux; U; Android 2.2; en-us; DROID2 GLOBAL Build/S273) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; E10i Build/2.0.2.A.0.24) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17

2. 移除浏览器地址栏

window.scrollTo(0, 1);

3. 防止网页触摸滚动

notouchmove = function(event) {
	event.preventDefault();
}
<div data-role="page" id="home" ontouchmove="notouchmove(event);">
...
</div>

4. 当横向浏览时显示信息

var updateorientation = function (){
	var classname = \'\',
	top = 100;
	switch(window.orientation){
		case 0:
		classname += "normal";
		break;

		case -90:
		classname += "landscape";
		break;

		case 90:
		classname += "landscape";
		break;

	}

	if (classname == \'landscape\') {
		if ($(\'#overlay\').length === 0) {
			window.scrollTo(0, 1);
			$(\'body\').append(\'<div id="overlay" style="width: 100%; height:\' + $(document).height() + \'px"><span style="top: \' + top + \'px">Landscape view is not supported for this page.</span></div>\');
		}
	} else {
		$(\'#overlay\').remove();
	}
};
Usage:

var supportsOrientationChange = "onorientationchange" in window,
orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";

window.addEventListener(orientationEvent, function() {
	updateorientation();
}, false);

5.显示部分描述信息,当点击时显示完整信息

var truncatedesc = function(trunc, len) {
	if (trunc) {
	  var org = trunc;

	  if (trunc.length > len) {
		trunc = trunc.substring(0, len);
		trunc = trunc.replace(/w+$/, \'\');

		trunc = \'<span class="truncated">\' + trunc;
		trunc += \'<strong class="more-description">...</strong></span>\';
		trunc += \'<span class="original" style="display: none;">\' + org + \'</span>\';
	  }

	  $(\'.truncated\').live("touchstart touchend", function() {
		$(this).closest(\'div\').find(\'.original\').show();
		$(this).closest(\'div\').find(\'.truncated\').hide();
		return false;
	  });

	  return trunc;
	}
};
Usage:

truncatedesc(item.description, 100);

6. 收到成功的Ajax请求时,重定向到另一个页面(jQuery mobile)

var ajaxurl = ‘http://…’; // Your web service URL

$.ajax({
	url: ajaxurl,
	type: \'GET\',
	processData: false,
	contentType: "application/json",
	dataType: "jsonp",
	success: function(data) {
		$.mobile.changePage("results.html");
	},
	error: function() {
		alert(\'Error!\');
	}
});

7. 从列表视图的链接中删除活动状态(jQuery mobile)

$(\'div\').live(\'pageshow\', function (event, ui) {
	$(\'[data-role=listview] li\').removeClass("ui-btn-active");
});

8. 从下拉选择中禁用默认的jQuery mobile样式(jQuery mobile)

$(document).bind("mobileinit", function(){
  	$.mobile.page.prototype.options.keepNative = "select";
});

9. 动态更新列表视图(jQuery mobile)

var output  = \'<li><img src="\' + item.image + \'" alt="\' + item.title + \'" />\';
output += \'<h3><a href="\' + item.url + \'">\' + item.title + \'</a></h3>\';
output += \'</li>\';	

$(\'#mylistul\').append(output).listview(\'refresh\');

10. 动态添加表单输入和应用默认样式(jQuery mobile)

var html = \'<input type="search" name="suburb" id="suburb" placeholder="Enter suburb" />\';
$(\'#searchform\').append(html);
$(\'#suburb\').textinput();

发表于
2011-11-26 11:11 
满天窜 
阅读(237
评论(0
编辑 
收藏 
举报

 

版权声明:本文为stu-xu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/stu-xu/archive/2011/11/26/2264129.html

移动开发10段常用的JavaScript代码 - 满天窜的更多相关文章

  1. POI Excel 11 工作簿放大,设置页眉页脚 – java线程例子

    POI Excel 11 工作簿放大,设置页眉页脚 2012-07-20 12:51  java线程例子  阅 […]...

  2. Linux ssh登录和软件安装详解 – HackerVirus

    Linux ssh登录和软件安装详解 阿哲Style   Linux第一天 ssh登录和软件安装详解  Lin […]...

  3. OpenCV3入门(六)图像滤波 – 啊哈彭

    OpenCV3入门(六)图像滤波 OpenCV3入门(六)图像滤波 1、图像滤波理论 1.1图像滤波理论 图像 […]...

  4. 用Java实现FTP批量大文件上传下载 – BradyChen

    内容摘要:本文介绍了在Java中,如何使用Java现有的可用的库来编写FTP客户端代码,并开发成Applet控 […]...

  5. Python实现网络多人聊天室 – Linux

    Python实现网络多人聊天室 – Linux 相关连接:   Python实现网络多人聊天室 & […]...

  6. oracle定时器在项目中的应用 – 人生24k

    oracle定时器在项目中的应用 oracle定时器在项目中的应用 业务需求: 现在业务人员提出了一个需求: […]...

  7. 关于算法学习的总结和感悟(原创) – java线程例子

    关于算法学习的总结和感悟(原创) 2012-03-26 22:43  java线程例子  阅读(420)  评 […]...

  8. C 标准库IO缓冲区和内核缓冲区的区别 – AlanTu

    C 标准库IO缓冲区和内核缓冲区的区别 1.C标准库的I/O缓冲区          UNIX的传统 是Eve […]...

随机推荐

  1. 使用科大讯飞语音转文字的服务进行电话录音分析

    需求 合规性质检:1)设定规则,实现自动质检,自动打分;2)如VOC文本质检可与语音文字进行匹配,自动判断是否 […]...

  2. 【二】、如何进行网络通信

    【二】、如何进行网络通信 前言 我觉得学习网络编程就是了解计算机之间如何进行网络通信,从上一篇《学网络编程前的 […]...

  3. Delphi – 创建SuperDll 持续更新

    Delphi SuperDll 作为一名5年的Delpher,一直认为Delphi是桌面应用的王者,我相信其他 […]...

  4. 中国象棋棋谱口诀

    中国象棋棋谱口诀 象棋谱式 将军不离九宫内,士止相随不出官。象飞四方营四角,马行一步一尖冲。 炮须隔子打一子, […]...

  5. Docker深入浅出系列 | 容器数据持久化

    Docker深入浅出系列 | 容器数据持久化 Docker已经上市很多年,不是什么新鲜事物了,很多企业或者开发 […]...

  6. 《VR入门系列教程》之18—Oculus代码剖析

    代码剖析 原文作者:Tony Parisi     那么,Unity究竟是如何支持Oculus VR运行的?首 […]...

  7. Linux下配置系统ipv6环境

    一:检查Linux是否已经开启ipv6 1.使用 ifconfig 查看自己的IP地址是否含有IPv6地址。 […]...

  8. 多线程_基本操作

    什么是多线程:   进程:正在运行的程序,QQ 360 ……        线程:就是 […]...

展开目录

目录导航