Table of Contents
回复内容:
Home Web Front-end H5 Tutorial 淘宝网触屏版为什么rem跟px混用?有什么好处?为什么不都用rem?

淘宝网触屏版为什么rem跟px混用?有什么好处?为什么不都用rem?

Jun 07, 2016 am 08:42 AM

回复内容:

自荐两篇文章:

移动端适配方案(上) · Issue #17 · riskers/blog · GitHub
移动端适配方案(下) · Issue #18 · riskers/blog · GitHub

还有一个分享时的PPT:

移动端页面适配方案

希望能够对你帮助 就如 刘宁(知乎好多刘宁,手机艾特一下翻了好几页没找到) 所说,不是所有元素都需要缩放的。

最容易举例的就是1px的border,无论什么屏,无论像素密度多少,都要准确的显示1物理像素(物理像素与逻辑像素与像素密度请咨询谷大哥)。

淘宝目前的做法即是,通过脚本检测设备的像素密度,再设置文档的根font-size与viewport的缩放值,从而让px能够准确的显示为物理像素。

讲的比较乱,回头再更

@mablevi自由就好
我们的做法是:获取设备像素密度后,设置根节点的fontSize,然后文档中全部使用rem+px结合。
window.onload = function () {
    var nameAttr = document.createAttribute('name');
    nameAttr.value = 'viewport';
    var contentAttr = document.createAttribute('content');
    var dpr = window.devicePixelRatio || 1;
    contentAttr.value = 'width=device-width,initial-scale=' + (1 / dpr) + ',target-densitydpi=device-dpi,user-scalable=no';
    var element = document.createElement('meta');
    element.attributes.setNamedItem(nameAttr);
    element.attributes.setNamedItem(contentAttr);
    document.head.appendChild(element);
    document.getElementsByTagName('html')[0].style.fontSize = (16 * dpr) + 'px';
};
Copy after login
在页面交互,视觉设计上,不是所有元素的大小都是需要跟随设备尺寸不同而变换的。

rem单位会出现一些边界情况。比如,雪碧图背景的元素使用rem就会掉入一两个像素的偏差的陷阱。

其次不明白手淘为啥使用脚本去捕获当前设备的尺寸大小来设置根的font-size。我比较推荐使用mediaquery断点形式去设置font-size,因为那样更自然。 可能是开发过程中,开始使用rem来尝试做适应吧,基于以前的一些代码,慢慢替换. 用MX4的同学访问手淘触屏版有异常么?
前天测试的时候 alert出screen.width 的数值是物理分辨率的数值。
没百度到这个问题,就我一人遇到么。。。。 移动端适配等式:
设备宽度/设计稿宽度 = 某个元素某一距离实际值/该元素在设计稿的值
例子:
一个5s宽度320/设计稿宽度640 = 一个元素的高度未知/设计稿上它的高度为20


可以推出它的实际高度为10,通常我们使用rem作为等式前的值,但是因为太小无法作为html根字体大小使用,所以适当放大,通常为了方便计算,放大100倍,即为:
document.documentElement.fontSize = document.documentElement.clientWidth / 640 (或实际设计稿宽度) * 100
那么此时在设计稿上量的的某一距离,比如43px,就等于0.43rem。


至于文字,我们通常不用rem,因为由于不同的rem计算方式会产生很多奇怪的大小,使得文字出现糊掉或者模糊的情况,通常我们使用媒体查询事先设置好body的字体大小,这样来确保文字的正常显示。
即为:rem处理适配距离的问题,em处理文字大小问题。


详见:
chengfeilong.com/mobile 我是winter,匿名答。

rem用于随屏幕伸缩的尺寸。px用于正文文字大小、细边框等不宜随屏幕伸缩的尺寸。

考虑到字体的点阵信息,一般文字尺寸多会采用 16px 20px 24px等值,若以rem指定文字尺寸,会产生诸如21px,19px这样的值,会导致字形难看,毛刺,甚至黑块,故大部分文字应该以px设置。

一般标题类文字,可能也有要求随屏幕缩放,且考虑到这类文字一般都比较大,超过30px的话,也可以用rem设置字体。

那个百姓网的刘宁同学,Android有多少种尺寸你造么…… 其实是中间采用了两个库:lib-flexible.js和scroll.js,其中lib-flexible采用rem,而scroll.js模拟滚动采用的px,这个要问下作者了 @winter 关于这个问题,有一篇专门的文章在这里,自己看:div.io/topic/1092
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

What exactly does H5 page production mean? What exactly does H5 page production mean? Apr 06, 2025 am 07:18 AM

H5 page production refers to the creation of cross-platform compatible web pages using technologies such as HTML5, CSS3 and JavaScript. Its core lies in the browser's parsing code, rendering structure, style and interactive functions. Common technologies include animation effects, responsive design, and data interaction. To avoid errors, developers should be debugged; performance optimization and best practices include image format optimization, request reduction and code specifications, etc. to improve loading speed and code quality.

How to make h5 click icon How to make h5 click icon Apr 06, 2025 pm 12:15 PM

The steps to create an H5 click icon include: preparing a square source image in the image editing software. Add interactivity in the H5 editor and set the click event. Create a hotspot that covers the entire icon. Set the action of click events, such as jumping to the page or triggering animation. Export H5 documents as HTML, CSS, and JavaScript files. Deploy the exported files to a website or other platform.

What is the H5 programming language? What is the H5 programming language? Apr 03, 2025 am 12:16 AM

H5 is not a standalone programming language, but a collection of HTML5, CSS3 and JavaScript for building modern web applications. 1. HTML5 defines the web page structure and content, and provides new tags and APIs. 2. CSS3 controls style and layout, and introduces new features such as animation. 3. JavaScript implements dynamic interaction and enhances functions through DOM operations and asynchronous requests.

What application scenarios are suitable for H5 page production What application scenarios are suitable for H5 page production Apr 05, 2025 pm 11:36 PM

H5 (HTML5) is suitable for lightweight applications, such as marketing campaign pages, product display pages and corporate promotion micro-websites. Its advantages lie in cross-platformity and rich interactivity, but its limitations lie in complex interactions and animations, local resource access and offline capabilities.

What Does H5 Refer To? Exploring the Context What Does H5 Refer To? Exploring the Context Apr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

Is H5 page production a front-end development? Is H5 page production a front-end development? Apr 05, 2025 pm 11:42 PM

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the <canvas> tag to draw graphics or using JavaScript to control interaction behavior.

How to make pop-up windows with h5 How to make pop-up windows with h5 Apr 06, 2025 pm 12:12 PM

H5 pop-up window creation steps: 1. Determine the triggering method (click, time, exit, scroll); 2. Design content (title, text, action button); 3. Set style (size, color, font, background); 4. Implement code (HTML, CSS, JavaScript); 5. Test and deployment.

See all articles