Code Monkey home page Code Monkey logo

blog's Introduction

blog's People

Watchers

James Cloos avatar zhongjian.zhou avatar

blog's Issues

JavaScript异步编程

为了清晰和可维护性,努力组织异步代码。

  1. 深入理解JavaScript事件
    JavaScript事件处理器在线程空闲之前不会运行。而是加入到事件队列中,待线程空闲之后再选取执行。
    setTimeout / setInterval是不精确的计时函数,延迟比较粗糙>4ms。
    node中可以使用process.nextTick
    现代浏览器方法:requestAnimationFrame
    异步函数:调用一个函数时,程序只在该函数返回之后才能继续。
    异步递归需谨慎使用,应该使用回调。
    嵌套回调诱惑我们通过添加更多代码来添加更多特性,而不是将这些特性实现为可管理、可重用的代码片段。
  2. 分布式事件
    PubSub(Publish/Subscribe)发布/订阅模式
    EventEmitter(事件发生器)对象,nodejs
  3. Promise对象和Deferred对象
    jQuery 1.5+,所有ajax函数都会返回Promise对象。——封装性
    ❤『温馨提示50字』的Promise重构。
    http://api.jquery.com/category/deferred-object/
    $.Deferred
    deferred.resolve(), deferred.reject()
    deferred.promise()
    deferred.pipe()
    $.when()
    deferred.done().fail().always()
    deferred.then(doneCallbacks, failCallbacks)
  4. Async.js的工作流控制
  5. worker对象的多线程技术
  6. 异步的脚本加载

viewport meta tag

打开m.dianping.com,查看源码可以找到如下代码:
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
容易看出,这个meta属性是用来控制屏幕显示和缩放的,但是各属性有什么意义,以及在不同分辨率下的使用又是怎样呢?

1. css pixels & device pixels的区别 css pixels: 浏览器使用的抽象单位, 主要用来在网页上绘制内容。(window.innerWidth/innerHeight) device pixels: 显示屏幕的的最小物理单位,每个dp包含自己的颜色、亮度。(screen.width/height) 举例来说,当设备像素和css像素相同时,缩放屏幕,此时一个css像素在屏幕上占的空间会变小(下文中提到的scale)
 
2. 什么是viewport
手机浏览器是把页面放在一个虚拟的“窗口”(viewport)中,通常这个虚拟的“窗口”(viewport)比屏幕宽,这样就不用把每个网页挤到很小的窗口中(这样会破坏没有针对手机浏览器优化的网页的布局),用户可以通过平移和缩放来看网页的不同部分。移动版的 Safari 浏览器最新引进了 viewport 这个 meta tag,让网页开发者来控制 viewport 的大小和缩放,其他手机浏览器也基本支持。如图所示:
 
3. viewport的参数 width(viewport宽度)=device-width(设备宽度) height(viewport高度)=device-height(设备高度) minimum-scale(最小缩放比例)=1.0 initial-scale(初始缩放比例)=1.0 maximum-scale(最大放大比例)=1.0 user-scalable(是否允许用户缩放)=0/false(不可手动缩放)/1/true(可以手动缩放)
 
4. 实际验证
测试方法:在电脑上起一个静态服务,然后用手机浏览器访问。
测试代码:
<!DOCTYPE html> <html> <head> <title>viewport</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=640, initial-scale=0.6, user-scalable=0"> <script> window.onload = function(){ alert(screen.width); alert(window.innerWidth); } </script> </head> <body style="margin:0;"> <div style="width:200px;height:100px;background-color:red;"></div> <div style="width:50%;height:100px;background-color:green;"></div> <div style="width:100%;height:100px;background-color:yellow;"></div> </body> </html>
initial-scale和user-scalable都受maximum-scale和minimum-scale的约束。 如果设置的width小于device-width,那么会采用device-width。 如果设置的width*scale大于device-width,那么会出现横向滚动条。
测试设备的portrait device-width:(更多设备可参考文档5) iphone 4s safari 320(640 for retina) galaxy nexus chrome 360 meizu mx2 chrome (16:10) 400
设备像素=css像素*scale。
对于主流的320和640手机,有两种兼容方案。meta上面设置initial-scale为0.5,然后开发就以640像素去做设计。另外一种就是initial-scale为1.0,实际图片适应的时候就设置background-size强制大图缩小,而像素计算就以320的展示像素开发。目前的m站采用的就是这种方式。
此外,由于width=scale-width会导致iphone5高度不能占满全屏,所以比较合理的viewport meta标签设置是initial-scale=1.0, maximum-scale=1.0, minimum-scale=0。

nodejs

nodejs
NPM
node模块
node事件
node异步

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.