Home  >  Article  >  Web Front-end  >  Sample code sharing for practicing HTML5 CSS3Media Queries

Sample code sharing for practicing HTML5 CSS3Media Queries

黄舟
黄舟Original
2017-03-28 15:21:151727browse

Let’s introduce media first. To be precise, it should be CSS media queries (CSS Mediaquery), a media query contains a media type and at least one # that limits the scope of the style sheet using media attributes such as width, height, and color. ##Expression. The media query added by CSS3 allows the style to be applied to certain device ranges without modifying the content. How to define media, look at the code below, you can definitely guess it.





Regarding the explanation, the document says that when the media query is true, it is relevant. Style sheets or style rules will be applied according to the normal cascading rules. Even if the media query returns false, the style sheet with the media query on the 2cdf5bf648cf2f33323966d7f58a7f3f tag will still be downloaded (just not applied).

#So, this is also a drawback. If multiple style standards are defined for a certain page to respond to different media attributes, the loading time of the page will be affected. , but having said that, in the current era of rapid network development, network speed is also constantly improving and improving, so the impact is not big and can almost be ignored.

media You can use logical operators(and, not, only, etc.) to form media expressions and write more complex filtering conditions. I will not explain these expressions one by one here,

Next we will use several demos to demonstrate the usage and performance of media

##Since our purpose today is to explore how to monitor changes in the devicePixelRatio attribute. , then we can change the background style of a certain p under different devicePixelRatio values. The specific code is as follows:



    
        
        
        
    
    
        

The code is there, So how to test? Under normal circumstances, the devicePixelRatio attribute will not change, but there will definitely be special circumstances. For example, your computer is connected to two monitors, and the devicePixelRatio attributes of the two browsers are different. Same, then congratulations, you already have the testing conditions, just drag the page from one screen to another, so that you can see the effect and test it. Students will find that the background color of p is not as set in the code. It shows different colors under different devicePixelRatio attribute values. Why is this?

This code is the first one I wrote. After running it, I found that it had no effect. I didn’t know the reason at first. When dragging the page across the screen, in the browser console, I Found the reason. So what is the reason why the settings are invalid? Let's take a look at the screenshots of the Style content under the two screens. The left side is min-resolution equal to 1, and the right side is equal to 2

##      

Comparing the two pictures, we can find that when min-resolution is equal to 2, the properties defined in it are overwritten and do not take effect. Why is this?

To explain, I am afraid I need to add some knowledge here, that is, the specific effects of the prefixes of min- and max- in the code are described in the document. : Most media properties are prefixed with "min-" and "max-", which are used to express "greater than or equal to" and "less than or equal to". This avoids the use of "d21bf6265d53cdd4dcff18f6785f8fb4" characters that conflict with HTML and XML. If you do not specify a value for the media property, and the actual value of the property is non-zero, the expression resolves to true. Expressions containing this attribute value generally return false if the browser is running on a device that does not have this attribute value.

其实上面的说明已经帮我解释清楚了,我再通俗地和大家解释一下:当 devicePixelRatio 为 1 时,只有 min-resolution: 1dppx 这个条件满足,因此 p 的颜色是黑色没错;当 devicePixelRatio 为 2 时,两个 media 都满足条件,同时 CSS 的规则是后加载的样式将会覆盖先加载的样式,由于我么将 min-resolution: 1dppx 的 media 写在后面,因此如果两个 media 都满足条件的话, min-resolution: 1dppx 的 media 将会覆盖 min-resolution: 2dppx 的 media,因此不管你把页面拖到那个屏幕,那个 p 的背景色都是黑色。

那么我们将两个 media 调换一下位置,问题就顺利地解决了。

以上是根据不同的 media 条件设置不同的样式,这是 CSS 的做法,在 JavaScript 中,没有专门的方法来监听 window.devicePixelRatio 属性变化,那么该怎么监听 devicePixelRatio 属性的变化呢?方法也很简单,看看下面的代码,你一定就明白了:

window.matchMedia('screen and (min-resolution: 2dppx)').addListener(function(e) {
    console.info(e, window.devicePixelRatio);
});

稍微解释下,通过 window.matchMedia(‘media expression’) 方法获取到对应的 media,然后通过 addListener(function(e) {}) 来监听 media 的变化。

有玩过 Canvas 的朋友一定知道,要想绘制出来的内容效果最佳的话,Canvas 自身的 width 和 height 属性值与 style 中的 width 和 height 的比例应该恰好等于 devicePixelRatio 的值,所有如果你在切换不同 devicePixelRatio 属性值的屏幕时,没有重新设置 Canvas 的宽高的话,绘制出来的画面将不是最佳的效果。

接下来我们基于 HT for Web 的 3D 模型来做一个小实验。实验的内容是这样的,在 GraphView 中有一辆车根据某条路线前行,当拖到另外一个屏幕的时候,换辆车子。先来看看效果图:

上面两张图分别是在不同的屏幕中的截图,车子动起来的效果可以访问以下链接:



    
        
        HT for Web
        
        
        
        
        
    
    
    

来介绍下这次 Demo 中都用到的了 HT for Web 的那些技术。

首先是车子,车子并不是通过 HT for Web 生成的,而是通过专业的 3D 工具设计,然后导出 obj 和 mtl 文件,HT for Web 对 obj 和 mtl 文件进行解析,然后显示在 Graph3dView 中,

在 obj 文档中,你会看到一个一个飞机的例子,飞机沿着设定好的路线飞行,你应该会想,这个寻路是怎么实现的呢?其实很简单,我们将路线切割成一个个很小很小的单元,然后根据算法依次获取到小单元的坐标设置到移动的物体上,这样物体就动起来了。 

在 Demo 中,有一条很精致的马路,这条马路就是一个 Shape 节点,根据车的路径生成的马路,Shape 是一个六面体,因为首尾相连了,所以没有左右面,在这个例子中,我将马路的 back 和 top  面隐藏了,然后 bottom 面支持翻转,让 bottom 面的贴图显示在内表面上,这样马路就建成了。

The above is the detailed content of Sample code sharing for practicing HTML5 CSS3Media Queries. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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 [email protected]