自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

移動web開發(fā)常用JavaScript代碼

移動開發(fā)
在開發(fā)移動網(wǎng)站過程中,有一些Javascript代碼會經(jīng)常用到。以下是10段常用的JavaScript代碼。需要注意的是某幾段代碼需要jQuery mobile framework支持。

 1.如果網(wǎng)頁是在iPhone或Android瀏覽器中查看,則在主體元素中添加“iPhone”或“Android” 類名

Javascript代碼

  1. if (navigator.userAgent.match(/iPhone/i)) {   
  2.     $('body').addClass('iPhone');   
  3. else if (navigator.userAgent.match(/Android/i)) {   
  4.          $('body').addClass('Android');   
  5. }   

 

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. 移除瀏覽器地址欄

Javascript代碼

  1. window.scrollTo(0, 1);   

 

3. 防止網(wǎng)頁觸摸滾動

Javascript代碼

  1. notouchmove = function(event) {   
  2.     event.preventDefault();   
  3. }   
  4. <div data-role="page" id="home" ontouchmove="notouchmove(event);">   
  5. ...   
  6. </div>  

4. 當(dāng)橫向瀏覽時顯示信息

Javascript代碼

 

  1. var updateorientation = function (){   
  2.     var classname = '',   
  3.     top = 100;   
  4.     switch(window.orientation){   
  5.         case 0:   
  6.         classname += "normal";   
  7.         break;   
  8.    
  9.         case -90:   
  10.         classname += "landscape";   
  11.         break;   
  12.    
  13.         case 90:   
  14.         classname += "landscape";   
  15.         break;   
  16.    
  17.     }   
  18.    
  19.     if (classname == 'landscape') {   
  20.         if ($('#overlay').length === 0) {   
  21.             window.scrollTo(0, 1);   
  22.             $('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>');   
  23.         }   
  24.     } else {   
  25.         $('#overlay').remove();   
  26.     }   
  27. };   
  28. Usage:   
  29.    
  30. var supportsOrientationChange = "onorientationchange" in window,   
  31. orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";   
  32.    
  33. window.addEventListener(orientationEvent, function() {   
  34.     updateorientation();   
  35. }, false);   

 

5.顯示部分描述信息,當(dāng)點擊時顯示完整信息

Javascript代碼

  1. var truncatedesc = function(trunc, len) {   
  2.     if (trunc) {   
  3.       var org = trunc;   
  4.    
  5.       if (trunc.length > len) {   
  6.         trunc = trunc.substring(0, len);   
  7.         trunc = trunc.replace(/w+$/, '');   
  8.    
  9.         trunc = '<span class="truncated">' + trunc;   
  10.         trunc += '<strong class="more-description">...</strong></span>';   
  11.         trunc += '<span class="original" style="display: none;">' + org + '</span>';   
  12.       }   
  13.    
  14.       $('.truncated').live("touchstart touchend"function() {   
  15.         $(this).closest('div').find('.original').show();   
  16.         $(this).closest('div').find('.truncated').hide();   
  17.         return false;   
  18.       });   
  19.    
  20.       return trunc;   
  21.     }   
  22. };   
  23. Usage:   
  24.    
  25. truncatedesc(item.description, 100);   

 

6. 收到成功的Ajax請求時,重定向到另一個頁面(jQuery mobile)

Javascript代碼

  1. var ajaxurl = ‘http://…’; // Your web service URL   
  2.    
  3. $.ajax({   
  4.     url: ajaxurl,   
  5.     type: 'GET',   
  6.     processData: false,   
  7.     contentType: "application/json",   
  8.     dataType: "jsonp",   
  9.     success: function(data) {   
  10.         $.mobile.changePage("results.html");   
  11.     },   
  12.     error: function() {   
  13.         alert('Error!');   
  14.     }   
  15. });   

 

7. 從列表視圖的鏈接中刪除活動狀態(tài)(jQuery Mobile)

Javascript代碼

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

 

8. 從下拉選擇中禁用默認的jQuery Mobile樣式(jQuery Mobile)

Javascript代碼

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

 

9. 動態(tài)更新列表視圖(jQuery mobile)

Javascript代碼

  1. var output  = '<li><img src="' + item.image + '" alt="' + item.title + '" />';   
  2. output += '<h3><a href="' + item.url + '">' + item.title + '</a></h3>';   
  3. output += '</li>';       
  4.    
  5. $('#mylistul').append(output).listview('refresh');  

 

10. 動態(tài)添加表單輸入和應(yīng)用默認樣式(jQuery Mobile)

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

 

推薦書籍

下面兩本關(guān)于HTML5和jQuery的書將有助于你理解和使用jQuery Mobile框架。

[[60679]]

HTML5: Up and Running

[[60680]]

jQuery: Novice to Ninja

責(zé)任編輯:佚名 來源: 雨中無傘博客
相關(guān)推薦

2011-10-08 13:54:27

JavaScript

2014-03-28 15:36:43

移動WebJavaScript開發(fā)框架

2011-08-22 10:11:03

JavaScript

2016-09-29 09:22:01

移動WebHtml5

2011-07-07 13:19:38

Web

2013-09-04 14:49:10

移動Web前端開發(fā)設(shè)計理念

2015-07-16 13:57:06

移動web開發(fā)規(guī)范

2011-06-30 09:46:59

jQuery MobiRails

2014-07-18 09:59:17

移動webJavascript移動Web

2013-09-10 16:16:19

移動網(wǎng)站性能優(yōu)化移動web

2011-10-28 11:20:25

移動Web

2016-04-25 10:07:18

jQuery代碼Web開發(fā)效率

2011-03-01 09:23:47

移動Web應(yīng)用開發(fā)成本

2011-08-22 14:31:53

iPhone開發(fā)

2011-02-22 10:23:43

2011-05-25 09:34:30

HTML5cssjavascript

2011-12-29 10:48:49

移動Web

2011-05-11 09:47:14

mobl移動web開發(fā)

2019-07-08 15:10:17

JS工具函數(shù)

2024-03-11 10:19:30

Plasmo瀏覽器Web
點贊
收藏

51CTO技術(shù)棧公眾號