[Phonegap+Sencha Touch] 移动开发68 Sencha Touch弹出键盘挡住输入框的解决办法

原文地址:http://blog.csdn.net/lovelyelfpop/article/details/49679331



现象:

sencha touch有一个缺点:当formPanel里的输入框控件较多的时候,软键盘弹出后会挡住正在输入的控件。如下图:

更清楚的画面请看视频http://video.weibo.com/show?fid=1034:4c31fd16a546276f392af6517462b856



解决办法:

在输入框聚焦和界面resize的时候,都自动将被聚焦的输入框滚动到可见位置。

Ext.define('UX.viewport.FieldScroller', {
    override: 'Ext.viewport.Default',

    onElementFocus: function() {
        this.callParent(arguments);

        this.scrollFocusedFieldIntoView();
    },

    scrollFocusedFieldIntoView: function() {
        var me = this,
            focusedDom = me.focusedElement,
            fieldEl = focusedDom && Ext.fly(focusedDom).up('.x-field'),
            fieldId = fieldEl && fieldEl.id,
            fieldCmp = fieldId && Ext.getCmp(fieldId),
            offsetTop = 0,
            scrollingContainer, scroller, scrollerEl, domCursor, thresholdY, containerHeight;

        if (!fieldCmp) {
            return;
        }

        scrollingContainer = fieldCmp.up('{getScrollable()}');

        if (scrollingContainer) {
            scroller = scrollingContainer.getScrollable().getScroller();
            scrollerEl = scroller.getElement();
            domCursor = focusedDom;

            while (domCursor && domCursor !== scrollerEl.dom) {
                offsetTop += domCursor.offsetTop;
                domCursor = domCursor.offsetParent;
            }

            containerHeight = scroller.getContainerSize().y;
            thresholdY = offsetTop + fieldEl.getHeight() + (me.config.fieldFocusPadding || 40);
            // console.log('offsetTop=%o, containerHeight=%o, thresholdY=%o', offsetTop, containerHeight, thresholdY);

            if (scroller.position.y + containerHeight < thresholdY) {
                // console.log('scrolling to ', thresholdY - containerHeight);
                scroller.scrollTo(0, thresholdY - containerHeight, true);
            }
        }
    }
}, function() {
    Ext.onSetup(function() {
        Ext.Viewport.on('resize', 'scrollFocusedFieldIntoView');
    });
});

(代码来自github:https://github.com/JarvusInnovations/jarvus-touch-fieldscroller


使用方法:

将上面的代码保存为文件"FieldScroller.js",位置和命名空间自己改,记得requires。其实就是第三方扩展类的引入

当然你可以可以按照github上面的,给sencha app添加这个packages,然后在app.json的requires节点加上package名字


下面是效果:

(更清楚的画面请看视频:http://video.weibo.com/show?fid=1034:edcf3db9e9cdada72ace71924c3fdfcb



ExtJs 6 Modern 请使用下面的代码:

//在输入框聚焦和界面resize的时候,都自动将被聚焦的输入框滚动到可见位置
Ext.define('UX.overrides.viewport.Default', {
    override: 'Ext.viewport.Default',

    onElementFocus: function() {
        this.callParent(arguments);

        this.scrollFocusedFieldIntoView();
    },

    scrollFocusedFieldIntoView: function() {
        var me = this,
            focusedDom = me.focusedElement,
            fieldEl = focusedDom && Ext.fly(focusedDom).up('.x-field'),
            fieldId = fieldEl && fieldEl.id,
            fieldCmp = fieldId && Ext.getCmp(fieldId),
            offsetTop = 0,
            scrollingContainer, scroller, scrollerEl, domCursor, thresholdY, containerHeight;
        
        //if (!fieldCmp || ((fieldCmp.isXType('richtextareafield') || fieldCmp.isXType('simpletextareafield')) && fieldCmp.element.hasCls('cmtbar-field'))) {
        if (!fieldCmp) {
            return;
        }

        scrollingContainer = fieldCmp.up('{getScrollable()}');

        if (scrollingContainer) {
            scroller = scrollingContainer.getScrollable();
            scrollerEl = scroller.getElement();
            domCursor = focusedDom;

            while (domCursor && domCursor !== scrollerEl.dom) {
                offsetTop += domCursor.offsetTop;
                domCursor = domCursor.offsetParent;
            }

            containerHeight = scroller.getElement().getHeight();
            thresholdY = offsetTop + Math.min(fieldEl.getHeight(), 40) + (me.config.fieldFocusPadding || 40);
            // console.log('offsetTop=%o, containerHeight=%o, thresholdY=%o', offsetTop, containerHeight, thresholdY);

            if (scroller.position.y + containerHeight < thresholdY) {
                // console.log('scrolling to ', thresholdY - containerHeight);
                scroller.scrollTo(0, thresholdY - containerHeight, true);
            }
        }
    },

    onAppLaunch: function(){
        this.callParent(arguments);
        Ext.Viewport.on('resize', 'scrollFocusedFieldIntoView');
    }
});






欢迎加入Sencha Touch + Phonegap交流群

1群:194182999 (满)

2群:419834979

共同学习交流(博主QQ:479858761

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Sencha Touch 是第一个专门设计为移动设备开发 web 应用的 JavaScript 框架,它基于 HTML5 和 CSS3 的 web 标准,全面兼容 Android 和 Apple iOS,通过 Sencha Touch 框架用户可以创建非常像移动设备本地应用的 web 应用,它提供了丰富的华丽的控件和强大的功能,包括对触控事件的增强,数据整合等。本文通过对 Sencha Touch 框架的介绍,使读者对该框架有一定的了解,并通过一些代码示例给用户更详细直观的对 Sencha Touch 框架功能的了解。 ExtJS ExtJS 是为 web 开发人员提供的基于 JavaScript 和 web 标准快速构建可以跨浏览器平台运行的强大的 web 应用程序开发框架。它提供了丰富的用户界面组件和完善的文档资源,并且还有一个最重要的优势就是组件的设计简洁而容易扩展。 Ext GWT Ext GWT 是使用 Java 构建富 web 应用的最快,最有效的框架。它提供了运行性能良好的用户界面控件,并且在界面布局管理和全键盘支持方面有更突出的优势。 等等 Ext Core Ext Core 是一个构建跨浏览器运行的动态 web 应用的 JavaScript 库,它提供了 DOM 查询和元素选择的跨浏览器访问 API,它是一个轻量级的,运行性能良好并且很容易使用的 JavaScript 库。 Ext Designer Ext Designer 是一个帮助用户更快的创建桌面应用程序的可视化的界面图形化工具。 Sencha Touch Sencha Touch 是第一个基于 HTML5 的 Mobile web 应用开发框架。 Sencha Animator Sencha Animator 是一个创建基于 WebKit 浏览器和触屏移动设备运行的 CSS3 动画的工具,用户可以通过它创建出令人惊叹的动画,效果一点也不逊色于 Adobe 的动画工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神秘_博士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值