移动开发的小总结

- 1 min

最近换了新公司,全部转移到了移动终端的开发上,所幸现在各个浏览器厂商进步的很快,更所幸不需要兼容UC等一众国内浏览器,所以各种CSS3新特性用的十分爽快。 由于之前没有大段时间去做移动端开发,现在全部投入进来之后,发现自己已经落后很多了,各种新特性完全不知道,现在将工作中遇到的一些值得记录的地方,记录在这里,随时更新。

###CSS部分

在这个情况下,a模块的宽度是500px。而不是100 + 500 + 100,这是因为设置了box-sizing:border-box的原因。这个属性还有其他几种值可选,分别是:content-box,inherit,initial,padding-box

###js部分

本来是想在给input框外的元素添加tap事件的回调时,给input添加focus事件,但是发现在很多手机上并不好使。 在这里解释一下tap这个事件,tapzepto封装的一个事件,实现原理就是在touchstarttouchend都触发在同一元素上时,touchend结束时异步触发一个tap事件。按照道理来说,在这个时候触发一个input的focus事件,应该是可以使input获得焦点的,但是在iPhone上试了很多次,就是无法获得焦点,不知道是什么原因。后来的解决方案是在touchstart时,使input触发focus事件,结果就好了。具体的原理,还有待分析,但是在iphone和android下事件的触发时间是不一致的,在我的iphone中,点击一个input框外的元素,事件触发的顺序是:

	// 代码
	$('#input').bind('blur',
    	function () {
    		console.log('blur');
    });
    
    $('#input').bind('focus',
    	function () {
    		console.log('focus');
    });
    
    $('#other').bind('touchstart',
    	function () {
    		console.log('touchstart');
    });
    
    $('#other').bind('touchend',
    	function () {
    		console.log('touchend');
    });
    
    $('#other').bind('tap',
    	function () {
    		console.log('tap');
    });

    // iphone的结果是
    // touchstart -> touchend -> tap ->blur

    // 浏览器中模拟android的结果是
    // touchstart -> blur -> touchend -> tap

可以看出来,触发的顺序是不一致的。模拟的android中的顺序,更接近我们所理解的顺序,iphone中疑似对事件做了优化,所以导致blur在tap之后才被触发。 接下来,我又做了个测试,添加了一行代码:

    $('#input').bind('blur',
    	function () {
    		console.log('blur');
    });
    $('#input').bind('focus',
    	function () {
    		console.log('focus');
    });
    $('#other').bind('touchstart',
    	function () {
    		console.log('touchstart');
    });
    $('#other').bind('touchend',
    	function () {
    		console.log('touchend');
    });
    $('#other').bind('tap',
    	function () {
    		console.log('tap');
    		$('#input').focus();
    });


    // iphone的结果是:
    // touchstart -> touchend -> tap -> blur

    // 浏览器中模拟android的结果是
    // touchstart -> blur -> touchend -> tap -> focus

可以看出来,iphone中本应该被触发的focus事件没有触发,可以被认定为已经被优化掉了,至此大概可以理解,为什么在tap中触发input的focus事件没有生效了。

rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora