使用link标签引入的CSS文件,或者使用@import引入的CSS文件会阻止浏览器对网页的渲染,浏览器在这些样式文件下载完毕前不会渲染任何东西从而延迟网页渲染,我们称之为阻碍渲染CSS(Render blocking stylesheets)。但有些场景下,我们并不需要浏览器,比如说当我们首次渲染并不需要某个CSS文件时,或者CSS文件实在太大,我们更关心如何能更快地展现网页而不是更早渲染出漂亮的页面时,我们就需要采取一些策略来阻止浏览器帮我们的倒忙。

阻碍渲染CSS

  • 如背景所述,阻碍渲染CSS会延迟浏览器渲染网页,只有所有的CSS文件都加载完毕后,浏览器才会开始渲染网页
  • CSS文件越大,下载所需时间会越长,延迟网页渲染的时间也越长
  • CSS文件越多,下载所需时间会越长,延迟网页渲染的时间也越长
  • 延迟网页渲染的时间越长,网页加载的时间也会越长(实际上和感觉上都是这样)

##

GitHub Project: loadCSS loadCSS

A function for loading CSS asynchronously [c]2017 @scottjehl, @zachleat Filament Group, Inc. Licensed MIT

Why loadCSS?

Referencing CSS stylesheets with link[rel=stylesheet] or @import causes browsers to delay page rendering while a stylesheet loads. When loading stylesheets that are not critical to the initial rendering of a page, this blocking behavior is undesirable. The new standard enables us to load stylesheets asynchronously, without blocking rendering, and loadCSS provides a JavaScript polyfill for that feature to allow it to work across browsers, as well as providing its own JavaScript method for loading stylesheets.

Latest release: https://github.com/filamentgroup/loadCSS/releases NPM: https://www.npmjs.com/package/fg-loadcss Recommended loadCSS Usage

LoadCSS is designed for loading CSS that is not critical to the initial rendering of the page, and desirable to load in an asynchronous manner. (For the critical CSS rules, we recommend either inlining that CSS in a style element, or referencing it externally and server-pushing it using http/2. Read more here)

The standard markup pattern for loading files asynchronously is: (W3C Spec). We recommend using this markup pattern to reference your non-critical CSS files. loadCSS and its rel=preload polyfill are designed to enable this markup to work in browsers that don’t yet support this feature.

For each CSS file you’d like to load asynchronously, use a link element like this:

In browsers that support it, the rel=preload attribute will cause the browser to fetch the stylesheet, but it will not apply the CSS once it is loaded (it merely fetches it). To address this, we recommend using an onload attribute on the link that will do that for us as soon as the CSS finishes loading.

This step requires JavaScript to be enabled, so we recommend including an ordinary reference to your stylesheet inside a noscript element as a fallback.

After linking to your asynchronous stylesheet(s) this way, include the loadCSS script, as well as the loadCSS rel=preload polyfill script in your page. These can be inlined or linked and http/2-pushed if possible. Here’s how they would look inlined in the page:

These scripts will automatically detect if a browser supports rel=preload. In browsers that natively support rel=preload, these scripts will do nothing, allowing the browser to load and apply the asynchronous CSS (note the onload attribute above, which is there to set the link’s rel attribute to stylesheet once it finishes loading in browsers that support rel=preload). In browsers that do not support rel=preload, they will find CSS files referenced this way in the DOM and load and apply them asynchronously using the loadCSS function.

Note: regardless of whether the browser supports rel=preload or not, a CSS file will be referenced from the same location in the source order as your original link element. Keep this in mind, as you may want to place the link in a particular location in your head element so that the CSS loads with an expected cascade order. Also, any media attribute value on the original link element will be retained when the polyfill is in play.

You can view a demo of this rel=preload pattern here: https://master-origin-loadcss.fgview.com/test/preload.html

Manual CSS loading with loadCSS

The loadCSS.js file exposes a global loadCSS function that you can call to load CSS files programmatically, when needed.

loadCSS( “path/to/mystylesheet.css” ); The code above will insert a new CSS stylesheet link after the last stylesheet or script that it finds in the page, and the function will return a reference to that link element, should you want to reference it later in your script. Multiple calls to loadCSS will reference CSS files in the order they are called, but keep in mind that they may finish loading in a different order than they were called.

Function API

