惊艳的HTML5动画特效及源码,

2019-05-01 作者:计算机教程   |   浏览(109)

利用 CSS 变量实现令人震惊的悬浮效果

2018/05/05 · CSS · 悬浮

原文出处: [Tobias

惊艳的HTML5动画特效及源码,

今天我们要来分享一些很酷的HTML5动画演示,HTML5的强大之处在于它可以利用canvas的特性来绘制很多普通网页无法完成的图形和动画,canvas就像一块超级画板,在上面不仅可以实现平面图形,而且一些3D动画也非常简单。很多HTML5动画也都基于canvas,一起来看看吧。

Reich](https://blog.prototypr.io/stunning-hover-effects-with-css-variables-f855e7b95330)   译文出处:[众成翻译

meakaka](https://www.zcfy.cc/article/stunning-hover-effects-with-css-variables)   

最近,我从 Grover网站上发现以一个好玩儿的悬停动画,也有了些自己的灵感。这个动画是将鼠标移动到订阅按钮上移动光标会显示相应的彩色渐变。这个想法很简单,但是它能使这个按钮脱颖而出,人们一下子就注意到它了,增加了点击的概率。

图片 1

怎样才能达到这个效果,使我们的网站脱颖而出呢?其实,它并不像你想象的那么难!

HTML5/CSS3实现大风车旋转动画

作为今天首款HTML5动画,这款大风车旋转效果的确非常棒,它是用纯CSS3实现。

核心CSS代码:

图片 2.wmd1{ -webkit-transform: scale(.6); position:absolute; top:180px; left:200px; perspective: 1000px; } .base{ } .blades{ width: 350px; height: 350px; left: 10%; top: 10%; z-index:2; border-radius: 50%; position: absolute; margin-top: -30px; margin-left: 50px; animation: spin 6s linear infinite; } .blade1 { background: white; position:absolute; width:41px; height:139px; top:-10px; left:150.5px; transform:rotate(0deg); display:inline-block; background: linear-gradient(135deg, transparent 20px, white 0), linear-gradient(225deg, transparent 20px, white 0), linear-gradient(315deg, transparent 20px, white 0), linear-gradient(45deg, transparent 20px, white 0); background-position: top left, top right, bottom right, bottom left; background-size: 50% 50%; background-repeat: no-repeat; } .blade2 { background:white; position:absolute; width:41px; height:139px; top:105.5px; left:41px; transform:rotate(-90deg); display:inline-block; background: linear-gradient(135deg, transparent 20px, white 0), linear-gradient(225deg, transparent 20px, white 0), linear-gradient(315deg, transparent 20px, white 0), linear-gradient(45deg, transparent 20px, white 0); background-position: top left, top right, bottom right, bottom left; background-size: 50% 50%; background-repeat: no-repeat; } .blade3 { background:white; position:absolute; width:41px; height:139px; top:105.5px; right:41px; transform:rotate(-270deg); display:inline-block; background: linear-gradient(135deg, transparent 20px, white 0), linear-gradient(225deg, transparent 20px, white 0), linear-gradient(315deg, transparent 20px, white 0), linear-gradient(45deg, transparent 20px, white 0); background-position: top left, top right, bottom right, bottom left; background-size: 50% 50%; background-repeat: no-repeat; } .blade4 { background:white; position:absolute; width:41px; height:139px; bottom:-10px; left:150.5px; transform:rotate(180deg); display:inline-block; background: linear-gradient(135deg, transparent 20px, white 0), linear-gradient(225deg, transparent 20px, white 0), linear-gradient(315deg, transparent 20px, white 0), linear-gradient(45deg, transparent 20px, white 0); background-position: top left, top right, bottom right, bottom left; background-size: 50% 50%; background-repeat: no-repeat; } .vane1{ width:1px; height:350px; left:175px; background:white; position:absolute; transform:rotate(90deg); } .vane2{ width:1px; height:350px; left:171.5px; background:white; position:absolute; transform:rotate(180deg); } .base .bottom_base{ position:absolute; width:90px; height:100px; left:162px; border-right: 16px solid transparent; border-left: 16px solid transparent; border-bottom: 380px solid white; opacity:.8; z-index:-1; top:42.5px; } ul{ position:absolute; top:180px; left:-30px; } li{ width:10px; height:10px; background:white; padding:2px; display:block; margin: 30px; box-shadow: inset 0px -2px 0px lightgray; } li:nth-child(2){ position:absolute; top:-45px; left:20px; } li:nth-child(1){ position:absolute; top:35px; left:50px; } li:nth-child(3){ position:absolute; top:75px; left:50px; } @keyframes spin { 0% { transform:rotate(0deg); } 100% { transform:rotate(-360deg); } } View Code

图片 3

在线演示        源码下载

追踪位置

我们要做的第一件事就是获取到鼠标的位置。

document.querySelector('.button').onmousemove = (e) => { const x = e.pageX - e.target.offsetLeft const y = e.pageY - e.target.offsetTop e.target.style.setProperty('--x', `${ x }px`) e.target.style.setProperty('--y', `${ y }px`) }

1
2
3
4
5
6
7
8
9
document.querySelector('.button').onmousemove = (e) => {
 
  const x = e.pageX - e.target.offsetLeft
  const y = e.pageY - e.target.offsetTop
 
  e.target.style.setProperty('--x', `${ x }px`)
  e.target.style.setProperty('--y', `${ y }px`)
 
}
  1. 选择元素,等待,直到用户将鼠标移过它;
  2. 计算相对于元素的位置;
  3. 将坐标存在CSS的变量中。

是的,仅仅9行代码就让你能获知用户放置鼠标的位置,通过这个信息你能达到意想不到的效果,但是我们还是先来完成CSS部分的代码。

纯CSS3 3D图片翻转展示

这是一款基于CSS3的3D图片展示,在图片切换时将会有翻转的动画效果。

核心CSS代码:

图片 4.coverflow-list { width: 100%; } .coverflow-list .coverflow-item { display: inline-block; background: #414141; margin: 0 -50px; position: relative; box-shadow: 0 1px 8px rgba(0, 0, 0, 0.9); } .coverflow-list .coverflow-item .album-cover { display: block; height: 150px; margin: 0; } .coverflow-list .coverflow-item .album-cover img { height: 150px; width: 150px; opacity: 0.5; } .coverflow-list .coverflow-item .album-name { text-align: center; display: block; color: #444; } .coverflow-list .coverflow-item label { padding: 0; border: none; display: block; text-shadow: none; } /* Now apply 3D transforms (never done this before!) */ .coverflow-list { -webkit-transform: perspective(900px); -webkit-transform-style: preserve-3d; -webkit-perspective-origin: 100% 30%; } .coverflow-list .coverflow-item { -webkit-transition: all 0.4s ease; background: #212121; -webkit-transform: rotateY(45deg); } input[type="radio"] { display: none; } input[type="radio"]:checked .coverflow-item { -webkit-transform: rotateY(0deg); margin: 0 auto; background: #313131; transition: all 0.4s ease; } input[type="radio"]:checked .coverflow-item img { opacity: 1; } input[type="radio"]:checked .coverflow-item figcaption { color: #FFFFFF; } input[type="radio"]:checked .coverflow-item ~ .coverflow-item { -webkit-transform: rotateY(-45deg); background: #515151; } View Code

图片 5

在线演示        源码下载

动画渐变

我们先将坐标存储在CSS变量中,以便能够随时使用它们。

.button { position: relative; appearance: none; background: #f72359; padding: 1em 2em; border: none; color: white; font-size: 1.2em; cursor: pointer; outline: none; overflow: hidden; border-radius: 100px; span { position: relative; } &::before { --size: 0; content: ''; position: absolute; left: var(--x); top: var(--y); width: var(--size); height: var(--size); background: radial-gradient(circle closest-side, #4405f7, transparent); transform: translate(-50%, -50%); transition: width .2s ease, height .2s ease; } &:hover::before { --size: 400px; } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
.button {
  position: relative;
  appearance: none;
  background: #f72359;
  padding: 1em 2em;
  border: none;
  color: white;
  font-size: 1.2em;
  cursor: pointer;
  outline: none;
  overflow: hidden;
  border-radius: 100px;
 
  span {
    position: relative;
  }
 
  &::before {
    --size: 0;  
 
    content: '';
    position: absolute;
    left: var(--x);
    top: var(--y);
    width: var(--size);
    height: var(--size);
    background: radial-gradient(circle closest-side, #4405f7, transparent);
    transform: translate(-50%, -50%);
    transition: width .2s ease, height .2s ease;
  }
 
  &:hover::before {
    --size: 400px;
  }
}
  1. span包裹文本,以避免显示在按钮的上方。
  2. 将 widthheight初始化为0px,当用户悬停在按钮上时,将其改为400px。不要忘了设置这种转换以使其像风一样

    本文由www.2003.com发布于计算机教程,转载请注明出处:惊艳的HTML5动画特效及源码,

    关键词: