H利用模糊实现视觉3D效果实例讲解 setInterval和clearInterval方法案例详解

文章来源: 学习通http://www.bdgxy.com/

普学网http://www.boxinghulanban.cn/

智学网http://www.jaxp.net/


在javascript编程中,setInterval可以帮助我们实现一个定时器的功能,能够让我们定时执行某一项操作,如果不需要继续执行了,我们只需要调用clearInterval函数,清除定时器即可。

这里要重点说的是清除定时器的时机,我们一般是需要进行一个条件判断,比如 var count=5,我们定时执行count--操作,当count==0的时候,清除定时器,一般情况下,我们的代码是这样写的。

这段代码,逻辑上没有什么问题, 就是当count==0的时候,我们清除定时器。我们运行这段代码,看看效果:

我们惊奇的发现,并不是我们想象中的那样,setInterval这个函数,并不会在清理定时器之后,就退出,后续的操作还是会执行。为了让清除定时器之后的代码不再执行,我们可以直接进行return返回。

p {
font-size: 24vmin;
transform-style: preserve-3d;
animation: rotate 10s infinite ease-in-out;
}

@keyframes rotate {
0% {
transform: rotateY(-45deg);
}
50% {
transform: rotateY(45deg);
}
100% {
transform: rotateY(-45deg);
}
}

我们就可以得到这样一个 3D 文字效果:

实现文字的模糊

这个效果已经有了初步的 3D 效果,但是仅仅是这样,会觉得少了些什么。接下来我们就需要补充一下模糊的效果,让距离我们近的文字清晰,远离我们的文字模糊。

但这样就需要对每个文字进行精细化处理,上面的 HTML 结构无法做到对每一个文字的单独处理,我们简单改造一下结构:

<p>
    <span>C</span>
    <span>S</span>
    <span>S</span>
    <span>3</span>
    <span>D</span>
    <span>E</span>
    <span>F</span>
    <span>F</span>
    <span>E</span>
    <span>C</span>
    <span>T</span>
</p>

完整的代码大概是这样:

@import url('https://fonts.googleapis.com/css2?family=Lobster&display=swap');

$count: 12;

body, html {
font-family: ‘Lobster’, cursive;
perspective: 160vmin;
overflow: hidden;
}

p {
margin: auto;
font-size: 24vmin;
transform-style: preserve-3d;
animation: rotate 10s infinite ease-in-out;

span {
    text-shadow: 
        1px 1px 0 rgba(0, 0, 0, .9),
        2px 2px 0 rgba(0, 0, 0, .7),
        3px 3px 0 rgba(0, 0, 0, .5),
        4px 4px 0 rgba(0, 0, 0, .3),
        5px 5px 0 rgba(0, 0, 0, .1);
    
    &amp;:nth-child(-n+5) { 
        animation-delay: -5s; 
    }
}

}

@for KaTeX parse error: Expected '}', got '#' at position 36: …span:nth-child(#̲{i}),
span:nth-last-child(#{KaTeX parse error: Expected 'EOF', got '}' at position 2: i}̲) { ani…i} 10s infinite ease-in-out;
}

@keyframes filterBlur-#{$i} {
    0% {
        filter: blur(0px) contrast(5);
    }
    50% {
        filter: blur(#{7 - $i}px) contrast(1);
    }
    100% {
        filter: blur(0px) contrast(5);
    }
}

}
@keyframes rotate {
0% {
transform: rotateY(-45deg);
}
50% {
transform: rotateY(45deg);
}
100% {
transform: rotateY(-45deg);
}
}

简单解析下,这里有几个小技巧,仔细观察我们需要的效果:

1.第一个字符和最后一个字符在旋转的最左效果和最右效果下分别会离我们最近和最远,它们的效果其实应该是一致的,所以第一个字符和最后一个字符应该统一处理,依次类推,第二个字符和倒数第二字符统一处理,这里可以借助 SASS 利用 :nth-child:nth-last-child 高效编写 CSS 代码

2.每次有一半是清晰的,一半的是模糊的,需要区分对待,利用 animation-delay 让一半的动画延迟一半进行