If you’re calling loadCSS manually (without the rel=preload pattern, the function has 3 optional arguments.

before: By default, loadCSS attempts to inject the stylesheet link after all CSS and JS in the page. However, if you desire a more specific location in your document, such as before a particular stylesheet link, you can use the before argument to specify a particular element to use as an insertion point. Your stylesheet will be inserted before the element you specify. For example, here’s how that can be done by simply applying an id attribute to your script. … … ``` media: You can optionally pass a string to the media argument to set the media=”” of the stylesheet - the default value is all. Using with onload

Onload event support for link elements is spotty in some browsers, so if you need to add an onload callback, include onloadCSS function on your page and use the onloadCSS function:

var stylesheet = loadCSS( “path/to/mystylesheet.css” ); onloadCSS( stylesheet, function() { console.log( “Stylesheet has loaded.” ); }); Browser Support

loadCSS attempts to load a css file asynchronously in any JavaScript-capable browser. However, some older browsers such as Internet Explorer 8 and older will block rendering while the stylesheet is loading. This merely means that the stylesheet will load as if you referenced it with an ordinary link element.

Contributions and bug fixes

Both are very much appreciated - especially bug fixes. As for contributions, the goals of this project are to keep things very simple and utilitarian, so if we don’t accept a feature addition, it’s not necessarily because it’s a bad idea. It just may not meet the goals of the project. Thanks!

在前端的日常编码中,我们经常与图片打交道,使用体积尽量小、质量尽量高的图片一直是我们的追求,为此我们绞尽脑汁,选取合适的图片格式,进行最大的压缩,使用代码进行各种各样的优化。本文所述的方法是采用一张尺寸小的图片来模拟一张尺寸较大的图,从而达到减小所需图片资源体积的目的。

使用小尺寸图片模拟大尺寸图片

前置条件

  • 推荐使用compass编译,如果想要使用原生sass编译器编译,需要对代码做少许修改

特点

  • 在宽度方向上模拟图片的行为,可以随着图片容器的宽度变化像目标大尺寸图片一样调整图片的缩放和相对位置的缩放

  • 可以当做带背景的容器使用,便于添加内容文字

  • 兼容双倍高清图

  • 可以指定原图的缩放倍数

demo

目标尺寸图

代码

// image-set mixin
@mixin image-set($image1x,$image2x) {
  background-image: image-url($image1x);
  background-image: image-set($image1x 1x,$image2x 2x);
  background-image: -webkit-image-set($image1x 1x,$image2x 2x);
  background-image: -moz-image-set($image1x 1x,$image2x 2x);
  background-image: -o-image-set($image1x 1x,$image2x 2x);
  background-image: -ms-image-set($image1x 1x,$image2x 2x);
}

// 使用一个小图来模拟大图
@mixin fake-image(
  $image-path,    //普通图的大小
  $image2x-path,  //双倍图的大小(如果没有,可以设置为与普通图相同路径)
  $target-width,  //模拟目标图的宽度
  $target-height, //模拟目标图的高度
  $left,          //普通图在模拟目标图中的x坐标,单位为px
  $top,           //普通图在模拟目标图中的y坐标,单位为px
  $scale: 1,      //普通图的缩放倍数
  $not-responsive: false //设置响应式,如果非响应式图片,那么图片将有最大限制,即模拟目标图的宽度
) {

  $image-width: image-width($image-path) * $scale;
  $image-height: image-height($image-path) * $scale;
  
  position: relative;
  height: 0;
  width: 100%;
  padding-bottom: percentage($target-height / $target-width);

  @if($not-responsive) {
    max-width: $target-width;
  }

  &:after {
    content: '';
    display: block;
    position: absolute;
    top: 0;
    left: percentage($left / $target-width);
    z-index: 0;
    padding-top: percentage($top / $target-width);
    width: percentage($image-width / $target-width);
    height: 0;
    padding-bottom: percentage($image-height / $target-width);
    background-image: image-url($image-path);
    background-size: contain;
    background-position: bottom;
    background-repeat: no-repeat;
    
    @if($image2x-path) {
      @include image-set(image-url($image-path),image-url($image2x-path));
    }
  }

}
使用Jekyll搭建静态站

在阅读本文之前,首先你要理解什么是静态网站,其次对Github有一定的了解,这不是必须的,但是了解GitHub有助于你完全理解本文。静态网站通常指的是那些由静态化的页面和代码组成的网站,用户在访问和交互的过程中,除了从服务器上下载网站的静态资源之外,不会与服务器进行其他通信,静态网站没有数据库,没有后台,不会保存任何与访问有关的数据。

什么是Jekyll

Jekyll是一个静态站点生成器,它可以将你的源码(如模板、markdown文件、html文件和图片等静态资源)按照指定的配置转换成静态网页文件。

为什么选择Jekyll

安装Jekyll

CSS3简单动画

现代网页中常见的动画有两种:一种是使用canvas来实现的动画,它的原理与现实世界中动画片的原理是一样的,整个过程是通过脚本一帧一帧绘制出来,按照一定的频率播放来达到动画效果的;另一种是简单动画,是对现实世界动画的一种抽象和简化,只需要特别指定动画对象开始和结束的状态,设置动画过程的某些参数,就可以实现动画。

在CSS中,transition和animation是实现简单动画的两种方式,通过这两种方式实现动画的原理可以分解为以下三个步骤

  • 设定目标元素的动画初始状态 - 这是通过为元素赋予不同的CSS属性来实现的
  • 设置在两种状态之间过渡状态参数(动画的时间、延迟、开始、暂停、继续、循环次数等等) - 通过设定transition(animation)属性来实现
  • 开始动画 - 设定目标元素的结束状态

在网页中每一类元素都会包含不同的状态,这些状态由CSS样式来指定。如对于文字而言,文字有大有小、有丰富的颜色、阴影甚至还有镂空等复杂的表现;对于一张图片,有长、宽、圆角、透明度、远近透视、翻转、折叠等等,这些都是元素的状态。元素从起始状态经过变化过渡到结束状态就形成了动画。例如我们要完成一张图片从无到有慢慢出现的效果,这是通过控制其透明度来实现的。下面我们先看一下使用transition如何实现:

Transition

  1. 定义图片的初始状态
.start {
  opacity: 0;
}
  1. 定义图片的结束状态
.end {
  opacity: 1;
}
  1. 使用transition来定义图片从无到有的过渡过程
.animate {
  transition-property: opacity;
  transition-duration: 3s;
  transition-timing-function: linear;
  transition-delay: 1s;
}

这个过渡状态的意思就是,动画延迟1s开始,改变元素的透明度,过渡效果为线性过渡,整个过程持续3s

transition属性意义


属性 |描述 | ———————————————— transition-property |针对哪个属性进行动画 | ———————————————— transition-duration |动画持续时间 | ———————————————— transition-timing-function |动画的速度曲线函数 | ———————————————— transition-delay |延迟动画开始时间 | ————————————————