用一个div绘制背景流动网格特效

如何利用css绘制水平垂直直线组成网格

利用css3llinear-gradient、background-size
原理剖析:
利用css3渐变属性llinear-gradient来绘制横纵直线

 .grid
 	width 100vw
 	height 100vh
    background-image -webkit-linear-gradient(top, 
    	transparent 150px,rgb(66,79,255) 153px)
    background-size 100% 153px
    background-position 50% 50%

这里利用渐变前150px为透明(举例子)后3px为蓝色并通过设置background-size指定背景图片(渐变也是个图片)的大小水平限制设置为 100%(也就是不限制),垂直方向限制成只显示 153px 的范围。这样就会漏出 3 像素的蓝色,看上去就成了一条线了。
在这里插入图片描述依个葫芦画个瓢再加个垂直的竖线,就绘制成网格效果了,完整代码如下图所示

 .grid
   	width 100vw
 	height 100vh
    background-image 
    	-webkit-linear-gradient(top, transparent 150px, rgb(66,79,255) 153px),
    	-webkit-linear-gradient(left, transparent 150px, rgb(66,79,255) 153px)
    background-size 153px 153px
    background-position 50% 50%

在这里插入图片描述
通过改变渐变颜色及宽度,也可以做成格式桌布效果,这是我在上边网格之上调整的效果,知道原理自行调整即可。
在这里插入图片描述

如何形成流动网格效果

这里运用的技术与之前绘制网格的是一样的一样是利用css3,linear-gradientbackground-size形成流动网格线的绘制,需要注意的是这里我们利用伪元素来进行流动网格线的绘制。这里我们利用伪元素在网格线之上又叠加了一层网格线。

.grid::after
    content: ''
    display: inline-block
    height: inherit
    filter:blur(5px)
    background: 
    -webkit-linear-gradient(top, transparent 150px, rgb(111,230,244) 153px),
    -webkit-linear-gradient(left, transparent 150px, rgb(84,104,254) 153px)
    background-size: 153px 153px

下一步就是让网格线动起来,形成流动效果这里运用css3动画annimation来实现效果

 .grid::after
    content: ''
    display: inline-block
    height: inherit
    filter:blur(5px)
    background: -webkit-linear-gradient(top, transparent 150px, rgb(111,230,244) 153px),-webkit-linear-gradient(left, transparent 150px, rgb(84,104,254) 153px)
    background-size: 153px 153px
    animation: clipMe 8s linear infinite

  @keyframes clipMe {
    0%,100%{
      height:1px
      width:1px
    }
    25%{
      height: calc(100%*1/5)
      width: calc(100%*1/5)
    }
    50%{
      height: calc(100%*2/4)
      width: calc(100%*2/4)
    }
    75%{
      height: calc(100%*4/5)
      width: calc(100%*4/5)
    }
    100%{
      height: 100%
      width: 100%
    }

效果如图所示:
在这里插入图片描述
这里还可以通过调整背景图位置,来形成其它一些好玩的效果
在这里插入图片描述

如何平面流动网格图看起来像个背景效果

这里要用到css的3d效果,把平面图沿着x轴向里旋转,然后通过调整视点等形成背景效果。这里重要的一点是需要把网格背景适当放大,这里是放大了两倍,把中心点转换到视口正中,这样形成的背景图,才会有收缩的感觉,而不产生偏转。

em是相对长度单位,相对于px来说,px是固定的像素而em是相对于父元素

  .grid
    position absolute;
    left 50%;
    top 50%;
    margin -100vmax;
    width 200vmax;
    height 200vmax;
    transform-style preserve-3d
    transform: rotateX(80deg) translateZ(-10em)
    background-image -webkit-linear-gradient(top, transparent 150px, rgb(66,79,255) 153px),-webkit-linear-gradient(left, transparent 150px, rgb(84,104,254) 153px)
    background-size 153px 153px
    background-position 50% 50%

产生偏转的网格效果
在这里插入图片描述
放大两倍后的网格效果
在这里插入图片描述

完整代码

<template lang="pug">
  div.fontSzie
    .box
      .grid
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
@Component({
  components: {
  }
})
export default class FlowGrid extends Vue {}
</script>

<style scoped lang="stylus">
.fontSzie
  font-size 16px
.box
  position relative;
  overflow hidden;
  margin 0;
  background rgb(5,11,59);
  height 100vh;
  perspective 65em;
  perspective-origin 50% calc(50%-24em)
  .grid
    position absolute;
    left 50%;
    top 50%;
    margin -100vmax;
    width 200vmax;
    height 200vmax;
    transform-style preserve-3d
    transform: rotateX(80deg) translateZ(-10em)
    background-image -webkit-linear-gradient(top, transparent 150px, rgb(66,79,255) 153px),-webkit-linear-gradient(left, transparent 150px, rgb(84,104,254) 153px)
    background-size 153px 153px
    background-position 50% 50%
  .grid::after
    content: ''
    display: inline-block
    height: inherit
    filter:blur(5px)
    background: -webkit-linear-gradient(top, transparent 150px, rgb(111,230,244) 153px),-webkit-linear-gradient(left, transparent 150px, rgb(84,104,254) 153px)
    background-size: 153px 153px
    animation: clipMe 8s linear infinite

  @keyframes clipMe {
    0%,100%{
      height:1px
      width:1px
    }
    25%{
      height: calc(100%*1/5)
      width: calc(100%*1/5)
    }
    50%{
      height: calc(100%*2/4)
      width: calc(100%*2/4)
    }
    75%{
      height: calc(100%*4/5)
      width: calc(100%*4/5)
    }
    100%{
      height: 100%
      width: 100%
    }
  }
</style>

在Web开发中,使用循环来创建多个`div`元素,并通过点击事件改变某个`div`的背景色是一种常见的操作。这通常可以通过JavaScript结合HTML和CSS来实现。下面是一个简单的实现步骤: 1. 使用HTML创建一个容器元素,例如`div`,用于包含所有的子`div`元素。 2. 使用JavaScript(可以结合循环结构如`for`或`while`)动态地创建多个子`div`元素,并将它们添加到容器元素中。 3. 为每个子`div`元素添加一个点击事件监听器。 4. 在事件处理函数中,改变被点击`div`的背景色。 这里给出一个简单的示例代码: HTML部分: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>点击改变背景色</title> <style> .container { display: flex; flex-wrap: wrap; justify-content: center; } .clickable-div { width: 100px; height: 100px; margin: 5px; display: flex; align-items: center; justify-content: center; cursor: pointer; } </style> </head> <body> <div id="container" class="container"></div> <script> // JavaScript代码将在这里编写 </script> </body> </html> ``` JavaScript部分: ```javascript // 假设我们要创建10个div元素 for (let i = 0; i < 10; i++) { const div = document.createElement('div'); div.className = 'clickable-div'; div.textContent = '点击我'; div.onclick = function() { this.style.backgroundColor = '#' + Math.floor(Math.random()*16777215).toString(16); }; document.getElementById('container').appendChild(div); } ``` 在这个示例中,我们创建了一个`container`元素用于存放子`div`,并通过循环创建了10个可点击的子`div`。每个子`div`在被点击时会随机改变其背景色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值