HTML5特效(shadow、gradient、transition、transform、filter)

注意!
由于HTML5的兼容性,不同版本的浏览器的显示效果可能不一样,所以要注意兼容性问题。笔者使用的浏览器是Chrome,就需要在属性前添加-webkit-

阴影(shadow)

阴影主要有两种,分别为box-shadowtext-shadow,主要观察阴影的延展方向以及颜色
其中主要包含四个属性,分别为水平阴影、垂直阴影、模糊距离、阴影颜色

box-shadow

语法:

div {
	width: 100px;
	height: 100px;
	background-color: pink;
	box-shadow: 10px 8px 0 gray;
	/*属性:水平阴影 垂直阴影 模糊距离 阴影颜色*/
}

显示结果如图:
阴影2

分析一下可以知道,阴影从该div块的中心射出,这些阴影可以假设组合成了一个和div块重合的阴影块,该阴影块的位置是可以相对移动的,可以通过设置模糊距离来显示它的作用范围。
比如现在我将它的阴影块如下设置:

div {
	width: 100px;
	height: 100px;
	background-color: pink;
	box-shadow: 50px 50px 50px gray;
	/*属性:水平阴影 垂直阴影	模糊距离 阴影颜色*/
}

显示图像为:
阴影3
一个圆,看见了吗?

text-shadow

字体阴影也差不多

p {
	background-color: yellowgreen;
	color: black;
	text-shadow: -1px -1px 0 white;
}

显示效果如图:
在这里插入图片描述
实际开发中,可能还会设置多层字体阴影以达到层级效果


渐变(gradient)

渐变用于背景图(background-image),也分为两种,分别是水平渐变linear-gradient和垂直渐变radial-gradient,需要注意渐变方向颜色变化范围,如果不指明某颜色的范围,则等比例变化
主要属性为:(方向, 颜色1 百分比, 颜色2 百分比……)

linear-gradient

水平渐变,顾名思义,就是以线性方式实现颜色的变化,方向可以设置为left、top、right、bottom

div:nth-of-type(1) {
	/* 注意兼容性问题!以下代码不再做提醒 */
	background-image: -webkit-linear-gradient(left, cyan 20%, gold 50%, tomato, hotpink, skyblue);
	background-image: linear-gradient(left, cyan 20%, gold 50%, tomato, hotpink, skyblue);
}

显示效果如图:
水平渐变

radial-gradient

垂直渐变,注意背景图颜色变化是从一个点开始的,从内向外扩散

div:nth-of-type(2) {
	background-image: -webkit-radial-gradient(top,hotpink,skyblue, gold, orange, tomato);
	background-image: radial-gradient(top,hotpink,skyblue, gold, orange, tomato);
}

显示效果如图:
垂直渐变
方向设置成top,默认以顶部中间位置为圆心,也可以从左上方(top left)开始:

div:nth-of-type(2) {
	background-image: -webkit-radial-gradient(top left,hotpink,skyblue 50%, gold, orange, tomato);
	background-image: radial-gradient(top left,hotpink,skyblue 50%, gold, orange, tomato);
}

显示结果如图:
垂直渐变2,从左上方开始渐变
如果不设置方向,那就默认从中间圆心处开始向外扩散


过渡(transition)

过渡,可以玩得花里胡哨,主要用于设置不同状态间的转变效果,它有四个属性,分别为:

属性名作用
transition-property设置过渡属性名称
transition-duration定义过渡时间
transition-delay定义延时执行时间
transition-property设置过渡的时间曲线

这有张图
过渡属性

通常使用时不会如此花哨,就设置一个过渡时间transition-duration
比如:

div {
	width: 100px;
	height: 100px;
	background-color: cyan;
	/* 设置渐变时间为0.3s */
	transition: .3s;
}
div:hover {
	cursor: pointer;
	width: 300px;
	height: 200px;
	background-color: pink;
}

鼠标未移至div上时显示效果如图:
在这里插入图片描述
鼠标移至div上后显示效果如图:
过渡后
还没试过上传动图到博文将就用用吧别问问就是懒自己码码就知道它怎么变的了


变形(transform)

变形,可以改变元素位置、形状、大小
有五个属性:

属性名作用单位
translate[X/Y/Z]平移px
rotate[X/Y/Z]旋转deg、turn、grad
scale[X/Y/Z]伸缩数值
skew[X/Y]拉扯deg
transform-style:preserve-3d3d效果

额,这个就留给大哥们自己去动手码码吧~
下面是代码格式,等我啥时候会了贴动图再来添加动画

div {
	width: 200px;
	height: 200px;
	background-color: pink;
	transition: .5s;
}
div:hover {
	/*平移*/
	/*transform: translateX(100px);*/
	
	/*旋转   deg   turn  grad*/
	/*transform: rotate(360deg);*/
	/* transform: rotate(1turn); */
	/*transform: rotate(400grad);*/
	/* transform: rotateX(90deg); */
	
	/*缩放*/
	/* transform: scaleY(.2); */
	/* transform: scale(3); */
	
	/*倾斜*/
	/* transform: skewY(-90deg); */
}

