在当今互联网时代,网站成为了人们获取信息、交流、互动、展示等的主要工具,同时也成为了品牌导向和推广必备的渠道之一。对于网站制作来说,不仅需要考虑网站的结构和内容,还需要考虑网页的视觉效果,既能吸引用户的注意,也能提高用户体验,让用户留下深刻印象。而网页特效代码就是实现网页视觉效果的重要工具之一。
网页特效,是指一种能够美化、优化、增强网页效果的技术手段,比如动态效果、交互效果、视觉效果等。酷炫的网页特效可以使网站变得更加吸引人,用户可以更加轻松地了解网站的内容和服务,最终达到网站的宣传和推广目的。下面将给大家介绍一些常用的网页特效代码,帮助大家打造独具一格的网站视觉效果。
1. 鼠标悬停放大图片效果
在网站的图片展示中,悬停放大图片效果是一种常用特效。通过使用CSS3的transform函数实现网页图片的放大效果,让用户更加直观、深度地了解产品或服务。代码如下:
```
/* 鼠标悬停放大图片效果 */
.img-hover{
transition: all 0.4s ease-in-out;
transform: scale(1);
}
.img-hover:hover{
transform: scale(1.2);
}
```
2. 内容滑动显示效果
内容滑动显示效果是一种流行的网页可交互特效效果,通过设置CSS3的transition属性,实现网站产品或服务信息的突出显示,让用户更加直观地了解网站的服务内容。代码如下:
```
/* 内容滑动显示效果 */
.slide-down{
max-height:0px;
overflow:hidden;
transition: all 0.5s ease-in-out;
}
.slide-down.active{
max-height:1000px;
transition: all 0.5s ease-in-out;
overflow:auto;
}
```
3. 图片轮播特效
轮播特效可以把网站上的多张图片进行轮播播放,让用户快速浏览网站的内容,并且通过特定的动画方式增加用户视觉享受。常见的轮播特效有淡入淡出、旋转、滑动等,代码如下:
```
/* 图片轮播特效 */
.slider{
overflow:hidden;
}
.slider ul{
position:relative;
left:0%;
width:300%;
display:flex;
list-style:none;
transition:left 300ms ease-in-out;
}
.slider ul li{
position:relative;
flex:1;
}
.slider ul li img{
width:100%;
height:auto;
}
.slider ul li div{
width:100%;
height:100%;
position:absolute;
top:0;
left:0;
background-color:rgba(0, 0, 0, 0.5);
color:white;
z-index:1;
display:flex;
align-items:center;
justify-content:center;
opacity:0;
transition:all 0.2s linear;
}
.slider ul li:hover div{
opacity:1;
}
.slider ul li div h3{
font-size:28px;
text-align:center;
font-weight:700;
}
.slider ul li div p{
margin-top:20px;
font-size:16px;
text-align:center;
}
.slider ul li:nth-child(1){
background-color:#f2f2f2;
}
.slider ul li:nth-child(2){
background-color:#ccc;
}
.slider ul li:nth-child(3){
background-color:#f2f2f2;
}
.slider ul li:nth-child(4){
background-color:#ccc;
}
.slider ul li:nth-child(5){
background-color:#f2f2f2;
}
.slider ul li:nth-child(6){
background-color:#ccc;
}
.prev,
.next{
position:absolute;
width:40px;
height:40px;
top:50%;
transform:translateY(-50%);
display:flex;
align-items:center;
justify-content:center;
background-color:white;
color:#333;
font-size:24px;
transition:all 0.2s linear;
z-index:2;
}
.prev:hover,
.next:hover{
cursor:pointer;
}
.prev{
left:0;
}
.next{
right:0;
}
```
4. 鼠标悬停滑动菜单效果
鼠标悬停滑动菜单效果是一套非常出色的交互效果,可以很好地增加用户体验。鼠标悬停在列表上时,会有一个下拉菜单出现,展示更多子级菜单,让用户更加方便地访问所需页面。代码如下:
```
/* 鼠标悬停滑动菜单效果 */
.dropdown-menu{
position:relative;
display:inline-block;
}
.dropdown-menu ul{
position:absolute;
top:100%;
left:0%;
min-width:200px;
border:1px solid #ddd;
padding:10px;
background-color:white;
box-shadow:0px 0px 10px rgba(0,0,0,0.2);
opacity:0;
visibility:hidden;
transition:all 0.2s linear;
z-index:1;
}
.dropdown-menu:hover ul{
opacity:1;
visibility:visible;
}
.dropdown-menu ul li{
list-style:none;
padding:5px 10px;
}
.dropdown-menu ul li:hover{
background-color:#f2f2f2;
}
.dropdown-menu ul li a{
color:#333;
text-decoration:none;
}
```
5. 粒子化特效
粒子化特效可以将网站的元素划分为多个小粒子,通过运用JS动画技术来模拟颗粒物理场景,达到炫酷效果的同时让用户更容易理解网站的内容。代码如下:
```
/* 粒子化特效 */
var particleCount = 300;
var particleMax = 1000;
var sky,trees,field;
var loop = true;
var mouse = {x:0,y:0};
var particles = [];
function triangulate(x1, y1, x2, y2, x3, y3){
var a = x2 - x1,
b = y2 - y1,
c = x3 - x1,
d = y3 - y1,
e = a * d - b * c,
f = b * c - a * d,
g = -(a * x1 + b * y1),
h = -(c * x1 + d * y1),
i = 1 / e,
x = -(d * g - b * h) * i,
y = -(a * h - c * g) * i;
return [x,y];
}
function modulo(n, m){
return ((n % m) + m) %m;
}
function clamp (a,b,c){
return Math.max(b,Math.min(c,a));
}
function Particle (x,y){
this.x = 0;
this.y = 0;
this.vx = (Math.random() * 1) - 0.5;
this.vy = (Math.random() * 1) - 0.5;
this.x = x || 0;
this.y = y || 0;
this.draw = function (){
this.x += this.vx;
this.y += this.vy;
if(this.x < -Field.w) this.x += Window.w;
if(this.y < -Field.h) this.y += Window.h;
if(this.x > Field.w) this.x -= Window.w;
if(this.y > Field.h) this.y -= Window.h;
sky.context.beginPath();
sky.context.fillStyle = this.color;
sky.context.arc(this.x, this.y, this.radius * sky.pixelRatio, 0, Math.PI * 2, true);
sky.context.fill();
}
}
sky = {
canvas: document.createElement('canvas'),
context: null,
stars: 3000,
color: '#222',
pixelRatio: window.devicePixelRatio || 1,
resizeWindow: function (){
this.canvas.width = Window.w * this.pixelRatio;
this.canvas.height = Window.h * this.pixelRatio;
this.canvas.style.width = Window.w + 'px';
this.canvas.style.height = Window.h + 'px';
},
init: function (){
sky.context = sky.canvas.getContext('2d');
this.resizeWindow();
this.context.fillStyle = this.color;
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
},
update: function (){
var i, x, y;
for(i = 0; i < particleCount; i++){
particles.push(new Particle(Math.random() * Field.w, Math.random() * Field.h));
}
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
for (i = 0; i < particles.length; i++){
particles[i].draw();
}
}
};
trees = {
canvas: document.createElement('canvas'),
context: null,
color: '#4d4',
pixelRatio: window.devicePixelRatio || 1,
init: function (){
this.context = this.canvas.getContext('2d');
this.canvas.width = Window.w * this.pixelRatio;
this.canvas.height = Field.h * this.pixelRatio;
this.canvas.style.width = Window.w + 'px';
this.canvas.style.height = Field.h + 'px';
},
update: function (){
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.context.fillStyle = this.color;
for(var i = 0; i < 4; i++){
this.context.fillRect((Field.w / 4) * i + 0.5, 0, 1, Field.h);
}
for(var i = 0; i < 20; i++){
this.context.save();
this.context.translate(Math.random() * Field.w, Math.random() * 5);
this.context.fillStyle = '#050';
this.context.fillRect(0, -5, 2, 5);
this.context.restore();
}
}
};
field = {
canvas: document.createElement('canvas'),
context: null,
colors: ['#fff', '#ccc', '#999'],
grassColor: '#093',
pixelRatio: window.devicePixelRatio || 1,
init: function (){
this.context = this.canvas.getContext('2d');
this.canvas.width = Window.w * this.pixelRatio;
this.canvas.height = Field.h * this.pixelRatio;
this.canvas.style.width = Window.w + 'px';
this.canvas.style.height = Field.h + 'px';
},
update: function (){
var i, j, k;
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
for (i = -1; i < ((Field.w / 32) | 0) +1; i++) {
for (j = -1; j < ((Field.h / 32) | 0) +1; j++) {
this.context.fillStyle = this.colors[((i+j) % this.colors.length + this.colors.length) % this.colors.length];
for (k = 2; k >= 0; k--){
triangle(
i*32 + k*16 + (((j+k)%2)*16), j*32,
i*32 + k*16 + (((j+k)%2)*16), (j+1)*32,
i*32 + (k+1)*16 + (((j+k+1)%2)*16), (j+1)*32,
field.context
);
}
}
}
this.context.fillStyle = this.grassColor;
this.context.fillRect(0, Field.h-4, Field.w, Field.h);
}
};
function triangle (x1, y1, x2, y2, x3, y3, context) {
var p = triangulate(x1, y1, x2, y2, x3, y3);
context.beginPath();
context.moveTo(x1, y1);
context.lineTo(x2, y2);
context.lineTo(x3, y3);
context.lineTo(x1, y1);
context.closePath();
context.fill();
}
var Window = {
w: window.innerWidth,
h: window.innerHeight
};
var Field = {
init: function () {
this.h = Window.h /2;
this.w = Window.w /2;
sky.init();
trees.init();
field.init();
},
update: function () {
sky.update();
trees.update();
field.update();
}
};
Window.resize = function () {
Window.w = window.innerWidth;
Window.h = window.innerHeight;
Field.init();
};
Window.init = function () {
document.body.appendChild(sky.canvas);
document.body.appendChild(trees.canvas);
document.body.appendChild(field.canvas);
};
Window.init();
Field.init();
function anim () {
if(loop){
requestAnimationFrame(anim);
}
Field.update();
}
document.addEventListener('mousemove', function (e){
mouse.x = e.clientX || e.pageX;
mouse.y = e.clientY || e.pageY;
}, false);
document.addEventListener('resize', function (){
Window.resize();
}, false);
anim();
```
通过学习和应用这些网页特效代码,我们可以为网站增添视觉上的亮点。值得注意的是,特效的使用要恰到好处,避免对用户造成干扰,才能让网站的视觉效果达到最佳效果。