CSS-3D之制作3D骰子

CSS-3D之制作3D骰子

css3 3d其实还是转换的一种,css3提供了2D转换,里面有位移、缩放、旋转、斜拉,这些个效果都是在2d平面上的效果。那么3d其实就是多了一个维度,也就是多了个坐标轴。

页面中想做3d效果就需要一个东西,就是盛放3d变换的元素的容器,咱们把它叫做舞台<div class='stage'></div>,在stage的css加上transform-style: preserve-3d      可以不写哦(默认会判断)~~

transform-origin:50% 50%(默认)。

还有一个要说的就是perspective

写法有两种情况:
一、写到父元素
属性值:属性值
perspective: 1200px;
二、写到子元素,起效果的那个元素
 transform:perspective(1200px) translateZ(300px) translate(30px,30px);

总之呢,就是四个字,近大远小

再来看看这个东西perspective-origin,其实就像相机对焦一样,就是焦点嘛,眼睛聚焦的地方。

最后再看一个东西。

backface-visibility backface-visibility 属性定义当元素不面向屏幕时是否可见。

visible 背面是可见的。
hidden 背面是不可见的。

二话不说,直接上干货。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>骰子</title>
	<style>
		body{
			background:#ccc;
		}
		p{
			padding:0;
			margin:0;
		}
		/*创建舞台的样式*/
		.stage{
			width:100px;
			height:100px;
			/*background:#fff;*/
			perspective: 2000px;
			margin:100px auto 0;
			transform-style:preserve-3d; 
			border:.0px solid gray;
			position: relative;
			transition: all 1s;
			animation:;
			transform:rotateX(-25deg) rotateY(15deg);
			
		}
		.stage:hover{
			transform: rotateX(180deg) rotateY(180deg);
		}
		/*创建面的样式*/
		.con{
			width:100%;
			height:100%;
			background: #fff;
			opacity: 1;
			border-radius: 10px;
			position: absolute;
		}
		/*创点的样式*/
		.dot{
			width:25px;
			height: 25px;
			border-radius: 50%;
			background:blue;
		}
		/*	控制点的位置*/
		.stage .con:first-child{
			display:flex;
			justify-content: center;
			align-items: center;
		}
		.stage .con:first-child .dot{
			width:40px;
			height: 40px;
			background: red;
		}
		.stage .con:nth-child(4) .dot{
			background: red;
		}
		
		.stage .con:nth-child(2){
			display:flex;
			justify-content:space-around;
			
		}
		.stage .con:nth-child(2) .dot:nth-child(2){
			align-self: flex-end;
		}
		.stage .con:nth-child(3){
			display:flex;
			justify-content:space-around;
			
		}
		.stage .con:nth-child(3) .dot:nth-child(2){
			align-self: center;
		}
		.stage .con:nth-child(3) .dot:nth-child(3){
			align-self: flex-end;
		}
		.stage .con:nth-child(4){
			display:flex;
			flex-direction: column;
			justify-content:space-around;
			
		}
		.stage .con:nth-child(4) .scon{
			display: flex;
			justify-content:space-around;
		}
		.stage .con:nth-child(5){
			display:flex;
			flex-direction: column;
			justify-content:space-around;
			
		}
		.stage .con:nth-child(5) .scon{
			display: flex;
			justify-content:space-around;
		}
		.stage .con:nth-child(5) .scon:nth-child(2){
			display: flex;
			justify-content:center;
		}
		.stage .con:nth-child(6){
			display:flex;
			flex-direction: column;
			justify-content:space-around;
			
		}
		.stage .con:nth-child(6) .scon{
			display: flex;
			justify-content:space-around;
		}
		/*旋转六个面,形成一个立方体*/
		.stage .con:first-child{
			transform: translateZ(50px);
		}
		.stage .con:nth-child(6){
			transform: translateZ(-50px);
		}
		.stage .con:nth-child(2){
			transform: translateX(-50px) rotateY(90deg);
		}
		.stage .con:nth-child(5){
			transform: translateX(50px) rotateY(90deg);
		}
		.stage .con:nth-child(3){
			transform: translateY(-50px) rotateX(90deg);
		}
		.stage .con:nth-child(4){
			transform: translateY(50px) rotateX(90deg);
		}
	</style>
</head>
<body>
<div class="stage">
	<div class="con">
		<p class="dot"></p>
	</div>
	<div class="con">
		<p class="dot"></p>
		<p class="dot"></p>
	</div>
	<div class="con">
		<p class="dot"></p>
		<p class="dot"></p>
		<p class="dot"></p>
	</div>
	<div class="con">
		<div class="scon">
			<p class="dot"></p>
			<p class="dot"></p>
		</div>
		<div class="scon">
			<p class="dot"></p>
			<p class="dot"></p>
		</div>
	</div>
	<div class="con">
		<div class="scon">
			<p class="dot"></p>
			<p class="dot"></p>
		</div>
		<div class="scon">
			<p class="dot"></p>
		</div>
		<div class="scon">
			<p class="dot"></p>
			<p class="dot"></p>
		</div>
	</div>
	<div class="con">
		<div class="scon">
			<p class="dot"></p>
			<p class="dot"></p>
		</div>
		<div class="scon">
			<p class="dot"></p>
			<p class="dot"></p>
		</div>
		<div class="scon">
			<p class="dot"></p>
			<p class="dot"></p>
		</div>
	</div>
	
</div>
	<button>随机旋转</button>
</body>
<script src="jquery.js"></script>
<script>
   //随机旋转角度
	$("button").click(function(){
		var arr=[-4,-3,-2,-1,0,1,2,3,4];
		var a = Math.floor(Math.random()*9);
		var b = Math.floor(Math.random()*9);
		$(".stage").css({
			transform: "rotateX("+(arr[a]*90)+"deg) rotateY("+(arr[b]*90)+"deg)"
		});
		
	});
</script>
</html>



  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS-in-CSS是一种前端开发技术,也被称为CSS嵌套或CSS模块化。它的主要思想是将CSS样式定义在组件级别,使得样式与组件的逻辑和结构紧密关联,提高代码的可维护性和可重用性。 在传统的CSS开发中,样式是全局共享的,容易造成样式冲突和难以维护。而CSS-in-CSS通过将样式封装在组件内部,实现了样式的局部作用域。这样一来,每个组件都可以拥有自己独立的样式规则,不会相互干扰。 CSS-in-CSS有多种实现方式,其中比较常见的有以下几种: 1. CSS Modules:CSS Modules是一种使用Webpack等构建工具实现的CSS-in-CSS方案。它通过给每个CSS类名添加唯一的哈希值,实现了样式的局部作用域。在使用时,可以通过import语法引入CSS文件,并在组件中使用对应的类名。 2. CSS-in-JS:CSS-in-JS是一种将CSS样式直接写在JavaScript代码中的方式。它通过使用JavaScript对象来描述样式规则,并在运行时动态生成对应的CSS。常见的CSS-in-JS库有styled-components、Emotion等。 3. CSS Nesting:CSS Nesting是一种提案,旨在原生CSS中实现嵌套样式的语法。它使用类似于Sass的嵌套语法,可以更方便地描述组件内部的样式关系。目前,CSS Nesting还处于实验阶段,尚未得到所有浏览器的广泛支持。 总的来说,CSS-in-CSS是一种将CSS样式与组件紧密结合的开发方式,可以提高代码的可维护性和可重用性。不同的实现方式适用于不同的项目需求和开发环境。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值