Code Monkey home page Code Monkey logo

mini-note's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

wyx111

mini-note's Issues

css image-set

width: 200px;
height: 300px;
background: url(https://picsum.photos/id/234/200/300);
background: -webkit-image-set(url(https://picsum.photos/id/234/200/300) 1x, url(https://picsum.photos/id/234/400/600) 2x);
background-size: cover;

在不同设备像素比下加载不同规格的图片
有点类似img的src-set
目前chrome支持需要写前缀

css revert

重置属性值为其继承到的或者用户代理设置的样式

目前基本只能在safari和firefox上看到效果

DEMO:

h3 { 
  font-weight: normal;
  color: blue;
  border-bottom: 1px solid grey;
}
<h3>This will have custom styles</h3>
<p>Just some text</p>
<h3 style="all: revert">This should be reverted to browser/user defaults</h3>
<p>Just some text</p>

image

.parent {
  color: blue;
}
p {
  color: red;
}
.alt {
  color: revert;
}
<div class="parent">
  <p>Lorem, ipsum dolor.</p>
  <p class="alt">Fugit, id vel.</p>
</div>

image

参考:
https://developer.mozilla.org/zh-CN/docs/Web/CSS/revert

移动file上传限制

图片

<input type="file" accept="image/*">

视频

<input type="file" accept="video/*">

音频

<input type="file" accept="audio/*">

摄像

<input type="file" accept="image/*" capture="camera">

录像

<input type="file" accept="video/*" capture="camera">

accept=".jpg,.png"可能出现“没有应用可执行操作”报错

window.matchMedia

mql = window.matchMedia(mediaQueryString)

返回MediaQueryList对象,表示指定媒体查询字符串解析后的结果,对象的matches属性表示是否匹配媒体查询

判断横竖屏

window.matchMedia("(orientation: portrait)").matches

orientation: portrait | landscape
portrait 高度大于等于宽度

判断移动设备

window.matchMedia('(max-device-width: 20em)').matches

监听查询变化

function handleOrientationChange(mql) {
  if (mql.matches) {
    // ...
  } else {
    // ...
  }
}

var mql = window.matchMedia("(orientation: portrait)");
mql.addListener(handleOrientationChange);
handleOrientationChange(mql); 

// 移除监听事件
mql.removeListener(handleOrientationChange);

兼容性 IE 10+

相关阅读:
https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Testing_media_queries
https://css-tricks.com/a-web-component-with-different-html-for-desktop-and-mobile/

svg滤镜实现Gooey(粘黏)效果

circle mov

  * { box-sizing: border-box; }
	.box {
      position: relative;
      filter: url('#colorMatrix');  /* 关键代码就这一行 */
      height: 200px;
      width: 200px;
    }

    span {
      position: absolute;
      right: 10px;
      bottom: 10px;
      height: 50px;
      width: 50px;
      border-radius: 50%;
      text-align: center;
      color: #fff;
      line-height: 50px;
      background: yellowgreen;
      transition: transform .6s cubic-bezier(0.2,0,0.15,1);
    }

    .span1 { transition-delay: .1s; }

    .span2 { transition-delay: .25s; }

    .span3 { transition-delay: .4s; }

    label {
      position: absolute;
      z-index: 10;
      right: 10px;
      bottom: 10px;
    }

    label span {
      right: 0;
      bottom: 0;
    }

    input:checked ~ .span1 {
      transform: translate3d(-80px, 0, 0);
    }

    input:checked ~ .span2 {
      transform: translate3d(-64px, -64px, 0);
    }

    input:checked ~ .span3 {
      transform: translate3d(0, -80px, 0);
    }
	<div class="box">
    <label for="input"><span><i class="fa fa-plus"></i></span></label>
    <input type="checkbox" id="input" style="opacity: 0">
    <span class="span1"><i class="fab fa-500px"></i></span>
    <span class="span2"><i class="fab fa-facebook"></i></span>
    <span class="span3"><i class="fab fa-twitter"></i></span>
  </div>
  <svg viewBox="0 0 200 200" width="200px"  xmlns="http://www.w3.org/2000/svg" version="1.1">
    <defs>
      <filter id="colorMatrix">
      <feGaussianBlur
        in="SourceGraphic"
        stdDeviation="7"
        result="blur"
      ></feGaussianBlur>
      <feColorMatrix
        in="blur"
        type="matrix"
        values="1 0 0 0 0  
                0 1 0 0 0  
                0 0 1 0 0  
                0 0 0 18 -7"
        result="matrix"
      >
      </feColorMatrix> 
      <feBlend in="SourceGraphic" in2="matrix"></feBlend>
    </filter>
    </defs>
  </svg>

feGaussianBlur高斯模糊滤镜
feColorMatrix是一个颜色矩阵计算,这儿相当于对原图形的每一个像素(Alpha透明度按0-1的值算):

1 0 0 0 0 // R = 1*R + 0*G + 0*B + 0*A + 0
0 1 0 0 0 // G = 0*R + 1*G + 0*B + 0*A + 0
0 0 1 0 0 // B = 0*R + 0*G + 1*B + 0*A + 0
0 0 0 18 -7 // A = 0*R + 0*G + 0*B + 18*A + (-7)

即RGB的值不变,修改了透明度通道的对比度

使用css也可以通过添加高斯模糊和提高对比度做类似的效果,但是相比还是svg效果更好一些

css的实现可以看这个 Shape Blobbing in CSS | CSS-Tricks

数字格式化

toLocaleString

numObj.toLocaleString([locales [, options]])

const money = 100000.12;
money.toLocaleString('zh', { style: 'currency', currency: 'CNY' }); 
// "¥1,000.12"

money.toLocaleString('zh', { style: 'currency', currency: 'CNY', currencyDisplay: 'code' });
// "CNY1,000.12"

money.toLocaleString('zh', { style: 'currency', currency: 'cny', currencyDisplay: 'name' });
// "1,000.12 人民币"

currency货币符号,常用的例如CNY人民币,USD美元,JPY日元

currencyDisplay格式化显示,symbol表示本地化的货币符号,code表示国际标准货币代码,name表示本地化货币名称

ES6 Intl

new Intl.NumberFormat([locales[, options]])

options部分和上面的类似

const money = 100000.12;
new Intl.NumberFormat('zh-CN', { style: 'currency', currency: 'CNY'}).format(money);

// "¥100,000.12"

然后简单写个时间的

const date = new Date();
new Intl.DateTimeFormat('zh-CN').format(date); // "2019/6/10"
date.toLocaleString('zh'); // "2019/6/10 上午9:07:06"

兼容性ie11+,两个都是 - -

参考:
Intl.NumberFormat - JavaScript | MDN
Number.prototype.toLocaleString() - JavaScript | MDN
想偷懒的话,toLocaleString 了解一下? – 酷辣虫 CoLaBug

vscode插件推荐

Auto Rename TagHTML标签修改(重命名),vscode的改起始标签不会自动改结束标签,可以用这个插件实现

1_BaMAKMipsC2yUQJejyMu1A

Bracket Pair Colorizer括号对高亮,方便查看代码块,可能影响到主题配色,不过可以自定义颜色和匹配的符号

image

IntelliSense for CSS, SCSS class names in HTML, Slim and SCSS,class智能命名,给出css文件或html文件中出现的class名称列表,也支持link引用的外部文件

image

代码格式化Prettier,之前没用eslint的时候感觉这个挺好用的,但是如果配置eslint保存自动修改可能会和它有一点冲突

Live Server,给页面开一个静态资源服务,并且支持实时更新,很方便很方便很方便

参考:
8 Useful VSCode Add-Ons New Front-end Developers Should Absolutely Download

几个可以尝试使用的ES2019新特性

Object.fromEntries()

数组转对象

const myArray = [['one', 1], ['two', 2], ['three', 3]];
const obj = Object.fromEntries(myArray);

console.log(obj);    // => {one: 1, two: 2, three: 3}

另外还有一个Object.entries()(ES2017)是反过来的

const obj = {one: 1, two: 2, three: 3};

console.log(Object.entries(obj));    
// => [["one", 1], ["two", 2], ["three", 3]]

trimStart() 和 trimEnd()

删除字符串开头、结尾空格

const str = "   string   ";

console.log(str.trimStart());    // => "string   "
console.log(str.trimEnd());      // => "   string"

flat() 和 flatMap()

扁平化嵌套数组
flat(path) path用来指定深度

var arr1 = [1, 2, [3, 4]];
arr1.flat(); 
// [1, 2, 3, 4]

var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]

var arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]

flatMap()类似先map一下再flat(1)

let arr = ["今天天气不错", "", "早上好"]

arr.map(s => s.split(""))
// [["今", "天", "天", "气", "不", "错"],[""],["早", "上", "好"]]

arr.flatMap(s => s.split(''));
// ["今", "天", "天", "气", "不", "错", "", "早", "上", "好"]

Symbol对象描述属性

let sym = Symbol(‘foo’);
console.log(sym.description);    // => foo

sym = Symbol();
console.log(sym.description);    // => undefined

// create a global symbol
sym = Symbol.for(‘bar’);
console.log(sym.description);    // => bar

可选的catch绑定

try {
  
} catch {
  
}

参考:
5 ES2019 features you can use today
Array.prototype.flatMap()

获取图片颜色

    var canvas = document.getElementById('canvas')
    var ctx = canvas.getContext('2d')
    var img = new Image;
    img.setAttribute('crossOrigin', 'anonymous');  // 解决跨域问题  ie11+支持
    img.src = 'https://img-i.gcimg.net/2019/1204/wk2buqe52kwboiza024634.png'
    img.onload = function () {
      ctx.drawImage(img, 0, 0);
      var color = ctx.getImageData(1, 1, 1, 1).data;
      var rgb = `rgb(${color[0]}, ${color[1]}, ${color[2]})`;
      document.body.style.background = rgb;
    }

多行文本溢出显示省略号

屏幕快照 2019-11-05 上午11 44 03

.box {
  width: 200px;
  margin: 0 auto;
}
.multiple {
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}
<div class="box">
  <p class="multiple">层叠样式表(CSS)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。</p>
</div>

-webkit-line-clamp属性可以限制块容器内容的行数,它仅在设置display-webkit-box或者-webkit-line-box并且-webkit-box-orient属性值为vertical时候才会生效

大部分情况下还需要设置overflow: hidden,否则多余的行数不会被截取掉,但是依然会在限制的行末尾显示省略号

参考:
-webkit-line-clamp - CSS(层叠样式表) | MDN

一个CSS Shapes动画

先放codepen地址Wrecking Ball

shape-outside属性可以定义相邻内联元素围绕的形状。

以前做图文混排时图片浮动文字环绕在图周围,环绕的边界是矩形,利用这个属性可以定义圆形circle,椭圆ellipse,图片(环绕图片中的不透明部分),或者是其他几何形状polygon,然后可以实现类似杂志中的炫酷排版,比如Art Direction For The Web Using CSS Shapes — Smashing Magazine

然后拐回去说那个动画:

@keyframes wreckit {
  0% {
    margin-left: -1000px;
    shape-outside: circle( 150px at -850px 150px);
  }
  100% {
    margin-left: 1000px;
    shape-outside: circle( 150px at 1150px 150px);
  }
}

这里使用了一个圆形,然后在左右移动中改变了圆的中心点坐标(半径 at 圆形x 圆心y),因为元素的margin-left值在改变,图形计算又是默认依据margin-box,如果不改圆心结果应该是这个样:

屏幕快照 2019-05-14 下午4 04 43

不过也可以改用其他盒模型,比如shape-outside: circle(100px) content-box;

屏幕快照 2019-05-14 下午4 04 05

话说为啥要分享这个动画……因为第一眼看到的时候觉得迷之搞笑😂

参考
标准:
shape-outside - CSS(层叠样式表) | MDN
文章:
Take A New Look At CSS Shapes — Smashing Magazine
It’s a trap-ezoid: CSS Shapes aren’t what you’dexpect

IntersectionObserver

IntersectionObserver接口提供了目标元素(target)在根元素(root)中可视状态变化的监听方法。
可以用来更方便的实现类似图片懒加载,滚动加载更多,或者一些滚屏动画效果

123123 mov

先画一坨灰div
div*10{item}
    if ('IntersectionObserver' in window) {
      var observer = new IntersectionObserver(function (entries, _observer) {
        entries.forEach(item => {
          if (item.isIntersecting) { // 目标元素在根元素中可见(达到thresholds设置的范围)

            item.target.style.background = '#517791'; // 执行一些啥啥操作,这里只是改了背景色

            _observer.unobserve(item.target); // 取消观察
          }
        })
      }, {
        rootMargin: "0px 0px -60px 0px"  // 监听元素和root边界的偏移量,类似css margin。这里值是距离底部60像素

        // 这里还有两个可设置的属性:
        // root: 指定根元素,需要是目标元素的祖先元素,默认是浏览器视窗(viewport)
        // thresholds: 数组,值是监听元素和root边界交叉区域的比率
        //             例如设置[0.3, 0.7],意味在交叉区域面积30%和70%的位置会执行回调函数
        //             默认是0,即目标元素在root内一出现就会立即调用
      });

      document.querySelectorAll('div').forEach(item => { observer.observe(item) });
    }  

除了兼容性不太好没啥毛病

font 巴拉巴拉

字体压缩

npm install font-spider -g
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    @font-face {
      font-family: 'source';
      src:
        url('./font/SourceHanSerifCN-Heavy.ttf') format('truetype');
      font-weight: normal;
      font-style: normal;
    }
    * {
      font-family: 'source';
    }
  </style>
</head>
<body>
  这里是文字内容
</body>
</html>
font-spider index.html

CSS Grid实现响应式布局

原文:Create a responsive grid layout with no media queries, using CSS Grid - Andy Bell

html:

ul.auto-grid>li*12{Item $}

style:

grid-template-columns定义网格列,auto-fill表示自动填充,单元格等宽并且宽度最小16rem,max值使用1fr会把剩余空间平分给每个item,它们会拉伸占满空间

/* grid */
.auto-grid {
  --auto-grid-min-size: 16rem;
  display: grid;
  /* 网格列 */
  grid-template-columns: repeat(auto-fill, minmax(var(--auto-grid-min-size), 1fr));
  /* 网格间隔 */
  grid-gap: 1rem;
}

/* ... 省略其他的一些样式 */

CSS 加载动画

1 mov

<div class="circle"></div>
.circle {
  position: relative;
  height: 100px;
  width: 100px;
  animation: circle .7s linear infinite;
  border-radius: 50%;

  /* 渐变圆圈 */
  background: 
    radial-gradient(#fff, #fff 63%, transparent 64%),
    linear-gradient(to top, #fa8807, #fff) 50px 0 no-repeat,
    linear-gradient(to top, #fa8807, red) 0 0;
}

/* 旋转动画 */
@keyframes circle {
  0% {
    transform: rotate(0);
  }

  100% {
    transform: rotate(360deg);
  }
}

几个用在表单元素上的css4伪类

完整的css选择器列表: CSS4-Selectors | All selectors from level 4 to 1
需要用的还是先留意下浏览器兼容性😂

:required:optional

:required { color: red; } /* 带有required属性 */
:optional { color: blue; } /* 不带的 */
<input required type="text">  红色文字
<input type="text">  蓝色文字

:valid:invalid

:invalid { color: red; } /* 非法值 */
:valid { color: blue; } /* 合法值 */
<input required type="email">  
填写邮箱蓝色,格式错误红色

:in-range:out-of-range

:out-of-range { color: red; } /* 指定区间外的值 */
:in-range { color: blue; } /* 指定区间内的值 */
<input type="number" value="2" max="4" min="0" step="1" />
0-4以外的值是红色,0-4蓝色

:placeholder-shown

:placeholder-shown { border: 1px solid red; }
<input placeholder="请输入" type="text">
没有输入内容时(placeholder显示)是红色边框

:indeterminate

:indeterminate { opacity: 0.6; } 
/* checkbox 三种状态(checked, unchecked, indeterminate)中的 indeterminate(不确定) */
/* 也可用于radio 和 <progress> */
<label for="test">Label</label>
<input type="checkbox" id="test" />

<script>
// html中没 indeterminate="true" 这个属性,所以需要js设置
  document.getElementById("test").indeterminate = true;
</script>

字符串转DOM

不知道为啥今天就突然想起来了这个东西……

使用DOMParser将xml或html源码解析为DOM Document

const a = '<div class="div"><p>content</p></div>'; 
const parser = new DOMParser(); 
const doc = parser.parseFromString(a, 'text/html');
const result = doc.body.querySelector('.div');

document.body.appendChild(result);

8189C40F-D5C9-4EBF-AF36-8A60B55E6A7D

Range对象的createContextualFragment方法解析xml或html,返回文档片段DocumentFragment

const a = '<div class="div"><p>content</p></div>'; 
const result = document.createRange().createContextualFragment(a);

document.body.appendChild(result);

5C55C40E-23C1-443C-A9C8-FECE21B94173

Convert String to DOM Nodes

随机字母

String.fromCharCode(Math.random() * 26 + 'a'.charCodeAt(0))

css ::marker

::marker伪元素用来设置列表前的符号样式,任何display: list-item的元素都可以使用,比如<li><summary>

demo:

li::marker {
  margin-right: 4px;
  color: red;
  font-size: 0.9em;
}

image

平时设计总喜欢把这个符号设置成不一样的样式,用marker就很方便,但是目前只有新版的Firfox支持(68)

配合counter食用效果

::marker {
  color: rgb(83, 107, 187);
  content: "Step " counter(list-item) ": ";
}

image

css object-fit

用于指定可替换元素(如img iframe等)适应到其宽高的方式,可取值fill | contain | cover | none | scale-down

img {
  height: 100px;
  width: 160px;
  object-fit: contain;
}

效果类似于给背景图设置background-size: contain
兼容性 IE (x)

413 Request Entity Too Large 问题

nodejs返回413 (Request Entity Too Large) 解决方法:

app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(bodyParser.json({limit: '50mb'}));

nginx需要修改nginx.conf文件,然后重启

http {
    client_max_body_size 50M;
}

响应式图片

<img 
  src="https://picsum.photos/100/100?image=1"
  srcset="https://picsum.photos/200/200?image=2 2x, 
          https://picsum.photos/300/300?image=3 3x"
>

根据不同的设备像素比使用不同尺寸的图得到更好的显示效果,一般可以用同一张图的三种尺寸,这里方便看用了不一样的图
chrome里有改像素比的设置

屏幕快照 2019-06-20 上午10 22 17

效果

image

<img 
  src="https://picsum.photos/300/300?image=3"
  srcset="https://picsum.photos/300/300?image=3 300w, 
          https://picsum.photos/200/200?image=2 200w, 
          https://picsum.photos/100/100?image=1 100w"
  sizes="(max-width: 300px) 100px, (max-width: 400px) 200px, 300px"

srcset里的w是图像固有宽度,sizes里的媒体查询后的值是图片要展示的尺寸(px,em,vw都可以);
展示的时候先找符合当前设备宽度的那个媒体查询条件,它后面值与上面srcset的哪个值最接近就用哪个图;
当然不支持的浏览器就会直接用src

例如现在是375px宽的设备,满足(max-width: 400px) 200px就会使用200w的第二张图

屏幕快照 2019-06-20 上午10 56 01

但是如果设备像素比为2,图片实际使用尺寸是200*2,就会用400w的,但是这里没400w,就会用接近他的300w的那个

屏幕快照 2019-06-20 上午10 58 13

简单造富文本编辑器

屏幕快照 2019-07-08 上午8 46 49

起手html你肯定猜到了是啥

<div id="editor" contentEditable></div>

然后是操作可编辑内容区域内容的API:

document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)

aCommandName 命令名称
aShowDefaultUI 是否展示用户界面
aValueArgument 某些需要额外参数的命令的值

返回布尔值表示浏览器是否支持

demo:

简单的执行加粗

<button id="edit-bold">加粗</button>
const btn = document.getElementById('edit-bold');
btn.addEventListener('click', () => {
  document.execCommand('bold', false, null);
}, false);

在可编辑的div内输入内容,选择后点击按钮就会有加粗效果。由于大部分标签在被点击的时候会让div失去焦点,所以要用button或者a标签比较好。

其他比如斜体,下划线,左、中、右对齐,创建列表等和这个类似。

在内容外添加标签需要用到第三个参数,例如标题和引用

document.execCommand('formatBlock', false, '<h1>');

修改字体,文字大小和颜色等类似,需要提供值的字符串作为参数。

具体命令列表参考
document.execCommand - Web API 接口参考 | MDN
阅读参考
富文本原理了解一下? - 掘金

canvas 绘制模糊问题

const d = window.devicePixelRatio || 1;
const b = context.webkitBackingStorePixelRatio || 1;
const ratio = d / b;
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let w = 400;
let h = 400;
canvas.style.width = w + 'px';
canvas.style.height = h + 'px';
canvas.width = w * ratio;
canvas.height = h * ratio;
ctx.scale(ratio, ratio);

获取url参数

示例地址 https://example.com/?product=shirt&color=blue&newuser&size=m

const queryString = window.location.search;
// ?product=shirt&color=blue&newuser&size=m

const urlParams = new URLSearchParams(queryString);

URLSearchParams IE不支持

image

const product = urlParams.get('product')
console.log(product);
// "shirt"

const newUser = urlParams.get('newuser')
console.log(newUser);
// ""

console.log(urlParams.has('product'));
// true

console.log(urlParams.has('paymentmethod'));
// false

console.log(urlParams.getAll('size'));
// [ 'm' ]

for (const key of urlParams.keys()) console.log(key);
// product, color, newuser, size

drop-shadow

语法

filter: drop-shadow(offset-x offset-y blur-radius spread-radius color)

drop-shadow是一个预设的css滤镜函数,用于给图像添加阴影,和box-shadow比较像,区别是它不能设置inset关键字,也不能叠加多个值。drop-shadow不受盒模型的影响,它对图像的非透明部分创建阴影,也就是:

屏幕快照 2019-05-15 下午4 38 28

兼容性:edge 13+ …

css background-filter

可以为元素后面区域添加filter效果,简单例子

@supports (-webkit-backdrop-filter: none) or (backdrop-filter: none) {
  .modal {
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    background-color: rgba(255, 255, 255, 0.5);  
  }
  .warning {
    display: none;
  }
}

屏幕快照 2019-08-06 上午8 48 32

支持的浏览器就可以看到白色区域部分有模糊的效果;
另外为了能生效,元素或它的背景需要有部分透明

.modal { background-color: rgba(255, 255, 255, 0.95); }

目前支持的浏览器不是很多,可以更新下chrome感受感受

参考:
backdrop-filter - CSS(层叠样式表) | MDN
The backdrop-filter CSS property | CSS-Tricks

svg路径文字

css tricks改版后的页面上看到的

<textPath>通过xlink:href引用<path>元素,设置文字的排列;默认是沿着path的左侧(side=“right”支持的浏览器比较少)

圆形路径的文字:

image

<svg height="200" width="200" viewBox="-100,-100,200,200">
  <defs>
    <path id="circle" d="M-100,0 A100,100,0 1 1 100,0 A100,100,0 1 1 -100,0Z"></path>
  </defs>
  <text>
    <textPath xlink:href="#circle">text...</textPath>
  </text>
</svg>

也能画些……乱七八糟的东西……

image

另外css tricks里还用到了自定义元素custom element),定义<circle-text>来封装圆形文字排版的效果,设置半径和旋转角度的属性,复用了svg部分的代码。具体的可以参考Circle Text Custom Element

顺便推荐绘制图形导出svg的软件:
Sketch
Affinity Designer
在线的Figma: the collaborative interface design tool.

其他:
- SVG: Scalable Vector Graphics | MDN
使用 custom elements - Web Components | MDN

移动端弹出数字键盘

<input type="tel" />
<input type=“number” pattern=“\d*” />

type="number"会无视maxlength,可以改用text

<input type="text" pattern=“\d*” />

svg加载动画

codepen
2 mov

原文: https://glennmccomb.com/articles/building-a-pure-css-animated-svg-spinner/

html

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="45"/>
</svg>

css

// SVG 样式.
svg {
  max-width: 100px;
  animation: 2s linear infinite svg-animation;
}

// Circle 样式.
circle {
  animation: 1.4s ease-in-out infinite both circle-animation;
  display: block;
  fill: transparent;
  stroke: #2f3d4c;
  stroke-linecap: round;
  stroke-dasharray: 283; // 圆周长
  stroke-dashoffset: 280;
  stroke-width: 10px;
  transform-origin: 50% 50%;
}

包括三部分的动画

  1. 利用css animation改变<circle>元素stroke-dasharraystroke-dashoffset属性的值,实现的划线伸长变短的效果
@keyframes circle-animation {
  0%,
  25% {
    stroke-dashoffset: 280;
  }
  
  50%,
  75% {
    stroke-dashoffset: 75;
  }
  
  100% {
    stroke-dashoffset: 280;
  }
}
  1. circle元素自身的顺时针旋转

75%到100%旋转的地方划线是逆时针向后收缩变短的效果,由于circle顺时针旋转的比较快,看起来整体还是顺时针旋转

@keyframes circle-animation {
  0%,
  25% {
    // ...
    transform: rotate(0);
  }
  
  50%,
  75% {
    // ...
    transform: rotate(45deg);
  }
  
  100% {
    // ...
    transform: rotate(360deg);
  }
}
  1. svg元素的旋转
svg {
  animation: 2s linear infinite svg-animation;
}

@keyframes svg-animation {
  0% {
    transform: rotateZ(0deg);
  }
  100% {
    transform: rotateZ(360deg)
  }
}

CSS position sticky

position sticky可以用来很方便的实现滚动粘滞效果,不需要判断滚动判断scrollTop一个属性搞定😂

.box { 
  position: sticky; 
  top: 10px;  // 需要指定top right bottom left中的一个才会生效
}

效果相当于相对定位和固定定位的混合,在box到达顶部与父元素距离10像素之前为相对定位,之后就是固定定位。

推荐阅读
杀了个回马枪,还是说说position:sticky吧 « 张鑫旭-鑫空间-鑫生活

虽然兼容性

屏幕快照 2019-06-06 上午8 39 09

可以在支持的浏览器下使用,对其他做降级处理,其他常见的处理方式可以看这个

【前端词典】5 种滚动吸顶实现方式的比较性能升级版 - 掘金

同时下载多个文件

异步请求后端返回了多个文件下载地址

function downloadFile(url) {
      const iframe = document.createElement('iframe');

      iframe.style.display = 'none'; 
      iframe.style.height = 0;
      iframe.src = url; 
      document.body.appendChild(iframe);

      setTimeout(() => {
        iframe.remove();
      }, 1000);
    }

参考:https://www.cnblogs.com/lalalagq/p/9901893.html

Chrome插件推荐——Feeder

方便的使用RSS订阅功能

屏幕快照 2019-11-29 下午3 45 18

可以直接搜索关键词查找支持订阅的内容,或者在下面的分类里也可以找

屏幕快照 2019-11-29 下午3 41 50

一天不读书,无人看得出
一月不读书,智商输给猪

image-rendering

image-rendering:  auto | smooth | high-quality | crisp-edges | pixelated

设置图像缩放算法,指定图片缩小或者放大的情况下如何展示。
可以用于img元素,或者背景图和canvas中的图像。

借用w3c的栗子:

原图:
image

设置 auto | pixelated | crisp-edges 后“理想”的效果是:
image

chrome支持pixelated,firefox支持crisp-edges但是效果和pixelated差不多,参考Can I use

pixelated很适合像素图,比如……二维码(左边设置pixelated, 右边正常)

image

image

更多:

image-rendering - CSS(层叠样式表) | MDN

CSS Images Module Level 3

flex wrap指定位置换行

<style>
  .flex {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 10px;
  }
  .item {
    padding: 10px;
    background-color: cornflowerblue;
  }
  .flex-col {
    flex-direction: column;
    align-items: flex-start;
    align-content: flex-start;
    height: 300px;
  }
  .break {
    flex-basis: 100%;
    width: 0;
    height: 0;
  }
</style>
<div class="flex">
  <div class="item item1">1</div>
  <div class="item item2">2</div>
  <div class="item item3">3</div>
  <div class="break"></div>
  <div class="item item4">4</div>
  <div class="item item5">5</div>
  <div class="item item6">6</div>
  <div class="item item7">7</div>
  <div class="item item8">8</div>
</div>


<div class="flex flex-col">
  <div class="item item1">1</div>
  <div class="item item2">2</div>
  <div class="item item3">3</div>
  <div class="break"></div>
  <div class="item item4">4</div>
  <div class="item item5">5</div>
  <div class="item item6">6</div>
  <div class="item item7">7</div>
  <div class="item item8">8</div>
</div>

html <details>

<details>
  <summary>title</summary>
  <div class="content">content...</div>
</details>

<details>有一个open属性(表示内容部分是否可见的布尔值)和toggle事件(可见状态改变触发),它的默认的样式

屏幕快照 2019-05-27 上午9 35 10

details和summary可以用来实现一些常用的折叠展开效果,例如手风琴,折叠菜单,下拉菜单(栗子看这个页面右上角)

屏幕快照 2019-05-27 上午9 28 47

这里利用了open属性和before伪元素,添加了一个点击菜单外的关闭效果

甚至是做个弹窗
GitHub - github/details-dialog-element

关于默认样式中的小箭头样式,标准方法是改list-style(试了下firfox可以),chrome中可以用summary::-webkit-details-marker修改

summary::-webkit-details-marker {
  background: url(right-arrow.svg);
  color: transparent;
}

关于浏览器支持:反正用Chromium的Edge是要支持了😂……
Can I use… Support tables for HTML5, CSS3, etc

阅读更多:
借助HTML5 details,summary无JS实现各种交互效果 « 张鑫旭-鑫空间-鑫生活
Quick Reminder that Details/Summary is the Easiest Way Ever to Make an Accordion | CSS-Tricks

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.