css3灯塔3d灯光特效

2023-12-21 09:50

本文主要是介绍css3灯塔3d灯光特效,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

CSS3灯塔3d灯光特效

灯塔象征守护神和阳光,寓意希望,坚定而执着,始终如一不离不弃。灯塔,为一切夜里航行的人,用火光把道路照明,无私奉献。灯塔为迷茫的船只指引航行的方向,勇于承担,责任重大。

1.设计思路

  • 利用CSS3画出灯塔的外观
  • 画出灯塔射出的灯光并完成动画
  • 将灯塔封装为标签

2.编码

1)html灯塔布局

html:

<div id="lighthouse"><div id="top"></div><div id="lighttop"></div><div id="lightcontent"></div><div id="lightbottom"></div><div id="towerbody"></div><div id="towerlight"></div>
</div>

css:

:root{--width:150px;--height:250px;
}
#lighthouse{width: var(--width);height: var(--height);position: absolute;right: 0;bottom: 0;
}
#lighthouse #top{width: 0px;height: 0px;border-bottom: calc(var(--width)*0.2) solid #c0c0c0;border-left: calc(var(--width)*0.2)  solid transparent;border-right: calc(var(--width)*0.2)  solid transparent;margin: 0 auto;
}
#lighthouse #lighttop,#lightbottom{width: calc(var(--width)*0.5);height: calc(var(--height)*0.05);background-color: palegoldenrod;margin: 0 auto;border-radius: 8%;
}
#lighthouse #lightcontent{width: calc(var(--width)*0.4);height: calc(var(--height)*0.12);background-color: yellow;opacity: 0.5;margin: 0 auto;
}
#lighthouse #towerbody{width: calc(var(--width)*0.4);border-bottom: calc(var(--height)*0.5) solid skyblue;border-left: calc(var(--width)*0.15)  solid transparent;border-right: calc(var(--width)*0.15)  solid transparent;margin: 0 auto;
}
#lighthouse #towerlight{height: calc(var(--height)*0.12);border-bottom: calc(var(--height)*0.1) solid transparent;border-left: calc(var(--width)*1.5)  solid yellow;border-top: calc(var(--height)*0.1)  solid transparent;position: relative;top: calc(var(--height)*-0.77);left: calc(var(--height)*-0.6);animation: light-change 3s linear alternate infinite;transform-origin:100%;transform-style: preserve-3d;
}

效果如下:
在这里插入图片描述

2)css动画特效

#lighthouse #towerlight{height: calc(var(--height)*0.12);border-bottom: calc(var(--height)*0.1) solid transparent;border-left: calc(var(--width)*1.5)  solid yellow;border-top: calc(var(--height)*0.1)  solid transparent;position: relative;top: calc(var(--height)*-0.77);left: calc(var(--height)*-0.6);animation: light-change 3s linear alternate infinite;transform-origin:100%;transform-style: preserve-3d;
}
@keyframes light-change{0%{opacity: 0.1;transform: rotate3d(0,1,0,90deg);}50%{opacity: 0.3;transform: rotate3d(0,1,0,135deg);}100%{opacity: 0.1;transform: rotate3d(0,1,0,180deg);}
}

效果如下:
在这里插入图片描述

3)js点击改变灯光颜色

js:

var colors=new Array('yellow','red','white');var colorchoice=0;var lightcontent=document.getElementById('lightcontent');var towerlight=document.getElementById('towerlight');lightcontent.onclick=function(){colorchoice++;lightcontent.style.backgroundColor=colors[colorchoice%colors.length];towerlight.style.borderLeftColor=colors[colorchoice%colors.length];}

效果如下:
在这里插入图片描述

3.封装为自定义标签

1)引用方式(标签语法)

<light-tower width="150px" height="250px" id="lighttower" color="yellow,red,orange,purple"></light-tower>
<!--
width:灯塔宽度 height:灯塔高度 如果这两个属性为空,则灯塔宽高为0
color:灯塔灯光颜色集,可以有1-多个值,用“,”间隔开,第一个值为默认灯光颜色
color属性未设置时,默认颜色集为"yellow,red,white"
-->

2)实现

引入js文件方式:
1.直接引入博主的(定期更新完善)

<script type="text/javascript" src="http://121.196.148.73/js/lighttower.js"></script>

2.自己创建本地js文件,代码如下:

class LightHouse extends HTMLElement{constructor(){super();// 创建根元素,作用其实是将分离的css和html聚合起来var shadow = this.attachShadow({ mode: 'open' });//创建包裹元素var lighthouse=document.createElement('div');lighthouse.setAttribute('id','lighthouse');//创建子元素var top=document.createElement('div');top.setAttribute('id','top');var lighttop=document.createElement('div');lighttop.setAttribute('id','lighttop');var lightcontent=document.createElement('div');lightcontent.setAttribute('id','lightcontent');var lightbottom=document.createElement('div');lightbottom.setAttribute('id','lightbottom');var towerbody=document.createElement('div');towerbody.setAttribute('id','towerbody');var towerlight=document.createElement('div');towerlight.setAttribute('id','towerlight');//获取自定义标签的属性var width=this.getAttribute('width');var height=this.getAttribute('height');var color=this.getAttribute('color');if(color==null||color==="")color="yellow,red,white";//设置样式var style=document.createElement('style');style.textContent=`#lighthouse{width: `+width+`;height: `+height+`;}#lighthouse #top{width: 0px;height: 0px;border-bottom: calc(`+width+`*0.2) solid #c0c0c0;border-left: calc(`+width+`*0.2)  solid transparent;border-right: calc(`+width+`*0.2)  solid transparent;margin: 0 auto;}#lighthouse #lighttop,#lightbottom{width: calc(`+width+`*0.5);height: calc(`+height+`*0.05);background-color: palegoldenrod;margin: 0 auto;border-radius: 8%;}#lighthouse #lightcontent{width: calc(`+width+`*0.4);height: calc(`+height+`*0.12);background-color: yellow;opacity: 0.5;margin: 0 auto;}#lighthouse #towerbody{width: calc(`+width+`*0.4);border-bottom: calc(`+height+`*0.5) solid skyblue;border-left: calc(`+width+`*0.15)  solid transparent;border-right: calc(`+width+`*0.15)  solid transparent;margin: 0 auto;}#lighthouse #towerlight{height: calc(`+height+`*0.12);border-bottom: calc(`+height+`*0.1) solid transparent;border-left: calc(`+width+`*1.5)  solid yellow;border-top: calc(`+height+`*0.1)  solid transparent;position: relative;top: calc(`+height+`*-0.77);left: calc(`+height+`*-0.6);animation: light-change 3s linear alternate infinite;transform-origin:100%;transform-style: preserve-3d;}@keyframes light-change{0%{opacity: 0.1;transform: rotate3d(0,1,0,90deg);}50%{opacity: 0.3;transform: rotate3d(0,1,0,135deg);}100%{opacity: 0.1;transform: rotate3d(0,1,0,180deg);}}`;var colors=new Array();colors=color.split(",");var colorchoice=0;lightcontent.onclick=function(){colorchoice++;lightcontent.style.backgroundColor=colors[colorchoice%colors.length];towerlight.style.borderLeftColor=colors[colorchoice%colors.length];}shadow.appendChild(lighthouse);shadow.appendChild(style);lighthouse.appendChild(top);lighthouse.appendChild(lighttop);lighthouse.appendChild(lightcontent);lighthouse.appendChild(lightbottom);lighthouse.appendChild(towerbody);lighthouse.appendChild(towerlight);}
}
customElements.define('light-tower', LightHouse);


END


创作不易,点个赞再走吧~

这篇关于css3灯塔3d灯光特效的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/519508

相关文章

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F