3.可以再配合 text-shadow 让文字更立体点

这样,我们可以最终得到如下效果:

完整的代码,你可以戳这里 -- CSS 灵感 -- 利用 filter:blur 增强文字的 3D 效果

使用模糊构建落叶效果

合理运用模糊,是能在没有 transform-style: preserve-3dperspective 的加持下,也能构建出不错的 3D 效果。

之前在 Youtube 的一个视频教学网站看到了下面这个落叶效果,就是利用模糊以及简单的层级关系,让整个画面看上去非常的真实:

<h2>Falling Leaves</h2>
<section>
  <div class="leaf">
    <div><img src="落叶图片.png" /></div>
    <div><img src="落叶图片.png" /></div>
    <div><img src="落叶图片.png" /></div>
    <div><img src="落叶图片.png" /></div>
    <div><img src="落叶图片.png" /></div>
    <div><img src="落叶图片.png" /></div>
    <div><img src="落叶图片.png" /></div>
  </div>
  <div class="leaf leaf2">
    // 重复第二组
  </div>
  <div class="leaf leaf3">
    // 重复第三组
  </div>
</section>
.leaf {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}
.leaf img {
  width: 75px;
  height: 75px;
}
.leaf div:nth-child(1) {
  left: 20%;
  animation: fall 22s linear infinite;
  animation-delay: -2s;
}
.leaf div:nth-child(2) {
  left: 70%;
  animation: fall 18s linear infinite;
  animation-delay: -4s;
}
.leaf div:nth-child(3) {
  left: 10%;
  animation: fall 21s linear infinite;
  animation-delay: -7s;
}
.leaf div:nth-child(4) {
  left: 50%;
  animation: fall 24s linear infinite;
  animation-delay: -5s;
}
.leaf div:nth-child(5) {
  left: 85%;
  animation: fall 19s linear infinite;
  animation-delay: -5s;
}
.leaf div:nth-child(6) {
  left: 15%;
  animation: fall 23s linear infinite;
  animation-delay: -10s;
}
.leaf div:nth-child(7) {
  left: 90%;
  animation: fall 20s linear infinite;
  animation-delay: -4s;
}
.leaf2 {
  transform: scale(1.6) translate(5%, -5%) rotate(15deg);
  filter: blur(1px);
  z-index: 10;
}
.leaf3 {
  filter: blur(2px);
  transform: scale(0.8) translate(-5%, 10%) rotate(170deg);
}
@keyframes fall {
  0% {
    top: -30%;
    transform: translateX(20px) rotate(0deg);
  }
  20% {
    transform: translateX(-20px) rotate(45deg);
  }
  40% {
    transform: translateX(20px) rotate(90deg);
  }
  60% {
    transform: translateX(-20px) rotate(135deg);
  }
  80% {
    transform: translateX(20px) rotate(180deg);
  }
  100% {
    top: 150%;
    transform: translateX(-20px) rotate(225deg);
  }
}

主要就是通过清晰与模糊两种状态的对比,速度的差异,来构建视差效果。

CodePen Demo -- Falling leaves

以上就是利用模糊实现视觉3D效果实例讲解的详细内容,更多关于模糊实现视觉3D的资料请关注菜鸟教程https://www.piaodoo.com/其它相关文章!

                        t="" class="has" height="189" src="//img.jbzj.com/file_images/article/202108/2021082810463810.png" width="800" />&#160;</p>

这种办法是可行的,但是也是没有太大的必要,我们可以进行简单的语句顺序调换,把条件判断放到最后,我们的代码就成了这样。

同样,可以达到我们预期的效果:

setInterval函数执行,会返回一个定时参数,这里我们叫interval,当我们需要清除定时器的时候,就直接传入这个参数,如clearInterval(interval)。 

到此这篇关于HTML DOM setInterval和clearInterval方法案例详解的文章就介绍到这了,更多相关HTML DOM setInterval和clearInterval方法内容请搜索菜鸟教程https://www.piaodoo.com/以前的文章或继续浏览下面的相关文章希望大家以后多多支持菜鸟教程https://www.piaodoo.com/!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值