滤镜(filter)

滤镜,在线修图,功能多,用的时候再看吧
原图:
滤镜:原图
毁灭吧赶紧的

属性名作用取值范围显示结果
filter:blur(4px)模糊大于0在这里插入图片描述
filter:brightness(2)亮度正自然数在这里插入图片描述
filter: contrast(5)对比度正自然数在这里插入图片描述
filter: drop-shadow(5px 5px 5px tomato)阴影阴影
filter: grayscale(1);灰度[0,1]灰度
filter: hue-rotate(180deg);色相转变[0,360deg]色相转变
filter: invert(1)反转颜色[0,1]反转颜色
filter: opacity(.25)透明度[0,1]透明度
filter: saturate(10)饱和度[0,10]饱和度
filter: sepia(.5)褐色[0,1]褐色

代码如下:

div {
	width: 200px;
	height: 200px;
	background-image: url(img/xhr.jpeg);
	background-size: cover;

	/*模糊*/
	/* filter: blur(5px); */
	/*亮度*/
	/* filter: brightness(2); */
	/*对比度*/
	/* filter: contrast(5); */
	/*阴影*/
	/* filter: drop-shadow(5px 5px 5px tomato); */
	/*灰度*/
	/* filter: grayscale(1); */
	/*色相转变*/
	/* filter: hue-rotate(180deg); */
	/*反转*/
	/* filter: invert(1); */
	/*透明度*/
	/* filter: opacity(.25); */
	/*饱和度*/
	/* filter: saturate(10); */
	/*褐色*/
	/* filter: sepia(.5); */
}
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是几个有趣的 HTML5 特效代码,供您参考: 1. 粒子效果 ```html <!DOCTYPE html> <html> <head> <style> #particles { position: absolute; width: 100%; height: 100%; top: 0; left: 0; z-index: -1; } </style> </head> <body> <canvas id="particles"></canvas> <script> var canvas = document.getElementById('particles'); var ctx = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; var particles = []; function Particle() { this.x = canvas.width * Math.random(); this.y = canvas.height * Math.random(); this.vx = Math.random() * 10 - 5; this.vy = Math.random() * 10 - 5; this.size = Math.random() * 3 + 1; this.opacity = Math.random(); } Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; if (this.x < 0 || this.x > canvas.width) { this.vx = -this.vx; } if (this.y < 0 || this.y > canvas.height) { this.vy = -this.vy; } this.opacity -= 0.01; if (this.opacity < 0) { this.opacity = 0; } }; Particle.prototype.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.fillStyle = 'rgba(255, 255, 255, ' + this.opacity + ')'; ctx.fill(); }; function loop() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < particles.length; i++) { particles[i].update(); particles[i].draw(); if (particles[i].opacity === 0) { particles.splice(i, 1); } } if (particles.length < 1000) { particles.push(new Particle()); } requestAnimationFrame(loop); } loop(); </script> </body> </html> ``` 2. 水波纹效果 ```html <!DOCTYPE html> <html> <head> <style> #ripple { position: relative; width: 300px; height: 300px; margin: 50px auto; background-color: #eee; overflow: hidden; } #ripple:after { content: ''; position: absolute; width: 100%; height: 100%; top: 0; left: 0; background-image: radial-gradient(circle, #fff 10%, #000 90%); background-position: center; background-repeat: no-repeat; background-size: 0 0; opacity: 0.7; transition: background-size 0.3s ease-out; } </style> </head> <body> <div id="ripple"></div> <script> var ripple = document.getElementById('ripple'); ripple.addEventListener('click', function(e) { var x = e.clientX - ripple.offsetLeft; var y = e.clientY - ripple.offsetTop; var size = 0; var interval = setInterval(function() { size += 5; ripple.style.backgroundSize = size + 'px ' + size + 'px'; ripple.style.backgroundPosition = x + 'px ' + y + 'px'; if (size > 300) { clearInterval(interval); ripple.style.backgroundSize = '0 0'; } }, 30); }); </script> </body> </html> ``` 3. 3D 立方体翻转效果 ```html <!DOCTYPE html> <html> <head> <style> .container { perspective: 1000px; perspective-origin: center; width: 400px; height: 400px; margin: 50px auto; } .cube { position: relative; width: 100%; height: 100%; transform-style: preserve-3d; transition: transform 1s; } .cube:hover { transform: rotateY(180deg); } .cube .face { position: absolute; width: 400px; height: 400px; background-color: #eee; border: 1px solid #ccc; line-height: 400px; font-size: 48px; text-align: center; } .cube .front { transform: translateZ(200px); } .cube .back { transform: rotateY(180deg) translateZ(200px); } </style> </head> <body> <div class="container"> <div class="cube"> <div class="face front">Front</div> <div class="face back">Back</div> </div> </div> </body> </html> ``` 希望这些代码对您有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值