设为首页 加入收藏

TOP

移动网站开发中常用的10段JavaScript代码
2014-11-24 12:21:27 来源: 作者: 【 】 浏览:0
Tags:移动 网站开发 常用 JavaScript 代码



2、移除浏览器地址栏

window.scrollTo(0, 1);


3、防止网页触摸滚动

notouchmove = function(event) {
event.preventDefault();
}


...



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('
Landscape view is not supported for this page.
');
}
} 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 = '' + trunc;
trunc += '...
';
trunc += '';
}

$('.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 = '
  • ' + item.title + '';
    output += '

    ' + item.title + '

    ';
    output += '
  • ';

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


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

    var html = '';
    $('#searchform').append(html);
    $('#suburb').textinput();


    】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
    分享到: 
    上一篇Android audioManager解决MediaPl.. 下一篇ARM的伪指令总结

    评论

    帐  号: 密码: (新用户注册)
    验 证 码:
    表  情:
    内  容:

    ·用 Python 进行数据 (2025-12-25 15:49:09)
    ·如何学习Python数据 (2025-12-25 15:49:07)
    ·利用Python进行数据 (2025-12-25 15:49:04)
    ·Java 学习线路图是怎 (2025-12-25 15:19:15)
    ·关于 Java 学习,有 (2025-12-25 15:19:12)